MyBatis的动态sql

大纲

在这里插入图片描述

动态SQL拼接

if 标签的使用

<if test="studentName!=null and studentName!='' "> <!--studentName是字段名-->
</if>
<!-- 进行空字符串校验 -->

foreach 标签的使用

<foreach collection="array" item="id" index="index" open="(" close=")" separator=",">
	#{id}
</foreach>
<!-- 
<!-- 传递List,List中是pojo https://www.cnblogs.com/sh086/p/8375791.html
array是类型 array list map set
item : 表示在迭代过程中每一个元素的别名
index :表示在迭代过程中每次迭代到的位置(下标)
使用Map(或Map.Entry对象的集合)时,index将是关键对象,而item将是value对象
-->

<!-- 案例 -->
<select id="dynamicForeachTest" parameterType="java.util.List" resultType="Blog">
  select * from t_blog where id in
  <foreach collection="list" index="index" item="item" open="(" separator=","close=")">
		#{item}       
  </foreach>    
</select>

choose 标签的使用

<choose>     
	<when test="studentName!=null and studentName!='' ">     
		ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')      
	</when>     
	<when test="studentSex!= null and studentSex!= '' ">     
		AND ST.STUDENT_SEX = #{studentSex}      
	</when>
</choose>
<!-- 可以自动处理第一个and -->

格式化标签

where标签

<where>   
	<if test="name!=null and name!='' ">     
		NAME LIKE CONCAT(CONCAT('%', #{name}),'%')      
	</if>     
	<if test="hobby!= null and hobby!= '' ">     
        AND hobby = #{hobby}      
	</if>  
</where>  
<!-- 
替代了where 1=1
如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉
-->

set标签

<update id="updateStudent" parameterType="Object">     
    UPDATE STUDENT SET    
        <if test="name!=null and name!='' ">     
            NAME = #{name},      
        </if>     
        <if test="hobby!=null and hobby!='' ">     
            MAJOR = #{major},      
        </if> 
        <if test="hobby!=null and hobby!='' ">     
            HOBBY = #{hobby}    
        </if>          
    WHERE ID = #{id};      
</update>
<!--
	如果最后的if语句没有生效,会自动剔除末尾if语句中多余的逗号
-->

trim标签

trim属性主要有以下四个

  • prefix:前缀

  • suffix:后缀

  • prefixOverrides:前缀判断的条件

  • suffixOverrides:后缀判断的条件

    AND name=#{name} AND hobby=#{hobby}

配置关联关系

association标签

<resultMap type="org.zang.domain.Person" id="personMapper">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sex" column="sex"/>
        <result property="age" column="age"/>

        <!-- 一对一关联映射:association   -->
        <association property="card" column="card_id"
        select="org.zang.mapper.CardMapper.selectCardById" 
        javaType="org.zang.domain.Card"/>
</resultMap>
<!-- <association.../>元素来映射一对一的关联关系 -->
<mapper namespace="org.zang.mapper.CardMapper">
    <!-- 根据id查询Card,返回Card对象 -->
  <select id="selectCardById" parameterType="int" resultType="org.zang.domain.Card">
      SELECT * from tb_card where id = #{id} 
  </select>
</mapper>

collection标签

<!-- 映射Clazz对象的resultMap -->
    <resultMap type="org.zang.domain.Clazz" id="clazzResultMap">
        <id property="id" column="id"/>
        <result property="code" column="code"/>
        <result property="name" column="name"/>
        
        <!-- 一对多关联映射:collection fetchType="lazy"表示懒加载  -->
        <collection property="students" javaType="ArrayList" column="id" ofType="org.zang.domain.Student" 
        select="org.zang.mapper.StudentMapper.selectStudentByClazzId" fetchType="lazy">
          <id property="id" column="id"/>
          <result property="name" column="name"/>
          <result property="sex" column="sex"/>
          <result property="age" column="age"/>
      </collection>
    </resultMap>
<!-- <collection.../>元素映射一对多的关联关系 -->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值