MyBatis—SQL语句的动态拼接

SQL的动态拼接

            if标签    where标签    choose when otherwise标签    set标签    
            trim标签    bind标签    sql和include标签    foreach标签
LogMapper.xml

1.if标签:
  test中写判断条件 参数直接paramN或者别名
  特点:
  只要成立就拼接在Sql语句中,都成立就全部都拼接
  注意:
  where子句中加上1=1来规避and的风险

          <select id="selg" resultType="log">
  select * from log where 1=1
  <if test="param1!=null and param1!=''">
  and outno=#{param1}
  </if>
  <if test="param2!=null and param2!=''">
  and inno=#{param2}
  </if>
  </select>
2.where标签:
特点:
会自动的给Sql语句添加where关键字,并将第一个and去除。
          <select id="selw" resultType="log">
  select * from log
  <where>
  <if test="param1!=null and param1!=''">
  and outno=#{param1}
  </if>
  <if test="param2!=null and param2!=''">
  and inno=#{param2}
  </if>
  </where>
  </select>
3.choose when otherwise标签
  特点:
  条件只要有一个成立,其他的就不会再判断了。
  如果没有成立的条件则默认执行otherwise中的内容
       <select id="selc" resultType="log">
  select * from log
  <where>
  <choose>
  <when test="param1!=null and param1!=''">
  and outno=#{param1}
  </when>
  <when test="param2!=null and param2!=''">
  and inno=#{param2}
  </when>
  <otherwise>
  and 1=1
  </otherwise>
  </choose>
  </where>
  </select>
4.set标签:
  产生一个set关键字,自动去除最后一个逗号。
  注意:
  在判断条件中最后保持有一个永远成立的条件。避免sql错误。
          <update id="upA">
  update account 
  <set>
  <if test="aname!=null and aname!=''">
  aname=#{aname},
  </if>
  <if test="money !=null  and money !=''">
  money=#{money},
  </if>
  <if test="ano !=null  and ano !=''">
  ano=#{ano},
  </if>
  </set>
  where  ano=#{ano}
  </update>
5.trim标签:
  prefix:在trim的内容前添加指定的内容
  prefixOverrides在trim的内容前去除指定的内容
  suffix:在trim的内容后添加指定的内容
  suffixOverrides:在trim的内容后去除指定的内容
  注意:
  先去除后添加
  添加内容会默认添加一个空格。
          <update id="upT" parameterType="account">
  update account 
  <trim prefix="$" prefixOverrides="" suffix="" suffixOverrides="">
  <if test="ano !=null  and ano !=''">
  ano=#{ano},
  </if>
  <if test="aname!=null and aname!=''">
  aname=#{aname},
  </if>
  <if test="money !=null  and money !=''">
  money=#{money},
  </if>
</trim>
  where ano=#{ano}
  </update>
6.bind标签:
  name:参数名
  value:表达式,注意字符串拼接按照变量方式进行拼接
  例如:
  <bind name="money" value="'$'+money"/>
给参数重新赋值
          <update id="upB" parameterType="account">
<bind name="money" value="money+100"/>
  update account 
  <trim prefix="set" suffixOverrides=",">
  <if test="ano !=null  and ano !=''">
  ano=#{ano},
  </if>
  <if test="aname!=null and aname!=''">
  aname=#{aname},
  </if>
  <if test="money !=null  and money !=''">
  money=#{money},
  </if>
</trim>
  where ano=#{ano}
  </update>
7.sql和include标签:
  sql标签:在外部声明公用SQL语句
  id
  include标签:引入声明的公共SQL语句
  refid:
  优点:便于SQL的整体修改
  缺点:难于阅读
             <select id="selA" resultType="account">
  select <include refid="mysql"></include> from account
  </select>
  
  <sql id="mysql">
  ano,aname,apwd,money
  </sql>
8.foreach标签:
  collection:要遍历的集合对象
  item:记录每次遍历的结果
  open:在结果的左边添加内容
  separator:结果和结果之间的内容
  close:在最后添加的内容
              <select id="selF" parameterType="list" resultType="account">
  select * from account where ano in
  <foreach collection="list" item="item" open="(" separator="," close=")">
  #{item}
  </foreach>
  </select>
  
  <insert id="inF">
  insert into log values 
  <foreach collection="list"  item="log" separator=",">
  (#{log.outno},#{log.inno},#{log.money})
  </foreach>  
  </insert>

以上是mapper.xml里边的代


LogMapper.java   (和LogMapper.xml一样名字的接口interface)


Test.java


木子璇总结时刻:欢迎小伙伴们提出建议哦,如有错误,望大神指出哦,谢谢啦。

  • 8
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis SQL语句的进阶主要包括以下几个方面: 1. 通过查询时自定义别名的方式解决名称不一致而导致的无法封装数据的问题。在查询语句中,可以使用AS关键字为查询结果的列指定别名,从而解决结果集与实体类属性名称不一致的问题。 2. 使用resultMap节点解决名称不一致而导致的无法封装数据的问题。resultMap节点可以在映射文件中定义,用于将查询结果映射到实体类中。通过定义resultMap节点,我们可以灵活地指定查询结果列与实体类属性之间的对应关系,从而解决名称不一致的问题。 3. 一对一的关联查询。在MyBatis中,可以通过嵌套查询或者使用association标签来实现一对一的关联查询。嵌套查询是指在resultMap中定义一个嵌套的resultMap,通过association标签来指定关联关系。这样可以在查询数据的同时,将关联的数据一并查询出来并映射到实体类中。 4. 一对多的关联查询。在MyBatis中,可以通过collection标签来实现一对多的关联查询。collection标签可以在resultMap中定义,用于指定集合属性的映射关系。通过定义collection标签,我们可以在查询数据的同时,将关联的多个数据一并查询出来并映射到实体类中。 5. 通过使用动态SQL来实现更灵活的查询。MyBatis提供了丰富的动态SQL语法,在查询时可以根据条件进行判断、循环等操作,从而实现更灵活的查询。比如可以使用if标签、choose标签、foreach标签等来动态拼接查询条件,以满足不同的查询需求。 总结:MyBatis SQL语句的进阶包括通过自定义别名、使用resultMap节点解决名称不一致的问题,实现一对一和一对多的关联查询,以及使用动态SQL实现更灵活的查询。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值