jpa中MetaObjectHandler允许子项目覆盖
This commit is contained in:
@ -0,0 +1,28 @@
|
||||
package com.chinaunicom.mall.ebtp.cloud.mybatis.example.config;
|
||||
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
|
||||
@Configuration
|
||||
public class ExampleConfiguration {
|
||||
|
||||
@Bean
|
||||
MetaObjectHandler metaObjectHanlder() {
|
||||
return new MetaObjectHandler() {
|
||||
|
||||
@Override
|
||||
public void updateFill(MetaObject metaObject) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
setFieldValByName("createBy", "Example", metaObject);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -2,17 +2,24 @@ package com.chinaunicom.mall.ebtp.cloud.mybatis.example.model;
|
||||
|
||||
import java.sql.Date;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class Demo {
|
||||
|
||||
|
||||
private Long id;
|
||||
private String col1;
|
||||
private String col2;
|
||||
|
||||
/** 参数自动注入 */
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private String createBy;
|
||||
|
||||
private Date createTime;
|
||||
private String updateBy;
|
||||
private Date updateTime;
|
||||
|
@ -10,8 +10,6 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
import com.chinaunicom.mall.ebtp.cloud.mybatis.example.mapper.DemoMapper;
|
||||
import com.chinaunicom.mall.ebtp.cloud.mybatis.example.model.Demo;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class MybatisPlusExampleTest {
|
||||
@ -24,12 +22,10 @@ public class MybatisPlusExampleTest {
|
||||
@Test
|
||||
public void testInsert() {
|
||||
Demo demo = new Demo();
|
||||
// demo.setId("10");
|
||||
// demo.setCol1("test");
|
||||
// demo.setCol2("test01");
|
||||
//
|
||||
// demo.setId("10"); 不需要认为设置id,由mybatis-plus自动生成
|
||||
demo.setCol1("test");
|
||||
demo.setCol2("test01");
|
||||
|
||||
System.out.println(IdUtil.getSnowflake(30, 30).nextId());
|
||||
Assertions.assertThat(mapper.insert(demo)).isGreaterThan(0);
|
||||
}
|
||||
//
|
||||
|
@ -3,6 +3,7 @@ package com.chinaunicom.mall.ebtp.cloud.jpa.starter;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.ComponentScan.Filter;
|
||||
@ -21,6 +22,7 @@ import cn.chinaunicom.sdsi.framework.config.mybatis.MyMetaObjectHandler;
|
||||
public class JpaStarterConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(MetaObjectHandler.class)
|
||||
MetaObjectHandler metaObjectHandler() {
|
||||
return new MetaObjectHandler() {
|
||||
/**
|
||||
@ -43,6 +45,8 @@ public class JpaStarterConfiguration {
|
||||
*/
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
setFieldValByName("createBy", "tester", metaObject);
|
||||
|
||||
// Object obj = getFieldValByName("createBy", metaObject);
|
||||
// obj = getFieldValByName("createDate", metaObject);
|
||||
// if (obj == null) {
|
||||
|
Reference in New Issue
Block a user