动态sql
student表
student实体类
public class Student{
private Integer sid;
private String sname;
private String sex;
private Integer eid;
}
1.标签
1).if标签
<!--if标签:test满足布尔表达式满足就拼接里面的-->
<if test=""></if>
2).where标签
<!--
where节点的作用:
1.生成where关键字
2.会自动去掉where关键字之后的and/or关键字
一般if标签放在where标签中
-->
<where></where>
3).trim标签
<!--
prefix:指定一个字符串,拼接到条件的前面
prefixOverrides: 指定一个字符串,在条件的前面去掉该字符串,如果没有该值,就不管,可以有多个:and|or|.. 只能满足一个
suffix: 指定一个字符串,拼接到条件的后面
suffixOverrides:指定一个字符串,在条件的后面去掉该字符串,如果没有该值,就不管,可以有多个:and|or|.. 只能满足一个
-->
<trim prefix="" prefixOverrides="" suffix="" suffixOverrides=""></trim>
4).foreach标签
<!-- foreach
collection:指定循环的元素,如果传入的参数是数组写array,集合写list
item:循环变量
open:循环开始前拼接的字符串
close:循环结束后拼接的字符串
index:循环的索引
separator:循环过程中两元素之间拼接的字符串
-->
<foreach collection="" item="" open="" close="" index="" separator=""></foreach>
5).sql代码片段
<!--id:唯一的标识-->
<sql id=""></sql>
<!--配合-->
<!--refid:参考sql的id-->
<include refid=""/>
2.条件查询使用if标签
<!--Student.xml-->
<!-- 查询: if:标签 有值就添加条件,没值就拼接 -->
<select id="select1" resultType="Student"
parameterType="Student">
select * from student where 1=1
<if test="sname!=null and sname!=''">
and sname like '%' #{sname} '%'
</if>
<if test="sex!=null and sex!=''">
and sex=#{sex}
</if>
<if test="eid!=null">
<!-- <是特殊的字符需要转译 -->
<![CDATA[
and eid <= #{eid}
]]>
</if>
</select>
3.条件查询使用where标签
<!--
where节点的作用:
1.生成where关键字
2.会自动去掉where关键字之后的and/or关键字
-->
<select id="select2" resultType="Student"
parameterType="Student">
select * from student
<where>
<if test="sname!=null and sname!=''">
and sname like '%' #{sname} '%'
</if>
<if test="sex!=null and sex!=''">
and sex=#{sex}
</if>
<if test="eid!=null">
<!-- <是特殊的字符需要转译 -->
<![CDATA[
and eid <= #{eid}
]]>
</if>
</where>
</select>
4.条件查询使用trim标签
<!--
prefix:指定一个字符串,拼接到条件的前面
prefixOverrides: 指定一个字符串,在条件的前面去掉该字符串,如果没有该值,就不管
suffix: 指定一个字符串,拼接到条件的后面
suffixOverrides:指定一个字符串,在条件的后面去掉该字符串,如果没有该值,就不管
-->
<select id="select3" resultType="Student" parameterType="Student">
select * from student
<trim prefix="where" prefixOverrides="and">
<if test="sname!=null and sname!=''">
and sname like '%' #{sname} '%'
</if>
<if test="sex!=null and sex!=''">
and sex=#{sex}
</if>
<if test="eid!=null">
<!-- <是特殊的字符需要转译 -->
<![CDATA[
and eid <= #{eid}
]]>
</if>
</trim>
</select>
5.修改有值的
<!--id需要传
trim用于修改,有值就修改,没值就不修改
如果eid没有值就会多一个逗号需要处理
-->
<update id="update1" parameterType="Student">
update student
set
<trim suffixOverrides=",">
<if test="sname!=null and sname!=''">
sname=#{sname},
</if>
<if test="sex!=null and sex!=''">
sex=#{sex},
</if>
<if test="eid!=null and eid!=''">
eid=#{eid}
</if>
</trim>
where sid=#{sid}
</update>
6.添加有值的
<!-- 添加:有值的添加,没值的不添加,前提没有值的列的数据库没有设置非空约束,否则会报错
insert into student(x,x,x)value(x,x,x)
-->
<insert id="insert1" parameterType="Student">
insert into student
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="sname!=null and sname!=''">
sname,
</if>
<if test="sex!=null and sex!=''">
sex,
</if>
<if test="eid!=null">
eid,
</if>
</trim>
value
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="sname!=null and sname!=''">
#{sname},
</if>
<if test="sex!=null and sex!=''">
#{sex},
</if>
<if test="eid!=null">
#{eid},
</if>
</trim>
</insert>
7.批量删除
<!-- 批量删除,根据id删除
如果传入的是集合或者数组,不用写参数类型(parameterType)
delete student where sid in(x,x,x)
-->
<delete id="delete1">
delete from student
where
sid in
<foreach collection="array" open="(" close=")" separator="," item="sid">
#{sid}
</foreach>
</delete>
8.批量添加
<!-- 批量添加
insert into student(sname,sex,eid)value(x,x,x),(x,x,x)
-->
<insert id="insert2">
insert into student(sname,sex,eid)
value
<foreach collection="list" item="s" separator=",">
(#{s.sname},#{s.sex},#{s.eid})
</foreach>
</insert>
以下等同于上方
<!-- 批量添加
insert into student(sname,sex,eid)value(x,x,x),(x,x,x)
-->
<sql id="baseSql">insert into student(sname,sex,eid) value</sql>
<insert id="insert2">
<include refid="baseSql"></include>
<foreach collection="list" item="s" separator=",">
(#{s.sname},#{s.sex},#{s.eid})
</foreach>
</insert>