前言
本章学习MyBatis的动态SQL的相关知识
方法
1.概念
相信大家已经猜到了,所谓的动态SQL实际上就是根据条件的不同动态的执行sql命令!
那么,我们MyBatis提供了相关的支持!
注意:本章起,项目将由maven进行构建!
2.使用if标签构建动态SQL
以前的StudentMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- mapper配置文件的根路径,namespace代表实现类的全路径(包名+类名) -->
<mapper namespace="cn.edu.ccut.mapper.StudentMapper">
<!-- 该处每一个标签代表一个方法(select、insert、delete、update等),
id为方法名,
parameterType为参数类型,
resultType为返回值类型
-->
<select id="doLogin" resultType="boolean">
select * from users where username=#{username} and password=#{password}
</select>
</mapper>
这就意味着这两个参数不可以为null,我们试着传入null
显然是报错的!
也就是说,我们在构建SQL的时候可以判断其是否为null进一步调整执行的SQL
不一定是这种情况,当需求比较复杂需要动态的执行一些SQL的时候,动态SQL就显得尤为重要!
实例:使用if标签构建动态SQL
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- mapper配置文件的根路径,namespace代表实现类的全路径(包名+类名) -->
<mapper namespace="cn.edu.ccut.mapper.StudentMapper">
<!-- 该处每一个标签代表一个方法(select、insert、delete、update等),
id为方法名,
parameterType为参数类型,
resultType为返回值类型
-->
<select id="doLogin" resultType="boolean">
select * from users where 1=1
<if test="username!=null">
and username = #{username}
</if>
<if test="password!=null">
and password = #{password}
</if>
</select>
</mapper>
注意:if标签和我们的JSTL标签类似,但是该标签的值采用OGNL书写方式,变量直接书写即可,条件可取为(and、or等等)
3.使用where标签构建动态SQL
相信大家已经发现了,我在使用if标签的时候,sql后面跟了一句where 1=1,聪明的小伙伴们应该知道此处的奥妙。如果不加的话,会报SQL语法错误。那么我们可以使用where标签避免这种事情!
实例:使用where标签构建动态SQL
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- mapper配置文件的根路径,namespace代表实现类的全路径(包名+类名) -->
<mapper namespace="cn.edu.ccut.mapper.StudentMapper">
<!-- 该处每一个标签代表一个方法(select、insert、delete、update等),
id为方法名,
parameterType为参数类型,
resultType为返回值类型
-->
<select id="doLogin" resultType="boolean">
select * from users
<where>
<if test="username!=null">
and username = #{username}
</if>
<if test="password!=null">
and password = #{password}
</if>
</where>
</select>
</mapper>
我们发现,使用where标签省去了我们添加为SQL添加where关键字的麻烦!
3.使用choose、when、otherwise标签构建动态SQL
和JSTL类似,其代表着if...else的作用
实例:使用choose、when、otherwise标签构建动态SQL
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- mapper配置文件的根路径,namespace代表实现类的全路径(包名+类名) -->
<mapper namespace="cn.edu.ccut.mapper.StudentMapper">
<!-- 该处每一个标签代表一个方法(select、insert、delete、update等),
id为方法名,
parameterType为参数类型,
resultType为返回值类型
-->
<select id="doLogin" resultType="user">
select * from users
<where>
<choose>
<when test="password!=null">
and password = #{password}
</when>
<otherwise>
and password = '1'
</otherwise>
</choose>
</where>
</select>
</mapper>
4.使用set标签构建动态SQL
<update id="doUpdate" parameterType="student">
update student
<set>
id = #{id},
<if test="name !=null and name != ''">
name = #{name},
</if>
<if test="age !=null">
age = #{age},
</if>
<if test="sex !=null and sex != ''">
sex = #{sex},
</if>
<if test="tel !=null and tel != ''">
tel = #{tel},
</if>
<if test="loc !=null and loc != ''">
loc = #{loc},
</if>
</set>
where id = #{id}
</update>
5.使用trim标签构建动态SQL
属性含义:
- prefix:在所在的SQL前加入的字符串
- prefixOverrides:在所在的SQL前去掉的字符串
- suffix:在所在的SQL后加入的字符串
- suffixOverrides:在所在的SQL后去掉的字符串
实例:使用trim标签模拟set标签功能
<update id="doUpdate" parameterType="student">
update student
<trim prefix="set" suffixOverrides=",">
id = #{id},
<if test="name !=null and name != ''">
name = #{name},
</if>
<if test="age !=null">
age = #{age},
</if>
<if test="sex !=null and sex != ''">
sex = #{sex},
</if>
<if test="tel !=null and tel != ''">
tel = #{tel},
</if>
<if test="loc !=null and loc != ''">
loc = #{loc},
</if>
</trim>
where id = #{id}
</update>
6.使用bind标签构建动态SQL
我们知道,sql中存在模糊查询如select * from student where a like ‘%s%’;
由于trim标签在新增%的时候存在空格,故我们使用bind标签修改
语法:<bind name="原来的值" value="修改后的值">
实例:使用bind标签模拟模糊查询
<select id="findByName" resultType="student" parameterType="string">
select * from student
<bind name="name" value="'%'+name+'%'"/>
<where>
name like #{name}
</where>
</select>
7.使用foreach标签构建动态SQL
我们知道,SQL常规查询中有in关键字,实现诸如select * from student where in (1,2,3)的功能,以我们目前掌握的标签很难实现,foreach的出现轻松的帮我们解决了上述难题。
属性含义:
- collection:代表传递进来的集合变量
- item:迭代变量
- open:所在SQL前面添加的字符串
- close:所在SQL后面添加的字符串
- separator:每个变量的分隔符
- index:迭代的脚标
实例:实现批量删除
<delete id="doBantchRemove" parameterType="collection">
delete from STUDENT
<where>
id in
<foreach collection="collection" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</where>
</delete>
8.使用include标签实现动态SQL
属性 refid对应 sql标签的id值,sql标签用来定义公共的串
include标签负责引用,多表联合查询使用较多
实例:使用include标签和sql标签实现 select username,password from users
<select id="findTest">
select <include refid="str"></include> from users
</select>
<sql id="str">
username,password
</sql>
基本的标签就是这么多了,它构成了MyBatis的精华,请大家细心消化!