动力节点首页 全国咨询热线:400-8080-105

绑定手机号,登录
手机号

验证码

微信登录
手机号登录
手机号

验证码

微信登录与注册
微信扫码登录与注册

扫码关注微信公众号完成登录与注册
手机号登录
首页 > 文章

MyBatis的标签使用

07-15 11:32 605浏览
举报 T字号
  • 大字
  • 中字
  • 小字

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

动力节点在线课程涵盖零基础入门,高级进阶,在职提升三大主力内容,覆盖Java从入门到就业提升的全体系学习内容。全部Java视频教程免费观看,相关学习资料免费下载!对于火爆技术,每周一定时更新!如果想了解更多相关技术,可以到动力节点在线免费观看MyBatis视频教程学习哦!

0人推荐
共同学习,写下你的评论
0条评论
代码小兵696
程序员代码小兵696

118篇文章贡献392976字

相关课程 更多>

作者相关文章更多>

推荐相关文章更多>

Java面试题及答案整理

提枪策马乘胜追击04-21 20:01

Spring常见面试题

代码小兵92504-17 16:07

Java零基础实战项目——五子棋

代码小兵98804-25 13:57

Java string类详解

杨晶珍05-11 14:54

6道经典算法面试题

杨晶珍05-12 16:39

发评论

举报

0/150

取消