MyBatis实现动态SQL的实现方法
MyBatis 最强大的特性之一就是它的动态语句功能。如果您以前有使用JDBC或者类似框架的 经历,您就会明白把SQL语句条件连接在一起是多么的痛苦,要确保不能忘记空格或者不要在 columns列后面省略一个逗号等。动态语句能够完全解决掉这些痛苦。
尽管与动态SQL一起工作不是在开一个party,但是MyBatis确实能通过在任何映射SQL语句中 使用强大的动态SQL来改进这些状况。
if 元素
if元素条件判断,动态 SQL 最常做的事就是有条件地包括 where 子句。例如:
<select id=”findActiveBlogWithTitleLike” parameterType=”Blog” resultType=”Blog”> SELECT * FROM BLOG WHERE state = ‘ACTIVE' <if test=”title != null”> AND title like #{title} </if> </select>
where元素
where元素知道插入“where”如果它包含的标签中有内容返回的话。此外,如果返回的内容 以“AND” 或者 “OR”开头,它会把“AND” 或者 “OR”去掉。
<select id="findStudentList" parameterType="hashmap" resultMap="studentInfo"> select * from t_student <where> <if test="name!=null"> and name=#{name} </if> <if test="age!=null"> and age=#{age} </if> </where> </select>
choose元素
有时候我们不想应用所有的条件,而是想从多个选项中选择一个。与 java 中的 switch 语句 相似,MyBatis 提供了一个 choose 元素。
when元素
当when里面的条件成立时,执行when标签内的语句
<select id="findStudentList" parameterType="hashmap" resultMap="studentInfo"> select * from t_student <where> <choose> <when test="name!=null"> and name=#{name} </when> <when test="age!=null"> and age=#{age} </when> </choose> </where> </select>
otherwise元素
当所有when都不成立了,执行otherwise
<select id="findStudentList" parameterType="hashmap" resultMap="studentInfo"> select * from t_student <where> <choose> <when test="name!=null"> and name=#{name} </when> <when test="age!=null"> and age=#{age} </when> <otherwise> and name='jim' </otherwise> </choose> </where> </select>
trim元素
如果 where 元素的行为并没有完全按您想象的那样,您还可以使用 trim 元素来自定义。
trim内的if有成立的就添加一个前缀用prefix=""定义,没有就不添加。
trim元素也可以去掉where后面指定的关键字and或者or等等,用prefixOverrides=""定义
<select id="findStudentList" parameterType="hashmap" resultMap="studentInfo"> select * from t_student <trim prefix="where" prefixOverrides="and"> <if test="name!=null"> and name=#{name} </if> <if test="age!=null"> and age=#{age} </if> </trim> </select>
set元素
在动态update语句里相似的解决方式叫做set,这个set元素能够动态地更新列。
set 元素将动态的配置set关键字,也用来剔除追加到条件末尾的任何不相关的逗号。
<update id="updateStudent" parameterType="Student"> update t_student <set> <if test="name!=null"> name=#{name}, </if> <if test="age!=null"> age=#{age}, </if> </set> where id=#{id} </update>
当然了,聪明的你肯定想知道等同的 trim 元素该怎么写吧,它就像这样 :
<update id="updateStudent" parameterType="Student"> update t_student <trim prefix="set" prefixOverrides=","> <if test="name!=null"> name=#{name}, </if> <if test="age!=null"> age=#{age}, </if> </trim> where id=#{id} </update>
注意这种情况,我们剔除了一个后缀, 同时追加了一个前缀 。
Foreach 元素
另一个动态 SQL 经常使用到的功能是集合迭代,通常用在 in条件句
foreach 元素非常强大,允许您指定一个集合,申明能够用在元素体内的项和索引变量。也 允许您指定开始和结束的字符,也可以加入一个分隔符到迭代器之间。这个元素的聪明之处在于 它不会意外地追加额外的分隔符。
<select id="findStudentByAge" resultMap="studentInfo"> select * from t_student where age in <foreach collection="list" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>
测试方法如下:
public void findStudentByAge() { SqlSession sqlSession = null; try { sqlSession = MyBatisUtil.getsqlSession(); StudentDao studentDao = sqlSession.getMapper(StudentDao.class); List<Integer> list= new ArrayList<Integer>(); list.add(21); list.add(23); List<Student> liststudent =studentDao.findStudentByAge(list); System.out.println(liststudent); } catch (Exception e) { e.printStackTrace(); } }
输出的sql结果:select * from t_student where age in (item,item),显示age为21、23的学生信息。
Settings 元素
Setting 元素下是些非常重要的设置选项,用于设置和改变 MyBatis 运行中的行为。下面的 表格列出了 Setting 元素支持的属性、默认值及其功能。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vuQc9cUw-1576746278488)(E:\javaEE笔记\img\QQ浏览器截图20191218153217.png)]
完整配置例子:
<settings> <setting name="cacheEnabled" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> <setting name="multipleResultSetsEnabled" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="useGeneratedKeys" value="false"/> <setting name="enhancementEnabled" value="false"/> <setting name="defaultExecutorType" value="SIMPLE"/> <setting name="defaultStatementTimeout" value="25000"/> </settings>
XML 中的特殊字符
如果 MyBatis 使用 XML 配置,那不可避免地会遇到一些对 XML 来说是特殊的字符。如小于号 “<”,因为 XML 解析器会认为是一个新元素的开始,因此要进行转义。这里有两个方法:
1 使用转义实体
下面是五个在 XML 文档中预定义好的转义实体 :
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
<select id="findStudentList" parameterType="hashmap" resultMap="studentInfo"> select * from t_student where age $lt; 23 </select>
2 使用 CDATA 部件
一个 CDATA 部件以"“标记结束。在”"之间 的特殊字符的意义都不起作用,而转变为普通字符串内容。
一般地,在 MyBatis 的 XML 映射语句配置文件中,如果 SQL 语句有特殊字符,那么使用 CDTA 部件括起来,如:
<select id="findStudentList" parameterType="hashmap" resultMap="studentInfo"> <![CDATA[ select * from t_student where age = 23 ]]> </select>
而在动态 SQL 各元素的测试语句中,在元素的属性中不能再嵌套其它元素或包含 CDATA 部 件,因此只能使用转义实体, 如:
<select id="selectAuthor_use_where" parameterType="Blog" resultType="Author"> select * from author <where> <if test="authorId != null and authorId >= 1 and authorId <= 5"> id = #{authorId} </if> </where> </select>
以上就是本文的全部内容,希望对大家的学习有所帮助
您可能感兴趣的文章:
- ## MyBatis持久层框架,实现数据访问,抽象方法多参数的处理,关于别名,MyBatis中的动态SQL,resultMap
- 自己动手实现mybatis动态sql的方法
- MyBatis中动态sql的实现方法示例
- oracle+mybatis 使用动态Sql在要insert的字段不确定的情况下实现批量insert
- MyBatis中用于实现动态SQL
- Mybatis4 之Mybatis动态sql的实现代码
- oracle+mybatis 使用动态Sql当插入字段不确定的情况下实现批量insert
- mybatis 插件: 打印 sql 及其执行时间实现方法
- [Mybatis] - 动态SQL中参数类型不同 , 称呼的方法不同
- [转]Linq to sql 实现多条件的动态查询(方法二)
- hibernate仿mybatis动态sql实现
- 在Oracle存储过程中实现类Mybatis的动态sql
- MyBatis的动态SQL语句实现
- mybatis高级版 利用动态sql片段加入 foreach 循环拼接 实现 单表批量查询、批量修改、批量删除、批量添加。
- Spring+Mybatis实现动态SQL查询
- Linq to sql 实现多条件的动态查询(方法一)
- mybatis实战之路,疯狂的数据库操作框架、动态sql实现CRUD及带条件的增CRUD
- JAVAWEB开发之mybatis详解(一)——mybatis的入门(实现增删改查操作)、自定义别名、抽取代码块以及动态SQL的使用
- mybatis3通过provider注解结合动态sql实现增删改查,不再依赖xml成为可能
- SpringBoot入门-21(springboot集成mybatis注解形式增删查改properties配置,利用@Provider实现动态SQL)