当前使用版本(必须填写清楚,否则不予处理)
com.baomidou mybatis-plus-boot-starter 3.0.7.1 spingboot2
该问题是怎么引起的?(最新版上已修复的会直接close掉)
po采用了多继承,在父类中配置了insert、update的自动填充字段,但是并没有触发自动填充。debug模式下不会进入MyMetaObjectHandler的insertFill和updateFill方法。 具体代码如下: 子po类
@TableName("base_data_system")
public class DataSystem extends BaseEntity implements Serializable {
private String name;
private String description;
private String chargeDepartment;
private String chargePerson;
geter and setter
}
父po类
public class BaseEntity {
@TableId private Long id;
@TableField(value = "create_user_id",fill = FieldFill.INSERT)
private Long createUserId;
@TableField(value = "update_user_id",fill = FieldFill.INSERT_UPDATE)
private Long updateUserId;
private Date createTime;
@TableField(value = "update_time",fill = FieldFill.UPDATE)
private Date updateTime;
getter and setter
}
handler类(将引用部分也copy过来了)
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.nopadding.sapphire.backend.constant.Constant;
import com.nopadding.sapphire.backend.po.User;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpSession;
import java.util.Date;
/**
* mybatis-plus 自定义handler,实现部分字段的自动填充
* @author cheyantao
* @date 2019/1/29
*/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
private static final Logger LOGGER = Logger.getLogger(MyMetaObjectHandler.class);
@Autowired
HttpSession session;
@Override
public void insertFill(MetaObject metaObject) {
LOGGER.info("insert字段自动填充");
this.setInsertFieldValByName("createUserId",getUserId(),metaObject);
this.setInsertFieldValByName("updateUserId",getUserId(),metaObject);
}
@Override
public void updateFill(MetaObject metaObject) {
LOGGER.info("update字段自动填充");
this.setUpdateFieldValByName("updateUserId",getUserId(),metaObject);
this.setUpdateFieldValByName("updateTime",new Date(),metaObject);
}
private Long getUserId(){
User currentUser= (User) this.session.getAttribute(Constant.CURRENT_USER_TAG);
return currentUser==null?1L:currentUser.getId();
}
}
单元测试代码:
@Test
public void testInsert(){
DataSystem system=new DataSystem();
system.setName("test1");
system.setDescription("test1");
system.setChargePersonTel("dasdad");
system.setChargePerson("person1");
system.setChargeDepartment("department1");
system.setState(1);
int test= this.mapper.insert(system);
System.out.println(test);
}
重现步骤
一直出问题。
报错信息
报错信息为mybatis的字段不能为空的错误,因为相关字段在数据库设置了不能为空且没有默认值。如下: Caused by: java.sql.SQLIntegrityConstraintViolationException: Column 'create_user_id' cannot be null at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:117) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
Comment From: ytche
因为我自己还改了一些mybaits的配置,MetaObjectHandler是否依赖于哪个可能被禁用的mybatis配置?
Comment From: miemieYaho
https://gitee.com/baomidou/mybatis-plus-samples/tree/master/mybatis-plus-sample-auto-fill-metainfo
Comment From: ZJM6658
老哥,你的解决了吗? 我遇到同样的问题,设置无效 @ytche
Comment From: ytche
@ZJM6658 已经解决了,只是时间太久远我也记不太清晰了,只能看着git提交记录回忆一下。 因为我用的是springboot,我记得是配置项的问题。 之前因为刚使用springboot,网上抄了一段配置代码
@Bean(name = "dbSqlSessionFactory")
public SqlSessionFactory dbSqlSessionFactory(@Qualifier("dbData") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));
sqlSessionFactoryBean.setConfigLocation( new ClassPathResource("mybatis-config.xml"));
return sqlSessionFactoryBean.getObject();
}
应为之前被
- SqlSessionFactory不要使用原生的,请使用MybatisSqlSessionFactory
坑过, 我就抱着试一试的态度把这段配置代码删掉,然后选择配置文件配置mapper的文件路径。
mybatis-plus:
mapper-locations: classpath*:/mapper/**/*.xml
问题就解决了。 希望能够帮到你
Comment From: light0x00
遇到了同样的问题,我的场景是因为有多数据源,所以 new 了两个 MybatisSqlSessionFactory 实例 ,这种情况需要手动设置 MetaObjectHandler
@Bean(name = "messageSqlSessionFactory")
public SqlSessionFactory messageSqlSessionFactoryBean(@Qualifier("messageDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
//mybatis 原生配置
mybatisSqlSessionFactoryBean.setDataSource(dataSource);
//mp 自动填充
GlobalConfig globalConfig=new GlobalConfig();
globalConfig.setMetaObjectHandler(new MyMetaObjectHandler());
mybatisSqlSessionFactoryBean.setGlobalConfig(globalConfig);
return mybatisSqlSessionFactoryBean.getObject();
}