山西省煤炭基本建设局网站,寺庙网站开发策划书,电子商务网站建设课外实训,视频制作的详细步骤简单介绍#xff1a;
在之前我们做的学生管理系统的时候#xff0c;曾经有一个环节是修改学生的数据。我们在修改的时候是必须将student对象的三个属性全部填入信息#xff0c;然后全部修改才可以#xff0c;这样会造成一个问题就是在我们明明只需要修改一个属性的时候却要…简单介绍
在之前我们做的学生管理系统的时候曾经有一个环节是修改学生的数据。我们在修改的时候是必须将student对象的三个属性全部填入信息然后全部修改才可以这样会造成一个问题就是在我们明明只需要修改一个属性的时候却要把全部的属性都要修改就会造成很多的资源浪费。而set标签就能帮助我们动态的判断某一个元素是不是为空值是否需要修改。
使用方法
select id唯一标识 resultType结果集封装的实体类 update student update if test判断条件 修改数据的SQL语句 /if if test判断条件 修改数据的SQL语句 /if /set where id #{id}
/update
代码实现
SQL映射文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespaceMappers.dynamicSqlselect idselectByIdOrName parameterTypestudent resultTypestudentselect * from student where 11
# 当id的值不等于null并且id的值不是空字符的时候就会拼接这个SQL语句if testid ! null and id ! and id #{id}/if
# 当name的值不等于null的时候并且name的值不是空字符串的时候就会拼接这个SQL语句if testname ! null and name !
# 注意这个地方是使用了一个concat函数将模糊匹配的百分号和参数进行拼接在使用的时候注意这个地方不要写错and name like concat (%,#{name},%)/if/selectselect idselectAll resultTypestudentselect * from student;/select
!-- 动态SQL中的choose元素--
!-- 当查询的条件满足第一个when的时候就拼接第一个when里面的SQL语句--
!-- 当查询的条件满足第二个when的时候就拼接第二个when里面的SQL语句--
!-- 当所有的when都不满足的时候就拼接otherwise里面的SQL语句--
!-- 当有多个when里面的条件都满足的时候就拼接最靠上的一条SQL语句并且不会执行其他when里面的语句--select idselectStudentByIdAndName resultTypestudent select * from student where 11choosewhen testname ! null and name ! and name like concat(%,#{name},%)/whenwhen testid ! null and id ! and id #{id}/whenotherwiseand password is not null/otherwise/choose/select
!-- 使用where来动态的处理where关键字是否添加--select idselectByIdAndWhere resultTypestudent parameterTypestudentselect * from studentwhereif testname ! null and name !and name like concat(%,#{name},%)/ifif testid ! null and id !and id #{id}/if/where/select
!-- 使用set标签简化update的代码和运行效率--update idupdateBySet parameterTypestudentupdate studentsetif testname ! null and name ! name #{name},/ifif testpassword ! null and password ! password #{password},/if/setwhere id #{id}/update
/mapper
接口类
package Mappers;import com.mybatis.POJO.student;import java.util.List;public interface dynamicSql {Liststudent selectByIdOrName(student s);Liststudent selectStudentByIdAndName(student s);Liststudent 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(张三);Liststudent 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();Liststudent 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语句的拼接和条件的判断。