网上购物网站建设公司百度扫一扫识别图片在线
简单介绍:
在之前我们做的学生管理系统的时候,曾经有一个环节是修改学生的数据。我们在修改的时候是必须将student对象的三个属性全部填入信息,然后全部修改才可以,这样会造成一个问题就是在我们明明只需要修改一个属性的时候却要把全部的属性都要修改,就会造成很多的资源浪费。而<set>标签就能帮助我们动态的判断某一个元素是不是为空值,是否需要修改。
使用方法:
<select id="唯一标识" resultType="结果集封装的实体类">
update student
<update>
<if test="判断条件">
修改数据的SQL语句
</if>
<if test="判断条件">
修改数据的SQL语句
</if>
</set>
where id = #{id}
</update>
代码实现:
SQL映射文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Mappers.dynamicSql"><select id="selectByIdOrName" parameterType="student" resultType="student">select * from student where 1=1
# 当id的值不等于null并且id的值不是空字符的时候,就会拼接这个SQL语句<if test="id != null and id != ''">and id = #{id}</if>
# 当name的值不等于null的时候并且name的值不是空字符串的时候,就会拼接这个SQL语句<if test="name != null and name != ''">
# 注意这个地方是使用了一个concat函数将模糊匹配的百分号和参数进行拼接,在使用的时候注意这个地方不要写错and name like concat ('%',#{name},'%')</if></select><select id="selectAll" resultType="student">select * from student;</select>
<!-- 动态SQL中的choose元素-->
<!-- 当查询的条件满足第一个when的时候,就拼接第一个when里面的SQL语句-->
<!-- 当查询的条件满足第二个when的时候,就拼接第二个when里面的SQL语句-->
<!-- 当所有的when都不满足的时候,就拼接otherwise里面的SQL语句-->
<!-- 当有多个when里面的条件都满足的时候,就拼接最靠上的一条SQL语句,并且不会执行其他when里面的语句--><select id="selectStudentByIdAndName" resultType="student" >select * from student where 1=1<choose><when test="name != null and name != ''">and name like concat('%',#{name},'%')</when><when test="id != null and id != ''">and id = #{id}</when><otherwise>and password is not null</otherwise></choose></select>
<!-- 使用<where>来动态的处理where关键字是否添加--><select id="selectByIdAndWhere" resultType="student" parameterType="student">select * from student<where><if test="name != null and name !=''">and name like concat('%',#{name},'%')</if><if test="id != null and id !=''">and id = #{id}</if></where></select>
<!-- 使用set标签简化update的代码和运行效率--><update id="updateBySet" parameterType="student">update student<set><if test="name != null and name != ''">name = #{name},</if><if test="password != null and password != ''">password = #{password},</if></set>where id = #{id}</update>
</mapper>
接口类:
package Mappers;import com.mybatis.POJO.student;import java.util.List;public interface dynamicSql {List<student> selectByIdOrName(student s);List<student> selectStudentByIdAndName(student s);List<student> selectByIdAndWhere(student s);int updateBySet(student s);
}
测试类:
package Mappers;import com.mybatis.POJO.Tools.createSqlSession;
import com.mybatis.POJO.student;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import java.util.List;public class dynamicSqlTest {@Testpublic void selectByIdOrName(){SqlSession sqlSession = new createSqlSession().create();dynamicSql dynamicSql = new createSqlSession().createdynamicSql();student s = new student();s.setId(1);s.setName("张三");List<student> stu = sqlSession.selectList("Mappers.dynamicSql.selectByIdOrName", s);for(student student : stu){System.out.println(student.toString());}}@Testpublic void selectStudentByIdAndName(){dynamicSql dynamicSql = new createSqlSession().createdynamicSql();student s =new student();
// s.setId(1);
// s.setName("张三");for (student student : dynamicSql.selectStudentByIdAndName(s)) {System.out.println(student);}}@Testpublic void selectAll(){SqlSession sqlSession = new createSqlSession().create();dynamicSql dynamicSql = new createSqlSession().createdynamicSql();List<student> list = sqlSession.selectList("Mappers.dynamicSql.selectAll");for(student student : list){System.out.println(student.toString());}}@Testpublic void selectByIdAndWhere(){SqlSession sqlSession = new createSqlSession().create();dynamicSql dynamicSql = new createSqlSession().createdynamicSql();student s = new student();
// s.setId(1);
// s.setName("张三");for (student student : dynamicSql.selectByIdAndWhere(s)) {System.out.println(student.toString());}}
// 测试set标签插入数据的方法@Testpublic void updateBySet(){SqlSession sqlSession = new createSqlSession().create();dynamicSql dynamicSql = new createSqlSession().createdynamicSql();student s = new student();s.setId(4);s.setName("张三");int i = dynamicSql.updateBySet(s);if(i > 0){System.out.println("修改成功!");}}
}
运行结果:
在我们修改数据之前,我们先来记录一下修改前的数据库文件:
接下来,我们先将id等于4位置的name从“大海”修改成为“小海”并且password不做修改:
我们创建了一个student对象,并且只传递了两个参数,一个id用来定位操作的数据,一个name的值用来修改之前的值:
在执行完程序之后,数据库中的值就被顺利的修改了:
那么接下来我们来修改name和password两个的值:
只要配置正确,依然可以正确的修改
注意点:
需要注意的就是我们传入的时候有一个id的值是必须有的,因为通过id才能定位到我们需要修改的行,然后就是SQL语句的拼接和条件的判断。