聊城网站建设策划建设公司,东莞保安公司最新招聘,天然气公司的网站应该怎么做,嘟嘟浏览器目录
if
where
set ctrl alt l格式化SQL语句 随着用户的输入或外部条件的变化而变化的SQL称为动态SQL if if用来判断条件是否成立#xff0c;使用test属性进行条件判断#xff0c;如果true#xff0c;则拼接SQL where wehre元素只会在有条件成立的情况下才插入…目录
if
where
set ctrl alt l格式化SQL语句 随着用户的输入或外部条件的变化而变化的SQL称为动态SQL if if用来判断条件是否成立使用test属性进行条件判断如果true则拼接SQL where wehre元素只会在有条件成立的情况下才插入where子句而且会自动去除开头的AND或OR 如果存在只传递姓名的情况之前的程序会无法成功查询可以通过动态SQL解决上述问题
EmpMapper.xml内容如下
?xml version1.0 encodingutf-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.itheima.mapper.EmpMapper!--resultType单条记录所封装的内容--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}/if/whereorder by update_time desc/select
/mapper SpringbootMybatisCrudApplicationTests.java内容如下
package com.itheima;import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.util.List;SpringBootTest
class SpringbootMybatisCrudApplicationTests {Autowiredprivate EmpMapper empMapper;Testpublic void testSelect(){ListEmp list empMapper.list(null,(short)1,null,null);System.out.println(list);}}运行结果如下 set set动态地在行首插入SET关键字并会删掉额外的逗号(用在update语句中) 将id为18的员工的username改为Tom111name改为Tom111gender改为2其他不变
按照之前的方法进行更新会使其他值均变为null
可以通过动态SQL解决
EmpMapper.xml内容如下
?xml version1.0 encodingutf-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper namespacecom.itheima.mapper.EmpMapper!--resultType单条记录所封装的内容--update idupdateupdate empsetif testusername ! nullusername#{username},/ifif testpassword ! nullpassword#{password},/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
EmpMapper.java内容如下
package com.itheima.mapper;import com.itheima.pojo.Emp;
import org.apache.ibatis.annotations.*;Mapper
public interface EmpMapper {public void update(Emp emp);}此次更新id为19的员工SpringbootMybatisCrudApplicationTests.java内容如下
package com.itheima;import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import java.time.LocalDateTime;SpringBootTest
class SpringbootMybatisCrudApplicationTests {Autowiredprivate EmpMapper empMapper;Testpublic void testUpdate(){Emp emp new Emp();emp.setId(19);emp.setUsername(Tom2222);emp.setName(Tom222);emp.setGender((short)1);emp.setUpdateTime(LocalDateTime.now());empMapper.update(emp);}} 运行结果如下发现只更新了四个字段其余字段不变