美文网首页
mybatis标签的使用

mybatis标签的使用

作者: 扎Zn了老Fe | 来源:发表于2018-12-15 22:36 被阅读0次

定义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

相关文章

网友评论

      本文标题:mybatis标签的使用

      本文链接:https://www.haomeiwen.com/subject/gvqzhqtx.html