mybatisPlus自动填创建时间和修改时间

package com.example.demo.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component // 一定不要忘记把处理器加到IOC容器中
public class MyMetaObjectHandler implements MetaObjectHandler {

    // 插入时候的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ...");
        // setFieldValByName(String fieldName, Object fieldVal, MetaObject metaObject)
        this.setFieldValByName("createTime", new Date(), metaObject);
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }

    // 更新时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ...");
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }

}
 /**
     *创建日期
     * @author lzc
     * @date 2021/6/1 14:08
     */
    @TableField( value = "crt_time",fill = FieldFill.INSERT)
    private Date createTime;


    /**
     *修改日期
     * @author lzc
     * @date 2021/6/1 14:08
     */
    @TableField(value = "upd_time",fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
    private Date updateTime;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus是一款优秀的MyBatis框架的增强工具包,它为MyBatis提供了许多强大的功能,其中就包括自动生成代码的功能。 使用MyBatis-Plus自动生成代码的步骤如下: 1. 首先在项目的pom.xml文件中添加MyBatis-Plus的依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> </dependency> ``` 2. 在application.yml或application.properties文件中配置MyBatis-Plus的自动生成代码的相关配置,例如: ```yaml # 数据源配置 spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC username: root password: root # MyBatis-Plus配置 mybatis-plus: # 实体扫描,多个package用逗号或者分号分隔 typeAliasesPackage: com.example.demo.entity # XML文件路径 mapperLocations: classpath:/mapper/*.xml # 自动充配置 global-config: db-config: # 自动充配置 field-strategy: not_null # 数据库主键自增策略 id-type: auto # 字段名下划线转驼峰 column-underline: true # 代码生成器配置 generator: # 生成文件输出目录 output-dir: src/main/java # 是否覆盖已有文件 fileOverride: true # 是否开启swagger注解 swagger2: true # 自定义文件命名,注意 %s 会自动充表实体属性! entity-name: %sEntity mapper-name: %sMapper xml-name: %sMapper service-name: %sService service-impl-name: %sServiceImpl controller-name: %sController # 指定生成的表名,多个表名用逗号分隔 include: user, role # 排除生成的表名,多个表名用逗号分隔 exclude: test, demo # 自定义继承的Entity类全称,带包名 super-entity-class: com.example.demo.common.BaseEntity # 自定义继承的Mapper类全称,带包名 super-mapper-class: com.example.demo.common.BaseMapper # 自定义继承的Service类全称,带包名 super-service-class: com.example.demo.common.BaseService # 自定义继承的ServiceImpl类全称,带包名 super-service-impl-class: com.example.demo.common.BaseServiceImpl # 自定义继承的Controller类全称,带包名 super-controller-class: com.example.demo.common.BaseController # 是否去掉生成实体时生成的get、set方法 entity-boolean-logic: false # 是否支持 ActiveRecord 模式 active-record: true # 是否生成实体时,生成字段注解 entity-column-annotation-enable: true # 是否生成 @RestController 控制器 rest-controller-style: true ``` 3. 在项目中创建一个代码生成器的类,例如: ```java package com.example.demo.generator; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; public class CodeGenerator { public static void main(String[] args) { // 代码生成器 AutoGenerator mpg = new AutoGenerator(); // 全局配置 GlobalConfig gc = new GlobalConfig(); gc.setOutputDir(System.getProperty("user.dir") + "/src/main/java"); gc.setAuthor("your name"); gc.setOpen(false); gc.setFileOverride(true); gc.setActiveRecord(false); gc.setEnableCache(false); gc.setBaseResultMap(true); gc.setBaseColumnList(true); gc.setIdType(IdType.AUTO); gc.setServiceName("%sService"); gc.setServiceImplName("%sServiceImpl"); gc.setControllerName("%sController"); mpg.setGlobalConfig(gc); // 数据源配置 DataSourceConfig dsc = new DataSourceConfig(); dsc.setDbType(DbType.MYSQL); dsc.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC"); dsc.setDriverName("com.mysql.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("root"); mpg.setDataSource(dsc); // 包配置 PackageConfig pc = new PackageConfig(); pc.setParent("com.example.demo"); pc.setEntity("entity"); pc.setMapper("mapper"); pc.setService("service"); pc.setServiceImpl("service.impl"); pc.setController("controller"); mpg.setPackageInfo(pc); // 策略配置 StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); strategy.setControllerMappingHyphenStyle(true); strategy.setInclude("user", "role"); strategy.setTablePrefix(pc.getModuleName() + "_"); mpg.setStrategy(strategy); // 执行生成 mpg.execute(); } } ``` 4. 运行代码生成器的main方法即可自动生成代码。 注意:在执行代码生成器之前,需要先创建好对应的数据库表。另外,生成的代码需要进行适当的修改和优化,以符合实际的业务需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值