Mybatis-plus实现主键自增和自动注入时间的示例代码
作者:Scavenger 时间:2022-11-05 00:22:51
mybatis-plus依赖导入
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
建议使用3.3.0后的版本。
导入mybatis-plus就不用导入mybatis了,冲突!
连接数据库
spring.datasource.username=root
spring.datasource.password=19981204
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl//配置默认日志
设置主键自增
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
}
使用注解 @TableId(type=IdType.AUTO),同时将数据库中的id字段设置为自增即可。
编写配置类来进行自动注入时间(继承元数据类并重写它的insertFill和updateFill方法)
使用注解@TableFiled(fill=FieldFill.INSERT)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
@TableField(fill = FieldFill.INSERT)
private LocalDateTime createTime;
@TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime;
}
package com.ls.handler;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
/**
* @author dell
*/
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject,"createTime", LocalDateTime.class,LocalDateTime.now());
this.strictUpdateFill(metaObject,"updateTime",LocalDateTime.class,LocalDateTime.now());
}
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject,"updateTime",LocalDateTime.class,LocalDateTime.now());
}
}
来源:https://segmentfault.com/a/1190000023287521
标签:Mybatis-plus,主键自增,自动注入时间
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java基础中异常及包归纳整理
2022-11-08 17:46:02
![](https://img.aspxhome.com/file/2023/2/115892_0s.gif)
SpringBoot使用WebSocket的方法实例详解
2022-12-26 03:19:25
Android使用Handler实现下载文件功能
2023-05-21 03:23:08
![](https://img.aspxhome.com/file/2023/3/137943_0s.gif)
Springboot项目中使用redis的配置详解
2021-11-26 03:43:44
![](https://img.aspxhome.com/file/2023/7/70367_0s.png)
Spring Cloud Gateway 服务网关快速实现解析
2023-12-19 04:28:33
![](https://img.aspxhome.com/file/2023/5/127215_0s.jpg)
在Java内存模型中测试并发程序代码
2023-11-24 20:37:55
![](https://img.aspxhome.com/file/2023/7/59967_0s.png)
Mapper类中存在名称相同的方法重载报错问题
2023-04-04 02:44:39
Android实现输入法弹出时把布局顶上去和登录按钮顶上去的解决方法
2022-11-10 15:07:26
![](https://img.aspxhome.com/file/2023/7/126707_0s.jpg)
简单讲解Android开发中触摸和点击事件的相关编程方法
2023-03-30 12:29:10
Java8 Stream流的常用方法汇总
2023-07-17 17:56:59
![](https://img.aspxhome.com/file/2023/6/65196_0s.png)
秒懂Kotlin之Java工程师快速掌握Kotlin的技巧
2023-07-09 21:25:19
![](https://img.aspxhome.com/file/2023/7/75697_0s.png)
Android存储字符串数据到txt文件
2021-11-05 21:55:16
软件开发七大过程模型
2023-12-22 17:06:14
![](https://img.aspxhome.com/file/2023/2/68722_0s.jpg)
Spring自动配置之condition条件判断下篇
2023-10-15 10:04:59
![](https://img.aspxhome.com/file/2023/0/62060_0s.png)
VS2010中lib与dll文件的生成与使用方法
2023-01-23 11:32:50
Java NIO和IO的区别
2023-07-15 22:53:46
![](https://img.aspxhome.com/file/2023/2/75432_0s.png)
Java定时调用.ktr文件的示例代码(解决方案)
2021-12-29 13:21:49
![](https://img.aspxhome.com/file/2023/7/72537_0s.png)
Java 异步编程实践_动力节点Java学院整理
2023-06-18 17:04:12
使用android隐藏api实现亮度调节的方法
2022-10-16 23:37:36
Java基础知识之Java语言概述
2023-11-28 04:36:35
![](https://img.aspxhome.com/file/2023/0/60080_0s.jpg)