老司机学习MyBatis之动态SQL使用set执行更新操作

一、前言

当在 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","queen@sina.com",1);数据库中原来的数据
		//做如下修改,将userName修改为queen123,将emp_email修改为queen123@sina.com
		Emp emp = new Emp(1,"queen123","queen123@sina.com",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), queen123@sina.com(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 元素就可以动态的更新那些修改了的字段。

 

                                                      1563721777457161.png

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值