动态SQL
动态SQL根据条件动态的对SQL进行拼接组装
执行原理:使用OGNL从SQL参数中计算表达式的值,根据表达式的值动态的拼接SQL,以此完成动态SQL功能
标签:如;if标签,where、trim、foreach等标签
if标签:
通过student中的条件查询对象或对象结果
通过id查询:select * from student where SID= ?
通过name查询:select * from student where Name= ?
通过id和name查血:select * from student where id=? and name =?
全表查询:select * from studentif标签使用示例:
<select id="selectStudent" parameterType="student" resultType="student">
select * from student
where 1=1
<if test="id != null and id != 0" >
and SID = #{id}
</if>
<if test="name != null" >
and Sname = #{name}
</if>
</select>
if标签通常是根据条件做判断,一般作为where子句的一部分
if表达式判断是否传递参数在其后面有一个test属性,该属性必填,为true或false,
为true时,会拼接该if中SQL片段,为false,则不拼接SQL子句
该test的判断是通过OGNL表达式判断
传递name和id属性:
Student213 student213 = new Student213();
student213.setId(1);
student213.setName("tulun");
studentMapper213.selectStudent(student213);
执行结果:
只传递id:
Student213 student213 = new Student213();
student213.setId(1);
studentMapper213.selectStudent(student213);
执行结果:
不传递参数:
Student213 student213 = new Student213();
studentMapper213.selectStudent(student213);
执行结果:
where标签
where标签作用:如果该标签包含的元素有结果返回,就插入where,如果where后面的字符串包含and 或者or开头的,将其提出。
<select id="selectStudent" parameterType="student" resultType="student">
select * from student
<where>
<if test="id != null and id != 0">
and SID = #{id}
</if>
<if test="name != null">
and Sname = #{name}
</if>
</where>
</select>
where表达式一般和if表达式一块使用,如果条件一个都不满足,则不拼接where条件,如果有一个或者是多个条件成立。where会自动拼接在SQL中,并且紧随where之后的and和or去掉。
不传递参数:
只传递一个参数:
传递两个参数:
set标签
set标签作用:如果标签包含的元素有返回值,就插入一个set,如果set后面的SQL中以逗号结尾的,将最后的逗号去掉。
<update id="updateNameById">
update student
<set>
<if test="name != null" >
Sname = #{name},
</if>
</set>
where SID = #{id}
</update>
Update table_name set id=XXX , name=XX,sex=XXX where id =XX;
trim标签
去掉SQL中多余的and关键字、逗号,或者给SQL拼接上where或者set关键字。
<select id="selectStudent" parameterType="student" resultType="student">
select * from student
<trim prefix="where" prefixOverrides="and">
<if test="id != null and id != 0">
and SID = #{id}
</if>
<if test="name != null">
and Sname = #{name}
</if>
</trim>
</select>
<update id="updateNameById">
update student
<trim prefix="set" suffixOverrides=",">
<if test="name != null" >
Sname = #{name},
</if>
</trim>
where SID = #{id}
</update>
foreach标签
批量操作标签
select * from student where id in(1,2,3,4,5);
insert into student (id,name) values(id1,name1),(id2,name2),(id3,name3);
<!--
foreach表达式
collection属性:必填
当参数为数组时,collection属性是array
当参数是list时,collection属性是list
当参数是map时,collection属性属性是map
item属性:变量名,值是从集合中迭代遍历的每一个值
open属性:循环开始的字符串
close属性:循环结束的字符串
separator属性:每次循环的分隔符
index属性:索引的属性名,当集合是数组时可以通过index来获取数据,当迭代是map时,index表示的是key
select * from student where id in(1,2,3,4,5);
-->
<select id="selectStudentsByIds" resultType="student">
select * from student where SID in
<foreach collection="list" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</select>
模糊匹配
字符串模糊匹配:使用 like 关键字
%:表示一个或者是多个字符
_:表示一个字符
需求:查询student表中用户名含L的所有用户
select * from student where Sname like %L%;
写法1:直接将通配符拼接在参数上
selectStudentByName("%L%")
写法2:使用MySQL提供的concat函数
MySQL还提供字符串拼接的方法:concat(x1,x2) ->x1x2
<select id="selectStudentByName" parameterType="string" resultType="student">
select * from student where Sname like concat('%',concat(#{name},'%'))
</select>
写法3:使用bind标签处理
<select id="selectStudentByName" resultType="student">
<bind name="namesql" value="'%'+name+'%'"/>
select * from student where Sname like #{namesql}
</select>
注意:bind表达式中value属性处理中借助OGNL表达式,对应的参数需要具有getter方法