必须拿下的Mybatis动态SQL

简介: Mybatis动态SQL

1、if标签

<select id="getUser" parameterType="java.lang.Integer" resultType="User">select*from user
<if test="id != null">where id = #{id}</if></select>

这样写如果id为null则查询所有,传了id就按id精准查询,如果有多个参数呢?

<select id="getUser" resultType="User">select*from user where<if test="id != null">            id = #{id}</if><if test="userName != null and userName !=''">and user_name like CONCAT(CONCAT('%',#{userName}),'%')</if></select>

如果 userName等于 null,那么查询语句为 select * from user where id=#{id},但是如果id为空呢?那么查询语句为 select * from user where and user_name like "%#{author}%",这是错误的SQL 语句,如何解决呢?请看下面的 where 标签!

2、where标签

<select id="getUser" resultType="User">select*from user where<where><if test="id != null">                id = #{id}</if><if test="userName != null and userName !=''">and user_name like CONCAT(CONCAT('%',#{userName}),'%')</if><if test="code != null">and code = #{code}</if></where></select>

<where>标签会进行自动判断
如果任何条件都不成立,那么就在sql语句里就不会出现where关键字
如果有任何条件成立,会自动去掉多出来的 and 或者 or。

在查询时我们会遇到多参数问题,那么在update时候也是,这时候可以使用set标签

3、set标签

<update id="updateUser" parameterType="User">update user
<set><if test="userName != null and userName != ''">            user_name = #{userName,jdbcType=VARCHAR},</if><if test="password != null and password !=''">            password = #{password,jdbcType=VARCHAR},</if></set>where id=#{id}</update>

set 标签会动态地在行首插入 set 关键字,并会删掉额外的逗号(这些逗号是在使用条件语句给列赋值时引入的)

4、Choose标签

Mybatis里面没有else标签,有时候,我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose 标签可以解决此类问题

<select id="getUser" resultType="User">select*from user
<where><choose><when test="userName != null and userName !=''">and user_name like CONCAT(CONCAT('%',#{userName}),'%')</when><when test="code != null">and code =#{code}</when><otherwise>and id >1</otherwise></choose></where></select>

其作用是: 提供了任何条件,就进行条件查询,否则就使用id>1这个条件。

5、 foreach标签

  •  collection:指定输入对象中的集合属性
  •  item:每次遍历生成的对象
  •  open:开始遍历时的拼接字符串
  •  close:结束时拼接的字符串
  •  separator:遍历对象之间需要拼接的字符串
select*from user
where id in<foreach collection="list" item="id" open="(" close=")" separator=",">            id=#{id}</foreach>

温馨提示:List对象默认用"list"代替作为键;数组对象有"array"代替作为键;当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list和array将会失效,需要使用keyName。


除了这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:如果User有属性List ids。入参是User对象,那么这个collection = "ids"。如果User有属性IdEntity Ids;其中IdEntity是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id"

6、sql片段

有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用。

比如我们上述查询sql都用的select * ,实际工作中不推荐使用select *,写个sql片段

<sql id="Base_Column_List">        id, user_name, code
</sql>

看看如何使用它

select<include refid="Base_Column_List"/>from user
where id in<foreach collection="list" item="id" open="(" close=")" separator=",">            id=#{id}</foreach>

注意:

  • 最好基于 单表来定义 sql 片段,提高片段的可重用性
  • 在 sql 片段中不要包括 where

7、bind标签

bind标签就像是再做一次字符串拼接,方便后续使用

<select id="listUser" resultType="User"><bind name="likename" value="'%' + userName + '%'"/>select*from  user where user_name like #{likename}</select>
相关文章
|
3天前
|
SQL Java 数据库连接
深入探索MyBatis Dynamic SQL:发展、原理与应用
深入探索MyBatis Dynamic SQL:发展、原理与应用
|
5天前
|
SQL XML 数据库
后端数据库开发高级之通过在xml文件中映射实现动态SQL
后端数据库开发高级之通过在xml文件中映射实现动态SQL
14 3
|
1天前
|
SQL 缓存 Java
Java框架之MyBatis 07-动态SQL-缓存机制-逆向工程-分页插件
Java框架之MyBatis 07-动态SQL-缓存机制-逆向工程-分页插件
|
1天前
|
SQL Java 数据库连接
MyBatis动态SQL
MyBatis动态SQL
6 0
|
4天前
|
SQL Java 数据库连接
Mybatis日志SQL解析
Mybatis日志SQL解析
6 0
|
5天前
|
JSON 前端开发 数据格式
MyBatis-Plus动态分页查询
MyBatis-Plus动态分页查询
8 0
|
5天前
|
SQL Java 数据库连接
Mybatis动态SQL语句总结
Mybatis动态SQL语句总结
|
7月前
|
SQL 安全 Java
MyBatis映射文件深入--动态sql
MyBatis映射文件深入--动态sql
59 0
|
7月前
|
SQL Java 数据库连接
MyBatis 动态 SQL
MyBatis 动态 SQL
|
24天前
|
SQL XML Java
MyBatis第四课动态SQL
MyBatis第四课动态SQL