在实际开发中,适合用于记录创建人修改人

  • pom.xml

    <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.2.0</version>
    </dependency>
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/p6spy/p6spy -->
    <dependency>
    <groupId>p6spy</groupId>
    <artifactId>p6spy</artifactId>
    <version>3.8.0</version>
    </dependency>
    <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
    </dependency>

    <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.49</version>
    <scope>test</scope>
    </dependency>
    <!-- for testing -->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>
    </dependencies>
  • application.yml

    spring:
    datasource:
    driver-class-name: com.p6spy.engine.spy.P6SpyDriver
    url: jdbc:p6spy:h2:tcp://192.168.180.115:19200/~/mem/test
    username: root
    password: test
  • 实体类

    @Data
    @TableName(value = "user")
    @NoArgsConstructor
    @AllArgsConstructor
    public class User { private Long id; private String name; private Integer age; private String email; @TableField(fill = FieldFill.INSERT_UPDATE)
    private String operator;
    }

    @Mapper
    public interface UserMapper extends BaseMapper<User> {

    }
  • 填充器

    @Component
    public class MyMetaObjectHandler implements MetaObjectHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyMetaObjectHandler.class);

    @Override
    public void insertFill(MetaObject metaObject) {
    LOGGER.info("start insert fill ....");
    //避免使用metaObject.setValue()
    this.setFieldValByName("operator", "Jerry", metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
    LOGGER.info("start update fill ....");
    this.setFieldValByName("operator", "Tom", metaObject);
    }
    }
  • 测试类

    @SpringBootTest
    class MetainfoApplicationTests {

    private static final Logger LOGGER = LoggerFactory.getLogger(MetainfoApplicationTests.class);

    @Autowired(required = false)
    private UserMapper userMapper;

    @Test
    public void test(){
    User user = new User(null,"Tom",1,"[email protected]",null);
    userMapper.insert(user);
    LOGGER.info("query user:{}",userMapper.selectById(user.getId()));
    User beforeUser = userMapper.selectById(1L);
    LOGGER.info("before user:{}",beforeUser);
    beforeUser.setAge(12);
    userMapper.updateById(beforeUser);
    LOGGER.info("query user:{}",userMapper.selectById(1L));
    }

    }
  • 测试结果
2019-10-31 13:22:13.328  INFO 14172 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
Consume Time:0 ms 2019-10-31 13:22:13
Execute SQL:INSERT INTO user ( id, name, operator, email, age ) VALUES ( 1189774553572806657, 'Tom', 'Jerry', '[email protected]', 1 )

Consume Time:6 ms 2019-10-31 13:22:13
Execute SQL:SELECT id,name,operator,email,age FROM user WHERE id=1189774553572806657

2019-10-31 13:22:13.394 INFO 14172 --- [ main] c.mp.metainfo.MetainfoApplicationTests : query user:User(id=1189774553572806657, name=Tom, age=1, [email protected], operator=Jerry)
2019-10-31 13:22:13.397 INFO 14172 --- [ main] c.mp.metainfo.MetainfoApplicationTests : before user:User(id=1, name=Jone, age=18, [email protected], operator=test)
Consume Time:0 ms 2019-10-31 13:22:13
Execute SQL:SELECT id,name,operator,email,age FROM user WHERE id=1

2019-10-31 13:22:13.404 INFO 14172 --- [ main] c.m.m.handler.MyMetaObjectHandler : start update fill ....
Consume Time:0 ms 2019-10-31 13:22:13
Execute SQL:UPDATE user SET name='Jone', operator='Tom', email='[email protected]', age=12 WHERE id=1

2019-10-31 13:22:13.417 INFO 14172 --- [ main] c.mp.metainfo.MetainfoApplicationTests : query user:User(id=1, name=Jone, age=12, [email protected], operator=Tom)
Consume Time:1 ms 2019-10-31 13:22:13
Execute SQL:SELECT id,name,operator,email,age FROM user WHERE id=1
05-23 16:36