持久层
# 用户表设计
CREATE TABLE `t_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`username` varchar(256) DEFAULT NULL COMMENT '用户名',
`password` varchar(512) DEFAULT NULL COMMENT '密码',
`real_name` varchar(256) DEFAULT NULL COMMENT '真实姓名',
`phone` varchar(128) DEFAULT NULL COMMENT '手机号',
`mail` varchar(512) DEFAULT NULL COMMENT '邮箱',
`deletion_time` bigint(20) DEFAULT NULL COMMENT '注销时间戳',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '修改时间',
`del_flag` tinyint(1) DEFAULT NULL COMMENT '删除标识 0:未删除 1:已删除',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 持久层框架
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 持久层配置
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://127.0.0.1:3306/link?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
connection-test-query: select 1
connection-timeout: 20000
idle-timeout: 300000
maximum-pool-size: 5
minimum-idle: 5
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 持久层接口扫描器
@MapperScan("com.Nreal.shortlink.admin.dao.mapper")
1
# MybatisPlus元数据填充
定义一个BaseDO,让UserDO继承此类:
@Data
public class BaseDO {
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private Date createTime;
/**
* 修改时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
/**
* 删除标识 0:未删除 1:已删除
*/
@TableField(fill = FieldFill.INSERT)
private Integer delFlag;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
配置类:
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
strictInsertFill(metaObject, "createTime", Date::new, Date.class);
strictInsertFill(metaObject, "updateTime", Date::new, Date.class);
strictInsertFill(metaObject, "delFlag", () -> 0, Integer.class);
}
@Override
public void updateFill(MetaObject metaObject) {
strictInsertFill(metaObject, "updateTime", Date::new, Date.class);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14