MyBatis之动态Sql拼接

深碍√TFBOYSˉ_ 2022-10-25 04:50 1593阅读 0赞

MyBatis之动态Sql拼接

前言

Mybatis 的映射⽂件中,前⾯我们的 SQL 都是⽐较简单的,有些时候业务逻辑复杂时,我们的 SQL是
动态变化的,在MyBatis之前对于一些复杂的 SQL 对于我们业务开发时候是不支持的,有时候需要我们为了一两个参数从而去编写重复的sql语句,对此,MyBatis提供了动态Sql去根据不同的条件动态的生成sql语句,极大了对复杂业务查询提供了便利性。

对此我们可以根据不同的取值,使用不同的Sql去查询。
业务查询实时可以根据id,或者name的传入而决定去查询所有还是根据条件查询。

Where 和 If 标签

MyBatis可以使用whereif去动态拼接Sql,从而生成不同的Sql处理业务逻辑。

  1. <select id="findByCondition" parameterType="user" resultType="user">
  2. select * from User
  3. <where>
  4. <if test="id!=0">
  5. and id=#{id}
  6. </if>
  7. <if test="username!=null and username!='' ">
  8. and username=#{username}
  9. </if>
  10. </where>
  11. </select>

where 标签相当于 1==1 表示真值条件
if 标签用来条件判断拼接Sql

foreach标签

循环执⾏sql的拼接操作,例如:SELECT * FROM USER WHERE id IN (1,2)。

  1. <select id="findByIds" parameterType="list" resultType="user">
  2. select * from User
  3. <where>
  4. <foreach collection="list" open="id in(" close=")" item="id" separator=",">
  5. #{id}
  6. </foreach>
  7. </where>
  8. </select>

foreach标签的属性含义如下:

标签⽤于遍历集合,它的属性:

  • collection:代表要遍历的集合元素,注意编写时不要写#{}
  • open:代表语句的开始部分
  • close:代表结束部分
  • item:代表遍历集合的每个元素,⽣成的变量名
  • sperator:代表分隔符

SQL⽚段抽取

Sql 中可将重复的 sql 提取出来,使⽤时⽤ include 引⽤即可,最终达到 sql 重⽤的⽬的

  1. <!--抽取sql⽚段简化编写-->
  2. <sql id="selectUser" select * from User</sql>
  3. <select id="findById" parameterType="int" resultType="user">
  4. <include refid="selectUser"></include> where id=#{id}
  5. </select>
  6. <select id="findByIds" parameterType="list" resultType="user">
  7. <include refid="selectUser"></include>
  8. <where>
  9. <foreach collection="array" open="id in(" close=")" item="id" separator=",">
  10. #{id}
  11. </foreach>
  12. </where>
  13. </select>

可以看到使用 sql标签将两个sql重复的部分select * from User抽取出来,再使用include标签将其引入。

trim

先说一下 trim 标签4个参数:

  • prefix
    给sql语句拼接的前缀
  • suffix
    给sql语句拼接的后缀
  • prefixOverrides
    去除sql语句前面的关键字或者字符,该关键字或者字符由prefixOverrides属性指定,假设该属性指定为”AND”,当sql语句的开头为”AND”,trim标签将会去除该”AND”
  • suffixOverrides
    去除sql语句后面的关键字或者字符,该关键字或者字符由suffixOverrides属性指定

如果第一个if条件成立 那么 sql 会变成

select name,id from user where name = #{name} …

prefixOverrides 去除了第一个and

suffixOverrides=","
同理 suffixOverrides会去除最后一个的,在update标签中最常用

choose 标签

有的时候我们并不想应用所有的条件,而只是想从多个选项中选择一个,可以使用choose标签

  1. <select id="getStudentListChoose" parameterType="user" resultMap="user">
  2. SELECT * from User WHERE 1=1
  3. <where>
  4. <choose>
  5. <when test="name!=null and student!='' ">
  6. AND name LIKE CONCAT(CONCAT('%', #{name}),'%')
  7. </when>
  8. <when test="sex!= null and sex!= '' ">
  9. AND sex = #{sex}
  10. </when>
  11. <otherwise>
  12. AND AGE = 15
  13. </otherwise>
  14. </choose>
  15. </where>
  16. </select>

MyBatis动态标签可以方便的处理不同条件下生成不同sql语句的场景,很方便我们业务不同条件下sql查询。

发表评论

表情:
评论列表 (有 0 条评论,1593人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Mybatis动态拼接sql

    Mybatis动态拼接sql 需求:查询某张表时条件不确定,可能有一个,可能有多个,也可能没有条件 <!-- 动态sql --> <select id="

    相关 mybatis动态SQL

    where标签 当我们拼接动态SQL时,如果一个查询条件都没有,那我们就不需要where子句,而如果有至少一个条件我们就需要where子句。 示例 <sel