一、前言
当在 update 语句中使用if标签时,如果前面的if没有执行,则或导致逗号多余错误。使用set标签可以将动态的配置 SET 关键字,并剔除追加到条件末尾的任何不相关的逗号。使用 if+set 标签修改后,如果某项为 null 则不进行更新,而是保持数据库原值。
二、案例
我们先来看看不用set标签时,我们如何实现update功能操作
修改EmpMapper.java类,新增一个方法
/** * 更新员工信息 * @param id */ public void updateEmp(Emp emp);
修改EmpMapper.xml文件:
<update id="updateEmp"> update t_emp set <if test="empName!=null and empName!="""> emp_name=#{empName}, </if> <if test="empEmail!=null and empEmail.trim()!="""> emp_email=#{empEmail}, </if> <if test="deptId!=null"> dept_id=#{deptId} </if> where id=#{id} </update>
修改MyBatisTest测试文件,增加测试方法testUpdateEmp
@Test public void testUpdateEmp() throws IOException { SqlSessionFactory sqlSessionFactory = getSqlSessionFactory(); SqlSession openSession = sqlSessionFactory.openSession(); try { EmpMapper mapper = openSession.getMapper(EmpMapper.class); //Emp emp = new Emp(1,"queen","[email protected]",1);数据库中原来的数据 //做如下修改,将userName修改为queen123,将emp_email修改为[email protected] Emp emp = new Emp(1,"queen123","[email protected]",1); mapper.updateEmp(emp); //别忘了事务最后要提交 openSession.commit(); } finally { openSession.close(); } }
控制台打印结果如下
2017-08-13 09:10:52,912 [main] [com.queen.mybatis.mapper.EmpMapper.updateEmp]-[DEBUG] ==> Preparing: update t_emp set emp_name=?, emp_email=?, dept_id=? where id=? 2017-08-13 09:10:52,968 [main] [com.queen.mybatis.mapper.EmpMapper.updateEmp]-[DEBUG] ==> Parameters: queen123(String), [email protected](String), 1(Integer), 1(Integer) 2017-08-13 09:10:52,969 [main] [com.queen.mybatis.mapper.EmpMapper.updateEmp]-[DEBUG] <== Updates: 1
如我们设想的一样,数据都更新了,这种当所有的条件都传入时,SQL打印正常。那么如果我们只更新这条数据某一列的值呢?例如:只更新userName这一列,会不会有什么问题?
测试方法testUpdateEmp
这时候,我们再测试一下,看看打印结果如何?
org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where id=1' at line 7 ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### SQL: update t_emp set emp_name=?, where id=? ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where id=1' at line 7 at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:26) at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:154) at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:54) at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52) at com.sun.proxy.$Proxy2.updateEmp(Unknown Source)
如上,控制台报错说:MySQL语法错误,很明显emp_name字段后面多了一个逗号 “,” 没有去掉。手写 set 时,有可能会多出一个逗号“,”
接下来我们来看看set标签,set标签可以帮我们把这些多出来逗号去掉
这时候,我们再来测试一下,发现控制台打印正常
2017-08-13 09:25:21,780 [main] [com.queen.mybatis.mapper.EmpMapper.updateEmp]-[DEBUG] ==> Preparing: update t_emp SET emp_name=? where id=? 2017-08-13 09:25:21,843 [main] [com.queen.mybatis.mapper.EmpMapper.updateEmp]-[DEBUG] ==> Parameters: queen321(String), 1(Integer) 2017-08-13 09:25:21,844 [main] [com.queen.mybatis.mapper.EmpMapper.updateEmp]-[DEBUG] <== Updates: 1
多出来逗号没有了
前面学过trim标签,其实trim标签也可以帮我们实现如上功能
测试就不写了,大家可以自己下去修改一下,体会一下就知道了。
总结一下:set 标签元素主要是用在更新操作的时候,它的主要功能和 where 标签元素其实是差不多的,主要是在包含的语句前输出一个 set,然后如果包含的语句是以逗号结束的话将会把该逗号忽略,如果 set 包含的内容为空的话则会出错。有了 set 元素就可以动态的更新那些修改了的字段。
=======欢迎大家拍砖,小手一抖,多多点赞哟!=======
版权声明:本文为博主原创文章,允许转载,但转载必须标明出处。