前言
mybatis是一個支持普通sql查詢,存儲過程和高級映射的優秀持久層框架。mybatis消除了幾乎所有的jdbc代碼和參數的手工設置以及對結果集的檢索封裝。mybatis可以使用簡單的xml或注解用于配置和原始映射,將接口和java的pojo(plain old java objects,普通的java對象)映射成數據庫中的記錄。
本文將給大家詳細介紹關于mybatis注解與xml常用語句的相關內容,下面話不多說了,來一起看看詳細的介紹吧
mybatis注解使用
1.簡單crud
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public interface usermapper { //查詢 @select ( "select * from user where id=#{id}" ) user selectuser( int id); //查詢全部 @select ( "select * from user" ) list<user> selectuserlist(); //增加數據 @insert ( "insert into user (name) values(#{name})" ) boolean insertuser(string name); //修改用戶 @update ( "update user set name=#{name} where id=#{id}" ) boolean updateuser( @param ( "name" ) string name, @param ( "id" ) int id); //刪除用戶 @delete ( "delete from user where id=#{id}" ) boolean deleteuser( int id); } |
2.一對一注解
1
2
3
4
5
6
7
8
9
10
|
@select ( "select * from user" ) @results ({ @result (id = true ,property = "id" ,column = "id" ), //id=true 對應于主鍵 @result (property = "uid" ,column = "uid" ), @result (property = "user" ,column = "uid" ,javatype = user. class , one = @one (select = "com.example.dao.userdao.finduserbyid" ,fetchtype = fetchtype. default )) //user 對應實體類中一對一的實體類名字,uid表示通過uid外鍵查詢user,javatype表示查詢結果 //映射成user類型對象,one=表示一對xx fetchtype.default默認是立即加載全部查詢,使用lazy懶加載需要才查詢 }) list<user> selectuserlist(); |
3,一對多注解
mybatis的xml配置
1.配置resultmap
1
2
3
4
5
6
7
|
<resultmap id= "baseresultmap" type= "xx" > <id column= "id" property= "id" jdbctype= "bigint" /> <result column= "aa" property= "aa" jdbctype= "varchar" /> <result column= "bb" property= "bb" jdbctype= "integer" /> <result column= "cc" property= "cc" jdbctype= "decimal" javatype= "java.math.bigdecimal" /> <result column= "dd" property= "dd" jdbctype= "date" /> </resultmap> |
2.通用sql短語
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<sql id= "base_column_list" > aa, bb </sql> <sql id= "where" > <trim prefix= "where" prefixoverrides= "and|or" > < if test= "id != null and id != ''" > and t.id = #{id} </ if > < if test= "content != null and content != ''" > and t.content like concat( '%' , #{content}, '%' ) </ if > and t.app_code in <foreach item= "item" index= "index" collection= "appcodes" open= "(" separator= "," close= ")" > #{item} </foreach> and t.user_id=u.id and t.removed= 0 </trim> </sql> |
3.需要驗證的插入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<insert id= "insert" parametertype= "xxx" usegeneratedkeys= "true" keycolumn= "id" keyproperty= "id" > insert into xxx ( <trim suffixoverrides= "," > < if test= "title != null and title != '' " > title , </ if > </trim> ) values ( <trim suffixoverrides= "," > < if test= "title != null and title != '' " > #{title} , </ if > </trim> ) </insert> |
4.需要驗證的更新
1
2
3
4
5
6
7
8
9
10
|
<update id= "update" parametertype= "xxx" > update xxx <set> < if test= "title != null and title != '' " > title = #{title} , </ if > </set> where id = #{id} </update> |
5.<!--批量更新ticketid和seatno-->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<update id= "xxxupdate" parametertype= "java.util.list" > update xxx <trim prefix= "set" suffixoverrides= "," > <trim prefix= "aa =case" suffix= "end," > <foreach collection= "orders" item= "item" index= "index" > < if test= "item.aa !=null" > when id=#{item.id} then #{item.aa} </ if > </foreach> </trim> <trim prefix= "bb =case" suffix= "end," > <foreach collection= "orders" item= "item" index= "index" > < if test= "item.bb !=null" > when id=#{item.id} then #{item.bb} </ if > </foreach> </trim> </trim> where id in <foreach collection= "orders" index= "index" item= "item" separator= "," open= "(" close= ")" > #{item.id,jdbctype=bigint} </foreach> </update> |
mybatis可以使用string給數據庫int類型賦值
springboot中開啟日志
1
|
#mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.stdoutimpl |
1.order by ${columnname}
這里 mybatis 不會修改或轉義字符串。note 用這種方式接受用戶的輸入,并將其用于語句中的參數是不安全的,會導致潛在的 sql 注入攻擊,因此要么不允許用戶輸入這些字段,要么自行轉義并檢驗。
2.如何使用連接池。
首先實例化連接池數據源對象,讓他實現datasourcefactory這個接口。然后實現方法。在mybatis。conf文件中設置數據連接池這個類,將數據庫連接信息放在config.properties文件中。
3.mybatis.config文件中setting和數據源的設置參數區別
會被覆蓋。
4.連接參數查詢順序
首先查詢properties文件,然后查詢resource文件,最后查詢方法參數。重復的話會被覆蓋。
5.druid連接池配置方式:
詳見官網
druiddatasourcefactory首先實行setproperties方法,然后返回設置數據源方法。drui數據源也需要在datasource中設置properties文件
6.實體類的方法不定義也可以進行映射
7.mybatis默認是事務不提交
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:https://segmentfault.com/a/1190000016333566