mybatis标签的使用

定义sql语句

select

入参标签 parameterType
基本类型:java.lang.Integer,java.lang.String,java.lang.Long,Date;
复杂类型:自定义类,java.util,java.util.List,java.util.Map
返回结果标签resultType
基本类型:java.lang.Integer,java.lang.String,java.lang.Long,Date;
复杂类型:自定义类,java.util,java.util.List,java.util.Map
返回结果标签resultMap
*resultType的特别版,当数据库表字段和实体类名字不同时,可以自定义个resultMap手动映射

 <!--参数为Long-->
<select id="findUserListByIdList" parameterType="java.lang.Long" resultType="User">  
        select * from user where  id = #{id};  
</select> 

 <!--参数为list-->
<select id="findUserListByIdListV2" parameterType="java.util.ArrayList" resultType="User">  
    select * from user user  
    <where>  
        user.ID in (  
        <foreach item="guard" index="index" collection="list"  
            separator=","> #{guard} </foreach>  
        )  
    </where>  

 <!--参数为map-->
</select>  
<select id="findUserListByIdListV3" parameterType="Map" resultType="User">  
       select * from Teacher where id=#{id} and sex=#{sex} 
  </select>  

 <!--返回结果为map,mapper接口返回类型List<Map<Integer,Integer>>,返回结果: [{id=11, number=1}, {id:13, number:23}],注意最外层是一个list-->
</select>  
<select id="findUserListByIdListV3" parameterType="Integer" resultType="java.util.Map">  
       select id, number  id from Teacher where  sex=#{sex} 
  </select>  

update

同select

insert

同select

delete

同select

if

if标签通常用于WHERE语句、UPDATE语句、INSERT语句中,通过判断参数值来决定是否使用某个查询条件、判断是否更新某一个字段、判断是否插入某个字段的值

include

引用定义的常量,格式为

<include refid="自定义常量" />

foreach

foreach 元素的属性主要有 item,index,collection,open,separator,close。
collection:是在使用foreach的时候最关键的也是最容易出错的,该属性是必须指定的,但是在不同情况下,该属性的值是不一样的,主要有一下3种情况:
(1)如果传入的是单参数且参数类型是一个List的时候,collection属性值为list .
(2)如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array .
(3)如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map,实际上如果你在传入参数的时候,在MyBatis里面也是会把它封装成一个Map的,map的key就是参数名,所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.
item: 表示集合中每一个元素进行迭代时的别名,
index:指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,
open: 表示该语句以什么开始,
separator: 表示在每次进行迭代之间以什么符号作为分隔 符,
close: 表示以什么结束。

<delete id="deleteContactsById" parameterType="list">
        DELETE FROM hotel_prepay_contact
        WHERE id in
        <foreach item="item" index="index" collection="list"
                 open="(" separator="," close=")">
            #{item}
        </foreach>

    </delete>

where:

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG 
 < WHERE>
  <if test="state != null">
    state = #{state}
  </if> 
  <if test="title != null">
    AND title like #{title}
  </if>
  <if test="author != null and author.name != null">
    AND author_name like #{author.name}
  </if>
</WHERE>
</select>

set:

<update id="updateStatus" parameterType="com.meituan.hotel.biz.model.prepaygoodschangeprice.PrepayGoodsChangePriceRecordModel">
        update hotel_biz_prepay_goods_change_price
        <set>
            modify_time = now(),check_time = now(),
            <if test="status != null and status !=''">
                status = #{status},
            </if>
            <if test="reason != null">
                reason = #{reason},
            </if>
            <if test="processingPlatform != null">
                processing_platform = #{processingPlatform},
            </if>
            <if test="traceId != null and traceId !=''">
                trace_id = #{traceId}
            </if>
        </set>
        <where>
            id = #{id} and delete_status = 1
        </where>
    </update>

trim

在where中,如果第一个if没有命中的话,那么就会多出一个and, 如下所示

 SELECT * FROM BLOG  WHERE AND title like #{title} AND author_name like #{author.name}

在set中,如果最后一个if没有命中的话,就会多出一个, , 如下所示

update hotel_biz_prepay_goods_change_price modify_time = now(),check_time = now(),status = #{status}, reason = #{reason},processing_platform = #{processingPlatform}, where  id = #{id} and delete_status = 1

可以使用trim进行改造。
prefix:前缀
prefixOverrides: 去掉第一个字符串
suffix: 后缀
suffixOverrides:去掉最后一个字符串

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG 
 <trim prefix ="WHERE" prefixoverride="AND |OR">
  <if test="state != null">
    state = #{state}
  </if> 
  <if test="title != null">
    AND title like #{title}
  </if>
  <if test="author != null and author.name != null">
    AND author_name like #{author.name}
  </if>
</trim>
</select>

这里是用了prefixoverride="AND |OR"去掉第一个and或者是or。

<update id="updateStatus" parameterType="com.meituan.hotel.biz.model.prepaygoodschangeprice.PrepayGoodsChangePriceRecordModel">
        update hotel_biz_prepay_goods_change_price
        <trim prefix ="set" suffixoverride="," suffix="id = #{id} and delete_status = 1">
            modify_time = now(),check_time = now(),
            <if test="status != null and status !=''">
                status = #{status},
            </if>
            <if test="reason != null">
                reason = #{reason},
            </if>
            <if test="processingPlatform != null">
                processing_platform = #{processingPlatform},
            </if>
            <if test="traceId != null and traceId !=''">
                trace_id = #{traceId}
            </if>
        </trim>
    </update>

suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样
参考:
[1].https://blog.csdn.net/s592652578/article/details/52871884
[2].https://www.jb51.net/article/139173.htm
[3].https://www.cnblogs.com/pjfmeng/p/7688172.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 简介 1.1 什么是 MyBatis ? MyBatis 是支持定制化 SQL、存储过程以及高级映射的优秀的...
    笨鸟慢飞阅读 5,700评论 0 4
  • Java数据持久化之mybatis 一. mybatis简介 1.1 原始的JDBC操作: Java 通过 Jav...
    小Q逛逛阅读 4,977评论 0 16
  • 1 Mybatis入门 1.1 单独使用jdbc编程问题总结 1.1.1 jdbc程序 上边使...
    哇哈哈E阅读 3,336评论 0 38
  • 20170902周慧心赏第19天 亲爱的老公,最近一段时间,你见到我父母都很有礼貌的叫一声爸,叫一声妈,出...
    hmzhou阅读 180评论 0 1
  • 而流传很久后,魔法小熊竟然奇迹般的又出现了。人们很是惊呆,小熊住宿在浩浩的家里,浩浩是一个5岁的小男孩,浩浩很喜欢...
    永远丿微笑阅读 291评论 0 1