确认
当前程序版本
3.5.8
问题描述
MySQL数据库,使用updateWrapper修改json类型字段时,typeHandler = JacksonTypeHandler.class不生效。
使用的是plus提供的JacksonTypeHandler。
@TableField(value = "num_attrs", typeHandler = JacksonTypeHandler.class)
private List
在使用updateById() 方法,可以修改成功。
public String update4(@RequestBody Demo demo) {
Demo demo1 = new Demo();
demo1.setId(demo.getId());
demo1.setNumAttrs(demo.getNumAttrs());
demoService.updateById(demo1);
return demo.toString();
}
但是使用Wrapper 修改就不行
public String update3(@RequestBody Demo demo) {
demoService.update(Wrappers.lambdaUpdate(Demo.class)
.set(Demo::getNumAttrs, demo.getNumAttrs())
.eq(Demo::getId, demo.getId()));
return demo.toString();
}
### Error updating database. Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
### The error may exist in com/fu/test/mapper/DemoMapper.java (best guess)
### The error may involve com.fu.test.mapper.DemoMapper.update-Inline
### The error occurred while setting parameters
### SQL: UPDATE demo SET num_attrs=? WHERE (id = ?)
### Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
; Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.; nested exception is com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.] with root cause
com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
详细堆栈日志
No response
Comment From: fu9809
看了其他的 issues,这样写就可以修改成功
public String update3(@RequestBody Demo demo) {
demoService.update(Wrappers.lambdaUpdate(Demo.class)
.set(Demo::getNumAttrs, demo.getNumAttrs(), "typeHandler=com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler")
.eq(Demo::getId, demo.getId()));
return demo.toString();
}
不理解,为什么使用Wrapper这个写法,就需要去指定 typeHandler,这个问题在去年就有人提到过,现在还是这样
Comment From: john1337
这个实现方式确实很鸡肋啊