一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - mybatis注解與xml常用語句匯總

mybatis注解與xml常用語句匯總

2021-05-31 11:05wotrd Java教程

最近一直在用mybatis,由于需要使用到了動態sql,遇到了一些問題,現在來總結一下,經驗教訓。下面這篇文章主要給大家總結介紹了mybatis注解與xml常用語句的相關資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下

前言

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 91香蕉视频在线 | 美女把腿开让我 | 92精品国产成人观看免费 | 亚洲精品动漫在线观看 | 国产乱子伦在线观看不卡 | 男人操女人动图 | 沉香如屑西瓜视频免费观看完整版 | 亚色九九九全国免费视频 | 天天射夜夜爽 | 1769亚洲资源站365在线 | 亚洲色图综合网 | 色综合久久丁香婷婷 | 久久久久久久久性潮 | 国产高清专区 | 日韩免费一级片 | 波多野结衣中文字幕乱七八糟 | 婷婷色在线 | 99精品视频在线观看免费 | 国产成人免费a在线资源 | 久久青草免费91线频观看站街 | 色老板在线视频观看 | 91久久国产| 每天都要睡男人(nph) | 日本大学生xxxxx69泡妞 | 亚洲aⅴ天堂 | 99久久免费国产特黄 | 国产我不卡 | 日本videossexx日本人 | 好大好爽好涨太深了小喜 | 男女姓交大视频免费观看 | 啊啊啊好大视频 | 日韩毛片大全免费高清 | 日本美女xx | 久久精品国产视频澳门 | 男模chinesegayxxxx | 国产一页 | 午夜国产精品 | 日韩精品福利视频一区二区三区 | 毛片影院 | 色久久一个亚洲综合网 | 日本男男gayxxxxx免费 |