MyBatis的入门笔记(二)
- 2018年11月14日
动态SQL:
如数据库中有这样一张学生表,下面将从这张表的增删改查说起动态SQL
1.动态SQL:如果+其中语句
根据姓名和年龄来查询数据,如果名称为空,那么将只根据年龄来查询,反之根据名称来查询。
如果不使用动态SQL我们查询所有数据,或者根据姓名和年龄来查询,如果名称为空,查询的结果也就为空。
这样用到动态SQL来查询的话,在SQL中判断是否传入,如果标签来判断要比JDBC写SQL拼接要简单很多。
<select id="selectgetif" resultType="Student">
select * from student
<!--<where>
<if test="name != null">
name=#{name}
</if>
<if test="age != 0">
and age=#{age}
</if>
</where>-->
<!--动态sql trim语句-->
<trim prefix="where" prefixOverrides="and | or">
<!--<if test="name != null">
and name=#{name}
</if>
<if test="age != 0">
and age=#{age}
</if>-->
<!--引入sql片段,如果refid 指定的不在本文件中,那么需要在前面加上 namespace-->
<include refid="studentifsql"></include>
</trim>
</select>
这个”这样如果名称不传的话,SQL就是这样的:
反正也一样,值得注意的地方就是”这个地方标签,标签会知道如果它包含的标签中有返回值得话,就插入那里,如果标签的返回内容以与和或开头,MyBatis的会自动剔除掉。
2.动态SQL:如果+设定语句
同理,上面的对于查询SQL语句包含其中关键字,如果在进行更新操作的时候,含有设置关键字,含有设置关键字,我们该怎么处理。
现在这里根据ID更新表中数据:
<!--动态sql if+set判断更新语句-->
<update id="updateif">
update student
<!--<set>-->
<!--<if test="name !=''">-->
<!--name = #{name},-->
<!--</if>-->
<!--<if test="age !=0">-->
<!--age = #{age}-->
<!--</if>-->
<!--</set>-->
<trim prefix="set" prefixOverrides=",">
<if test="name !=''">
name = #{name},
</if>
<if test="age !=0">
age = #{age}
</if>
</trim>
where id=#{id}
</update>
如果名称为空,也就是没有传值,
sql就是这样的:更新学生SET年龄=?id =?“
3.动态SQL:选择(当,否则)语句
有时候我们我们不想用到所有的查询条件,只想选择其中一个,查询条件有一个满足即可,这个时候就可以使用选择标签可以解决此类问题,类似于Java的的开关语句
<!--动态sql:choose(when,otherwise)语句意思就是只能以一种作为查询条件-->
<select id="selectgetchoose" resultType="Student">
select * from student
<where>
<choose>
<when test="id != 0">
id=#{id}
</when>
<when test="name != null">
and name=#{name}
</when>
<!--相当于else-->
<otherwise>
and age=#{age}
</otherwise>
</choose>
</where>
</select>
这里如果只传入了名
那么sql语句就是这种:select * from student WHERE name =?
这里的XML中选择查询条件,也就是这里总共写了三个条件,有ID,姓名,年龄
如果ID为空,那么看名字是否为空,如果不为空,那么语句为上面那种,
如果名称为空,sql语句就是:select * from student WHERE age =?
4.动态SQL:修剪语句
修剪标记是一个格式化的标记,可以以完成设定或者是其中标记的功能
a.prefix:前缀:其中或者集......
b.prefxoverride:去掉第一个和或者或(组就是去掉后面的)
键入代码:
5.动态SQL:SQL片段
有时候某些sql语句是一样的,用的特别多,就比如查询时用的select * from等等,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用。
比如:上面的地方语句判断就可以将代码抽取出来,
<!--定义sql片段-->
<sql id="studentifsql">
<if test="name != null">
and name=#{name}
</if>
<if test="age != 0">
and age=#{age}
</if>
</sql>
在引入SQL
<!--引入sql片段,如果refid 指定的不在本文件中,那么需要在前面加上 namespace-->
<include refid="studentifsql"></include>
注意:在SQL片段中不要包含其中
6.动态SQL:的foreach语句
需求:我们需要查询表中的ID分别为1,2,3的学生
Sql语句包括:select * from student,其中id = 1或id = 2或id = 3
或者:从学生中选择*,其中id为(1,2,3)
①。首先需要新建一个StudentVo类,里面封装一个列表<Integer> id的属性
然后用来自学生的foreach来改写select *,其中id = 1或id = 2或id = 3
<!--动态sql之foreach,查询多个id的Student-->
<select id="selectids" resultType="Student">
select * from student
<where>
<!--
collection:指定输入对象中的集合属性
item:每次遍历生成对象
open:开始遍历时的拼接字符串
close:结束时拼接的字符串
separator:遍历对象之间需要拼接的字符串
select * from student where id=1 or id=2 or id=3
-->
<foreach collection="id" item="id" open="(" close=")" separator="or">
id=#{id}
</foreach>
</where>
</select>
测试类:
控制台打印:
Log4j中的日志输出SQL为:
同理:用的foreach来改写
select * from student where where in(1,2,3)也是一样的