博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
3.3.10 动态SQL
阅读量:4309 次
发布时间:2019-06-06

本文共 2936 字,大约阅读时间需要 9 分钟。

 

十、动态SQL

根据条件的不同, SQL 语句也会随之动态的改变. MyBatis 中,提供了一组标签用于实现动态 SQL.

1. <if>

用于进行条件判断, test 属性用于指定判断条件. 为了拼接条件, 在 SQL 语句后强行添加 1=1 的恒成立条件.

<select id="sel" resultType="user">

select * from t_user where 1=1

<if test="username != null and username != ''">

and username=#{

username}

</if>

<if test="password != null and password != ''">

and password=#{

password}

</if>

</select>

 

2. <where>

用于管理 where 子句. 有如下功能:

(1) 如果没有条件, 不会生成 where 关键字

(2) 如果有条件, 会自动添加 where 关键字

(3) 如果第一个条件中有 and, 去除之

<select id="sel" resultType="user">

select * from t_user

<where>

<if test="username != null and username != ''">

and username=#{

username}

</if>

<if test="password != null and password != ''">

and password=#{

password}

</if>

</where>

</select>

 

3. <choose><when><otherwise>

这是一套标签, 功能类似于 switch...case...

<select id="sel" resultType="user">

select * from t_user

<where>

<choose>

<when test="username != null and username != ''">

and username = #{

username}

</when>

<when test="password != null and password != ''">

and password = #{

password}

</when>

<otherwise>

and 1=1

</otherwise>

</choose>

</where>

</select>

 

4. <set>

用于维护 update 语句中的 set 子句. 功能如下:

(1) 满足条件时, 会自动添加 set 关键字

(2) 会去除 set 子句中多余的逗号

(3) 不满足条件时, 不会生成 set 关键字

int updUser(User user);

<update id="updUser" parameterType="user">

update t_user

<set>

id=#{

id}, <!-- 防止所有条件不成立时的语法错误 -->

<if test="username != null and username != ''">

username=#{

username},

</if>

<if test="password != null and password != ''">

password=#{

password},

</if>

</set>

where id=#{

id}

</update>

 

 

5. <trim>

用于在前后添加或删除一些内容

(1) prefix, 在前面添加内容

(2) prefixOverrides, 从前面去除内容

(3) suffix, 向后面添加内容

(4) suffixOverrides, 从后面去除内容

<update id="updUser" parameterType="user">

update t_user

<!--

prefix: 前缀, 向前面添加内容

prefixOverrides: 从前面删除内容

suffix: 后缀, 向后面添加内容

suffixOverrides: 从后面删除内容

-->

<trim prefix="set" prefixOverrides="user" suffix="hahaha"

suffixOverrides=",">

username=#{

username},

</trim>

where id=#{

id}

</update>

 

6. <bind>

用于对数据进行再加工, 用于模糊查询

<select id="sel" resultType="user">

select * from t_user

<where>

<if test="username!=null and username!=''">

<bind name="username" value="'%' + username + '%'"/>

and username like #{

username}

</if>

</where>

</select>

 

7. <foreach>

用于在 SQL 语句中遍历集合参数, 在 in 查询中使用

(1) collection: 待遍历的集合

(2) open: 设置开始符号

(3) item: 迭代变量

(4) separator: 项目分隔符

(5) close: 设置结束符号

<select id="selIn" parameterType="list" resultType="user">

select * from t_user where id in

<foreach collection="list" open="(" separator="," close=")"

item="item">

#{

item}

</foreach>

</select>

 

List<User> selIn(@Param("list") List<Integer> list);

 

8. <sql><include>

<sql>于提取 SQL 语句, <include>于引 SQL 语句

<sql id="mySql">

id, username, password

</sql>

 

<select id="selIn" parameterType="list" resultType="user">

select

<include refid="mySql"/>

from t_user where id in

<foreach collection="list" open="(" separator="," close=")"

item="item">

#{

item}

</foreach>

</select>

转载于:https://www.cnblogs.com/kendyho/p/10847940.html

你可能感兴趣的文章
sdc时序约束
查看>>
Xilinx Jtag Access/svf文件/BSCANE2
查看>>
NoC片上网络
查看>>
开源SoC整理
查看>>
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
已知子网掩码,确定ip地址范围
查看>>
判断时间或者数字是否连续
查看>>
docker-daemon.json各配置详解
查看>>
Docker(一)使用阿里云容器镜像服务
查看>>
Docker(三) 构建镜像
查看>>
FFmpeg 是如何实现多态的?
查看>>
FFmpeg 源码分析 - avcodec_send_packet 和 avcodec_receive_frame
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>
Android 面试题整理总结(二)Java 集合
查看>>
学习笔记_vnpy实战培训day02
查看>>
学习笔记_vnpy实战培训day03
查看>>
VNPY- VnTrader基本使用
查看>>