首頁 > 軟體

使用Mybatis-plus實現時間自動填充(程式碼直接可用)

2022-06-17 14:03:51

一. 搭建基礎專案

引入依賴

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--lombok 依賴-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- swagger2 依賴 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!-- Swagger第三方ui依賴 -->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1.tmp</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>
    </dependencies>

controller

@RestController
public class TestController {
 
    @Autowired
    private IProjectService projectService;
 
    @ApiOperation("新增專案")
    @PostMapping("/")
    public void addProjectWrite(@RequestBody Project project) {
        projectService.save(project);
    }
}

service

public interface IProjectService extends IService<Project> {
 
}

serviceImpl

@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {
 
}

Mapper

@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {
 
}

Pojo

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("ts_project")
@ApiModel(value="Project物件", description="撰寫專案申請書的基本內容")
public class Project implements Serializable {
 
    private static final long serialVersionUID = 1L;
    @TableId(value = "id", type = IdType.AUTO)
    private String id;
 
    private Integer workNumber;
    private Integer adminId;
    private String name;
 
    @ApiModelProperty(value = "建立時間")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date gmtCreate;
 
    @ApiModelProperty(value = "更新時間")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date gmtModified;
 
}

application.yml 

server:
  # 埠
  port: 8081
 
spring:
  # 資料來源設定
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: root
 
 
# Mybatis-plus設定
mybatis-plus:
  #設定Mapper對映檔案
  mapper-locations: classpath*:/mapper/*Mapper.xml
  # 設定MyBatis資料返回型別別名(預設別名是類名)
  type-aliases-package: com.xxxx.server.pojo
  configuration:
    # 自動駝峰命名
    map-underscore-to-camel-case: fals

啟動類

@SpringBootApplication
@EnableScheduling
@MapperScan("com.xxxx.server.mapper")
public class TestApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class,args);
    }
}

搭建完成 

此時執行操作,並不會在表中新增時間,如下:

二. 設定自動填充

建立MyMetaObjectHandler檔案,實現自動填充

/**
 * 自動填充時間
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        this.setFieldValByName("gmtCreate",  new Date(), metaObject);
        this.setFieldValByName("gmtModified", new Date(), metaObject);
    }
 
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("gmtModified", new Date(), metaObject);
 
    }
}

修改pojo類

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("ts_project")
@ApiModel(value="Project物件", description="撰寫專案申請書的基本內容")
public class Project implements Serializable {
 
    private static final long serialVersionUID = 1L;
    @TableId(value = "id", type = IdType.AUTO)
    private String id;
 
    private Integer workNumber;
    private Integer adminId;
    private String name;
 
    @ApiModelProperty(value = "建立時間")
    @TableField(fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date gmtCreate;
 
    @ApiModelProperty(value = "更新時間")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date gmtModified;
}
  • 在gmtCreate上增加   @TableField(fill = FieldFill.INSERT) 表示建立時間。
  • 在gmtModified上增加 @TableField(fill = FieldFill.INSERT_UPDATE)表示修改時間。
  • gmtCreate和gmtModified需要與自定義方法中的欄位相匹配。

此時執行操作,會在表中新增時間,如下:

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。 


IT145.com E-mail:sddin#qq.com