MyBatis知识盘点【伍】_动态sql
来源:互联网 发布:下载软件的应用 编辑:程序博客网 时间:2024/05/18 17:40
上篇文章:MyBatis知识盘点【肆】_映射器
支持的元素有if、choose、trim、foreach和bind,下面分别介绍:
if:单条件分支判断
SELECT `user_id`,`platform`,`orderId`,`product_id`,`product_name`,NOW(),NOW(),NOW(),app_business_id,app_group_id,
<iftest="type ==2"><!-- 半月 -->
DATE_ADD(NOW(),INTERVAL 15 DAY)
</if>
<iftest="type ==3"><!-- 包月 -->
DATE_ADD(NOW(),INTERVAL 1 MONTH)
</if>
<iftest="type ==4"><!-- 包季 -->
DATE_ADD(NOW(),INTERVAL 3 MONTH)
</if>
<iftest="type ==5"><!-- 包半年 -->
DATE_ADD(NOW(),INTERVAL 6 MONTH)
</if>
<iftest="type ==6"><!-- 包年 -->
DATE_ADD(NOW(),INTERVAL 12 MONTH)
</if>
<iftest="type ==7"><!-- 走运营商产品订购,失效时间为空 -->
null
</if>
FROM `user_order` WHERE `orderId` =#{orderId};
choose(when、otherwise):相当于java中的case when,多条件分支判断
SELECT `user_id`,`platform`,`orderId`,`product_id`,`product_name`,NOW(),NOW(),NOW(),app_business_id,app_group_id FROM `user_order` WHERE 1=1
<choose>
<when test="user_id != null and user_id !=''">
AND user_id = #{user_id}
</when>
<when test="orderId!= null and orderId !=''">
AND orderId= #{orderId}
</when>
<otherwise>
AND product_id is not null
</otherwise>
</choose>
trim(where、set):处理sql拼装
trim可以去掉一些特殊的字符串,prefix代表前缀,prefixOverrdes是要去掉的字符串。
SELECT `user_id`,`platform`,`orderId`,`product_id`,`product_name`,NOW(),NOW(),NOW(),app_business_id,app_group_id FROM `user_order`
<trim prefix="where"prefixOverrdes="and">
<choose>
<when test="user_id != null and user_id !=''">
AND user_id = #{user_id}
</when>
<when test="orderId!= null and orderId !=''">
AND orderId= #{orderId}
</when>
<otherwise>
AND product_id is not null
</otherwise>
</choose>
</trim>
set可以在更新语句中,帮我们去掉多的逗号
<updateid="updateVIP" parameterType="java.util.Map">
UPDATE `user_order_vip` v SET v.`platform`=o.`platform`,v.`orderId`=o.`orderId`,v.`product_id`=o.`product_id`,v.`product_name`=o.`product_name`,
v.`updatetime`=NOW(),
v.starttime= CASE WHEN v.overtime>NOW() THEN v.starttime ELSE NOW() END,
<iftest="type !=null andtype!=''">
v.type = #{type},
</if>
<iftest="updatetime!=nulland updatetime!=''">
v.`updatetime`=#{updatetime}
</if>
</update>
foreach:in语句中使用
<selectid="selectOrders"parameterType="java.lang.String"resultType="com.hzdracom.core.bean.Order">
<!-- SELECT * FROM `user_order` WHERE `orderId`=#{orderId} -->
select *,CASE WHEN (is_unified_product is NULL) then 0 else is_unified_product end isUnifiedProduct from user_order
WHERE orderId IN
<foreach item="order_id" index = "index" collection="orderList" open="(" separator="," close=")">
#{order_id}
</foreach>
</select>
bind 模糊查询时用
<select id="selectFlowForUpdate" resultMap="BaseResultMap"
parameterType="java.lang.String">
<bind name="ENTR_ID" value="'%' + _parameter" />
select
*
from
TC_ENTR_FLOW
where ENTR_ID like #{ENTR_ID}
for update
</select>
阅读全文
0 0
- MyBatis知识盘点【伍】_动态sql
- MyBatis知识盘点【肆】_映射器
- MyBatis知识六 动态SQL语句
- mybatis知识总结(动态sql)
- MyBatis知识盘点【壹】_MyBatis和Hibernate
- MyBatis知识盘点【贰】_MyBatis组件概览
- redis知识盘点【壹】_基础知识
- redis知识盘点【叁】_持久化
- redis知识盘点【伍】_一致性哈希和cluster集群
- MyBatis知识盘点【叁】_mybatis-config.xml配置说明
- redis知识盘点【贰】_五种类型
- redis知识盘点【肆】_主从复制和sentinel哨兵
- mybatis动态SQL语句
- MyBatis动态SQL
- MyBatis 动态SQL
- Mybatis 动态SQL
- MyBatis动态SQL
- MyBatis动态SQL完整版
- JMeter压力测试(三)-多线程组并发测试(多场景混合并发)
- 线程创建方式
- springboot集成mybatis问题
- centos 安装mysql
- adduser in ubuntu 16.04 in sduer
- MyBatis知识盘点【伍】_动态sql
- syslog配置日志转发(imfile)
- 安装 virtualenv 和 virtualenvwrapper 来管理环境
- 关于斐波那契数的JavaScript实现
- 使用ls命令显示目录下文件及文件夹时会先显示不同颜色
- 状态栏修改颜色小总结
- 常见的HTML事件
- Mac安装Tensorflow初体验
- 计数排序