mybatis标签——动态sql语句

10 篇文章 0 订阅
5 篇文章 0 订阅

在mapper映射文件中写入

1. if标签

条件写在test

select * from user where 1=1 
  <if test="user.age!=-1 ">
  and user.age=#{userAge}<--and 用于拼接sql语句 -->
   </if>

2. where 标签——解决 where 1=1拼接

select * from user 
<where>
  <if test="userAge!=-1 ">
  and userAge=#{userAge}<--and 用于拼接sql语句 -->
   </if>
     <if test="userSex!=null ">
  and userSex=#{userSex}   <--and 用于拼接sql语句 -->
   </if>
   </where>

3.foreach标签——子查询使用

select * from user where id in (10,20,30 )

select *from user
<where>
<if test="ids != null and ids.size()>0">
<foreach collection="ids" open="and id in (”close=")" item="uid" separator=",">
#{uid}
</ foreach>

其中item决定下面的#{ }里的名字,ids是getIds得到的容器,从中获取item

4. sql标签——抽取重复的代码片段

用于抽取公共代码片段

4.1 抽取片段

<sql id="findUserTab">
select * from user
</sql>

4.2 引入代码片段

<include ref="findUserTab"></include >
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这次最后一次熬夜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值