一. 搭建基础项目引入依赖
controller
@RestControllerpublic class TestController {@Autowiredprivate IProjectService projectService;@ApiOperation("新增项目")@PostMapping("/")public void addProjectWrite(@RequestBody Project project) {projectService.save(project);}}service
public interface IProjectService extends IService {}serviceImpl
@Servicepublic class ProjectServiceImpl extends ServiceImpl implements IProjectService {} Mapper
@Servicepublic class ProjectServiceImpl extends ServiceImpl implements IProjectService {} Pojo
@Data@EqualsAndHashCode(callSuper = false)@Accessors(chain = true)@TableName("ts_project")@ApiModel(value="https://tazarkount.com/read/Project对象", description="撰写项目申请书的基本内容")public class Project implements Serializable {private static final long serialVersionUID = 1L;@TableId(value = "https://tazarkount.com/read/id", type = IdType.AUTO)private String id;private Integer workNumber;private Integer adminId;private String name;@ApiModelProperty(value = "https://tazarkount.com/read/创建时间")@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")private Date gmtCreate;@ApiModelProperty(value = "https://tazarkount.com/read/更新时间")@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: 8081spring:# 数据源配置datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghaiusername: rootpassword: root# Mybatis-plus配置mybatis-plus:#配置Mapper映射文件mapper-locations: classpath*:/mapper/*Mapper.xml# 配置MyBatis数据返回类型别名(默认别名是类名)type-aliases-package: com.xxxx.server.pojoconfiguration:# 自动驼峰命名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文件,实现自动填充
/** * 自动填充时间 */@Componentpublic class MyMetaObjectHandler implements MetaObjectHandler {@Overridepublic void insertFill(MetaObject metaObject) {this.setFieldValByName("gmtCreate",new Date(), metaObject);this.setFieldValByName("gmtModified", new Date(), metaObject);}@Overridepublic void updateFill(MetaObject metaObject) {this.setFieldValByName("gmtModified", new Date(), metaObject);}}
修改pojo类
@Data@EqualsAndHashCode(callSuper = false)@Accessors(chain = true)@TableName("ts_project")@ApiModel(value="https://tazarkount.com/read/Project对象", description="撰写项目申请书的基本内容")public class Project implements Serializable {private static final long serialVersionUID = 1L;@TableId(value = "https://tazarkount.com/read/id", type = IdType.AUTO)private String id;private Integer workNumber;private Integer adminId;private String name;@ApiModelProperty(value = "https://tazarkount.com/read/创建时间")@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 = "https://tazarkount.com/read/更新时间")@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) 表示创建时间 。此时执行操作,会在表中添加时间,如下: 【代码直接可用 Mybatis-plus实现时间自动填充】
在gmtModified上增加 @TableField(fill = FieldFill.INSERT_UPDATE)表示修改时间 。
gmtCreate和gmtModified需要与自定义方法中的字段相匹配 。
- 春季老年人吃什么养肝?土豆、米饭换着吃
- 三八妇女节节日祝福分享 三八妇女节节日语录
- 老人谨慎!选好你的“第三只脚”
- 校方进行了深刻的反思 青岛一大学生坠亡校方整改校规
- 脸皮厚的人长寿!有这特征的老人最长寿
- 长寿秘诀:记住这10大妙招 100%增寿
- 春季老年人心血管病高发 3条保命要诀
- 眼睛花不花要看四十八 老年人怎样延缓老花眼
- 香槟然能防治老年痴呆症? 一天三杯它人到90不痴呆
- 老人手抖的原因 为什么老人手会抖
