学校网站建设协议模板,公司要做网站去哪里,客户管理系统admin,如何汇报网站建设3. Mybatis动态SQL
3.1 什么是动态SQL
在页面原型中#xff0c;列表上方的条件是动态的#xff0c;是可以不传递的#xff0c;也可以只传递其中的1个或者2个或者全部。 而在我们刚才编写的SQL语句中#xff0c;我们会看到#xff0c;我们将三个条件直接写死了。 如果页面…3. Mybatis动态SQL
3.1 什么是动态SQL
在页面原型中列表上方的条件是动态的是可以不传递的也可以只传递其中的1个或者2个或者全部。 而在我们刚才编写的SQL语句中我们会看到我们将三个条件直接写死了。 如果页面只传递了参数姓名name 字段其他两个字段 性别 和 入职时间没有传递那么这两个参数的值就是null。
此时执行的SQL语句为 这个查询结果是不正确的。正确的做法应该是传递了参数再组装这个查询条件如果没有传递参数就不应该组装这个查询条件。 比如如果姓名输入了张, 对应的SQL为:
select * from emp where name like %张% order by update_time desc; 如果姓名输入了张,性别选择了男则对应的SQL为:
select * from emp where name like %张% and gender 1 order by update_time desc; SQL语句会随着用户的输入或外部条件的变化而变化我们称为动态SQL。 在Mybatis中提供了很多实现动态SQL的标签我们学习Mybatis中的动态SQL就是掌握这些动态SQL标签。 3.2 动态SQL-if
if用于判断条件是否成立。使用test属性进行条件判断如果条件为true则拼接SQL。
if test条件表达式要拼接的sql语句
/if
接下来我们就通过if标签来改造之前条件查询的案例。
3.2.1 条件查询
示例把SQL语句改造为动态SQL方式 原有的SQL语句
select idlist resultTypecom.itheima.pojo.Empselect * from empwhere name like concat(%,#{name},%)and gender #{gender}and entrydate between #{begin} and #{end}order by update_time desc
/select 动态SQL语句
select idlist resultTypecom.itheima.pojo.Empselect * from empwhereif testname ! nullname like concat(%,#{name},%)/ifif testgender ! nulland gender #{gender}/ifif testbegin ! null and end ! nulland entrydate between #{begin} and #{end}/iforder by update_time desc
/select
测试方法
Test
public void testList(){//性别数据为null、开始时间和结束时间也为nullListEmp list empMapper.list(张, null, null, null);for(Emp emp : list){System.out.println(emp);}
} 执行的SQL语句 下面呢我们修改测试方法中的代码再次进行测试观察执行情况
Test
public void testList(){//姓名为nullListEmp list empMapper.list(null, (short)1, null, null);for(Emp emp : list){System.out.println(emp);}
} 执行结果 再次修改测试方法中的代码再次进行测试
Test
public void testList(){//传递的数据全部为nullListEmp list empMapper.list(null, null, null, null);for(Emp emp : list){System.out.println(emp);}
}
执行的SQL语句 以上问题的解决方案使用where标签代替SQL语句中的where关键字 where只会在子元素有内容的情况下才插入where子句而且会自动去除子句的开头的AND或OR
select idlist resultTypecom.itheima.pojo.Empselect * from empwhere!-- if做为where标签的子元素 --if testname ! nulland name like concat(%,#{name},%)/ifif testgender ! nulland gender #{gender}/ifif testbegin ! null and end ! nulland entrydate between #{begin} and #{end}/if/whereorder by update_time desc
/select
测试方法
Test
public void testList(){//只有性别ListEmp list empMapper.list(null, (short)1, null, null);for(Emp emp : list){System.out.println(emp);}
} 执行的SQL语句 3.2.2 更新员工
案例完善更新员工功能修改为动态更新员工数据信息 动态更新员工信息如果更新时传递有值则更新如果更新时没有传递值则不更新 解决方案动态SQL
修改Mapper接口
Mapper
public interface EmpMapper {//删除Update注解编写的SQL语句//update操作的SQL语句编写在Mapper映射文件中public void update(Emp emp);
}
修改Mapper映射文件
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttps://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.itheima.mapper.EmpMapper
!--更新操作--update idupdateupdate empsetif testusername ! nullusername#{username},/ifif testname ! nullname#{name},/ifif testgender ! nullgender#{gender},/ifif testimage ! nullimage#{image},/ifif testjob ! nulljob#{job},/ifif testentrydate ! nullentrydate#{entrydate},/ifif testdeptId ! nulldept_id#{deptId},/ifif testupdateTime ! nullupdate_time#{updateTime}/ifwhere id#{id}/update
/mapper
测试方法
Test
public void testUpdate2(){//要修改的员工信息Emp emp new Emp();emp.setId(20);emp.setUsername(Tom111);emp.setName(汤姆111);
emp.setUpdateTime(LocalDateTime.now());
//调用方法修改员工数据empMapper.update(emp);
} 执行的SQL语句 再次修改测试方法观察SQL语句执行情况
Test
public void testUpdate2(){//要修改的员工信息Emp emp new Emp();emp.setId(20);emp.setUsername(Tom222);//调用方法修改员工数据empMapper.update(emp);
} 执行的SQL语句 以上问题的解决方案使用set标签代替SQL语句中的set关键字 set动态的在SQL语句中插入set关键字并会删掉额外的逗号。用于update语句中
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttps://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.itheima.mapper.EmpMapper
!--更新操作--update idupdateupdate emp!-- 使用set标签代替update语句中的set关键字 --setif testusername ! nullusername#{username},/ifif testname ! nullname#{name},/ifif testgender ! nullgender#{gender},/ifif testimage ! nullimage#{image},/ifif testjob ! nulljob#{job},/ifif testentrydate ! nullentrydate#{entrydate},/ifif testdeptId ! nulldept_id#{deptId},/ifif testupdateTime ! nullupdate_time#{updateTime}/if/setwhere id#{id}/update
/mapper 再次执行测试方法执行的SQL语句 小结 if 用于判断条件是否成立如果条件为true则拼接SQL 形式 if testname ! null … /if where where元素只会在子元素有内容的情况下才插入where子句而且会自动去除子句的开头的AND或OR set 动态地在行首插入 SET 关键字并会删掉额外的逗号。用在update语句中