0%

打造一套自己的业务开发模版

从今天起,开始打造一套属于自己的业务开发代码模版,本系列不会直接提供代码,只会展示部分关键性代码片段。

建立java模版

进入https://start.spring.io/

选择:

  • Spring Web
  • Lombok

  • Validation

  • Mybatis Framework

  • MySQL Driver

  • Spring Boot DevTools

  • Flyway Migration

1
autoboot ~/Downloads/template.zip

这里是我自己定制的一个自动解压开intellij脚本。

加入mybatix支持配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<properties>
...
<mybatisx.version>3.5.1</mybatisx.version>
...
</properties>
<dependencies>
...
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisx.version}</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

加入guavacommons-lang3mapstructswagger3 支持配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<properties>
...
<mapstruct.version>1.4.2.Final</mapstruct.version>
<guava.version>31.0.1-jre</guava.version>
<openapi.version>1.6.6</openapi.version>
...
</properties>
<dependencies>
...
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${mapstruct.version}</version>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>${openapi.version}</version>
</dependency>
...
</dependencies>

添加数据库配置,准备执行DDL

1
2
3
4
5
6
spring.application.name=template

spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/template?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL
spring.datasource.username=****
spring.datasource.password=****

创建sql文件并导入DDL

1
touch resources/db/migration/V1.0__INIT_DDL.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
SET NAMES utf8mb4;

DROP TABLE IF EXISTS `article`;
CREATE TABLE `article` (
`id` int NOT NULL AUTO_INCREMENT,
`content` varchar(255) DEFAULT '' COMMENT '文章内容',
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`delete_status` varchar(1) DEFAULT '1' COMMENT '是否有效 1.有效 2无效',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='文章表';

LOCK TABLES `article` WRITE;
INSERT INTO `article` (`id`, `content`, `create_time`, `update_time`, `delete_status`)
VALUES (5, '莎士比亚', '2017-10-25 01:08:45', '2017-10-30 09:59:41', '1'),
(6, '亚里士多德', '2017-10-26 02:49:28', '2017-11-18 01:54:15', '1'),
(10, '亚历山大', '2017-10-26 06:57:45', '2017-11-08 05:28:52', '1'),
(11, '李白', '2017-10-26 07:23:42', '2017-10-26 07:23:42', '1'),
(19, '文章test2🤣', '2017-11-18 05:37:07', '2021-05-08 08:10:55', '1'),
(20, '123', '2022-04-12 07:48:49', '2022-04-12 07:48:49', '1'),
(21, 'asdfasdf\n23412', '2022-04-12 07:49:10', '2022-04-12 07:49:10', '1'),
(22, '0c5f0411-ba35-11ec-9d10-a45e60dad82f', '2022-04-12 07:49:17', '2022-04-12 07:49:17', '1'),
(23, '0c5f0411-ba35-11ec-9d10-a45e60dad82f2', '2022-04-12 07:49:22', '2022-04-12 07:49:22', '1'),
(24, '0c5f0411-ba35-11ec-9d10-a45e60dad823', '2022-04-12 07:49:26', '2022-04-12 07:49:26', '1'),
(25, '0c5f0411-ba35-11ec-9d10-a45e60dad82f4', '2022-04-12 07:49:30', '2022-04-12 07:49:30', '1'),
(26, '0c5f0411-ba35-11ec-9d10-a45e60dad825', '2022-04-12 07:49:34', '2022-04-12 07:49:34', '1'),
(27, '123abcd', '2022-04-12 07:56:54', '2022-04-12 08:11:37', '1'),
(28, 'lkaldfjalskdflaskjdflakjsdflasdfasdf', '2022-04-21 08:38:22', '2022-04-21 08:38:22', '1');
UNLOCK TABLES;


DROP TABLE IF EXISTS `sys_resource`;
CREATE TABLE `sys_resource` (
`id` int NOT NULL DEFAULT '0' COMMENT '自定id,主要供前端展示权限列表分类排序使用.',
`code` varchar(50) NOT NULL DEFAULT '' COMMENT '资源编号',
`name` varchar(50) NOT NULL DEFAULT '' COMMENT '资源名称',
`description` varchar(255) NOT NULL DEFAULT '' COMMENT '资源描述',
`parent_code` varchar(50) NOT NULL DEFAULT '' COMMENT '父级资源编号',
`type` tinyint(1) NOT NULL DEFAULT '0' COMMENT '资源类型,0.菜单,1.模块,2.子附属',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
`create_user` varchar(50) NOT NULL DEFAULT '' COMMENT '创建人',
`update_user` varchar(50) NOT NULL DEFAULT '' COMMENT '更新人',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='后台权限表';

LOCK TABLES `sys_resource` WRITE;
INSERT INTO `sys_resource` (`id`, `code`, `name`, `description`, `parent_code`, `type`, `create_time`, `update_time`,
`create_user`, `update_user`)
VALUES (100, 'article', '文章管理', '', '', 0, '2022-04-18 10:08:41', '2022-04-18 10:08:41', 'admin', 'admin'),
(101, 'article:list', '文章管理-列表', '', 'article', 2, '2022-04-18 10:08:41', '2022-04-18 10:08:41', 'admin',
'admin'),
(102, 'article:add', '文章管理-添加', '', 'article', 1, '2022-04-18 10:08:41', '2022-04-18 10:08:41', 'admin',
'admin'),
(103, 'article:update', '文章管理-更新', '', 'article', 1, '2022-04-18 10:08:41', '2022-04-18 10:08:41', 'admin',
'admin'),
(600, 'user', '用户', '', '', 0, '2022-04-18 10:08:41', '2022-04-18 10:08:41', 'admin', 'admin'),
(601, 'user:list', '用户-列表', '', 'user', 2, '2022-04-18 10:08:41', '2022-04-18 10:08:41', 'admin', 'admin'),
(602, 'user:add', '用户-添加', '', 'user', 1, '2022-04-18 10:08:41', '2022-04-18 10:08:41', 'admin', 'admin'),
(603, 'user:update', '用户-更新', '', 'user', 1, '2022-04-18 10:08:41', '2022-04-18 10:08:41', 'admin', 'admin'),
(700, 'role', '角色权限', '', '', 0, '2022-04-18 10:08:41', '2022-04-18 10:08:41', 'admin', 'admin'),
(701, 'role:list', '角色权限-列表', '', 'role', 2, '2022-04-18 10:08:41', '2022-04-18 10:08:41', 'admin', 'admin'),
(702, 'role:add', '角色权限-添加', '', 'role', 1, '2022-04-18 10:08:41', '2022-04-18 10:08:41', 'admin', 'admin'),
(703, 'role:update', '角色权限-更新', '', 'role', 1, '2022-04-18 10:08:41', '2022-04-18 10:08:41', 'admin', 'admin'),
(704, 'role:delete', '角色权限-删除', '', 'role', 1, '2022-04-18 10:08:41', '2022-04-18 10:08:41', 'admin', 'admin');
UNLOCK TABLES;

DROP TABLE IF EXISTS `sys_role`;
CREATE TABLE `sys_role` (
`id` int NOT NULL AUTO_INCREMENT,
`role_code` varchar(20) NOT NULL DEFAULT '',
`role_name` varchar(20) DEFAULT NULL COMMENT '角色名',
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`delete_status` varchar(1) DEFAULT '1' COMMENT '是否有效 1有效 2无效',
PRIMARY KEY (`id`),
UNIQUE KEY `role_code_uindex` (`role_code`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='后台角色表';

LOCK TABLES `sys_role` WRITE;
INSERT INTO `sys_role` (`id`, `role_code`, `role_name`, `create_time`, `update_time`, `delete_status`)
VALUES (1, 'admin', '管理员', '2017-11-22 08:24:34', '2022-04-15 10:11:02', '1'),
(2, 'writer', '作家🌝', '2017-11-22 08:24:34', '2022-04-15 10:11:02', '1'),
(3, 'programer', '程序员', '2017-11-22 08:28:47', '2022-04-15 10:11:02', '1'),
(4, 'viewer', '只有文章', '2021-05-08 15:21:42', '2022-04-21 10:01:03', '1'),
(5, 'haha', '哈哈1', '2022-04-21 03:19:28', '2022-04-21 03:35:00', '2');
UNLOCK TABLES;

DROP TABLE IF EXISTS `sys_role_resource`;
CREATE TABLE `sys_role_resource` (
`id` int NOT NULL AUTO_INCREMENT,
`role_code` varchar(20) NOT NULL DEFAULT '' COMMENT '角色id',
`resource_code` varchar(50) NOT NULL DEFAULT '' COMMENT '权限id',
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`create_user` varchar(255) NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `role_permission_code_uindex` (`role_code`,`resource_code`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='角色-权限关联表';


LOCK TABLES `sys_role_resource` WRITE;
INSERT INTO `sys_role_resource` (`id`, `role_code`, `resource_code`, `create_time`, `create_user`)
VALUES (1, 'writer', 'article:list', '2017-11-22 08:26:21', 'admin'),
(2, 'writer', 'article:add', '2017-11-22 08:26:21', 'admin'),
(6, 'writer', 'article:update', '2017-11-22 08:28:28', 'admin'),
(7, 'writer', 'user:list', '2017-11-22 08:28:28', 'admin'),
(9, 'writer', 'user:add', '2017-11-22 08:28:28', 'admin'),
(10, 'writer', 'user:update', '2017-11-22 08:28:28', 'admin'),
(11, 'writer', 'role:list', '2017-11-22 08:28:31', 'admin'),
(12, 'writer', 'role:add', '2017-11-22 08:28:31', 'admin'),
(14, 'programer', 'user:add', '2017-11-22 08:28:47', 'admin'),
(16, 'programer', 'role:list', '2017-11-22 08:35:01', 'admin'),
(18, 'programer', 'article:list', '2017-11-22 08:35:01', 'admin'),
(19, 'programer', 'user:list', '2017-11-22 08:35:01', 'admin'),
(20, 'viewer', 'article:list', '2021-05-08 15:21:42', 'admin'),
(23, 'writer', 'article', '2017-11-22 08:26:21', 'admin'),
(24, 'writer', 'role', '2017-11-22 08:28:28', 'admin'),
(25, 'writer', 'user', '2017-11-22 08:28:28', 'admin'),
(26, 'programer', 'article', '2017-11-22 08:35:01', 'admin'),
(27, 'programer', 'user', '2017-11-22 08:35:01', 'admin'),
(28, 'programer', 'role', '2017-11-22 08:35:01', 'admin'),
(29, 'viewer', 'article', '2021-05-08 15:21:42', 'admin');
UNLOCK TABLES;

DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (
`id` int NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL COMMENT '用户名',
`password` varchar(255) DEFAULT NULL COMMENT '密码',
`nickname` varchar(255) DEFAULT NULL COMMENT '昵称',
`phone` varchar(50) NOT NULL DEFAULT '',
`email` varchar(50) NOT NULL DEFAULT '',
`avatar` varchar(255) NOT NULL DEFAULT '' COMMENT '用户头像',
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
`delete_status` varchar(1) DEFAULT '1' COMMENT '是否有效 1有效 2无效',
`create_user` varchar(255) NOT NULL DEFAULT '',
`update_user` varchar(255) NOT NULL DEFAULT '',
`last_change_password_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `username_uindex` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='用户表';

LOCK TABLES `sys_user` WRITE;
INSERT INTO `sys_user` (`id`, `username`, `password`, `nickname`, `phone`, `email`, `avatar`, `create_time`,
`update_time`, `delete_status`, `create_user`, `update_user`, `last_change_password_date`)
VALUES (10003, 'admin', '123456', '超级用户', '', '', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
'2017-10-30 03:52:38', '2022-04-21 08:37:25', '1', 'admin', 'admin', '2022-04-15 10:29:31'),
(10004, 'user', '123456', '莎士比亚😘', '', '',
'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', '2017-10-30 08:13:02',
'2022-04-21 08:37:25', '1', 'admin', 'admin', '2022-04-15 10:29:31'),
(10005, 'aaa', '123456', '🌹', '', '', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
'2017-11-15 06:02:56', '2022-04-21 08:37:25', '1', 'admin', 'admin', '2022-04-15 10:29:31'),
(10007, 'test', '123456', '就看看列表', '', '', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
'2017-11-22 08:29:41', '2022-04-21 08:37:25', '2', 'admin', 'admin', '2022-04-15 10:29:31'),
(10008, 'sa', '1', '😯', '', '', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
'2021-05-08 15:21:54', '2022-04-21 08:37:25', '1', 'admin', 'admin', '2022-04-15 10:29:31'),
(10009, 'a1', '123456', 'a123', '', '', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
'2022-02-22 06:40:59', '2022-04-21 08:37:25', '1', 'admin', 'admin', '2022-04-15 10:29:31'),
(10010, 'a2', NULL, NULL, '', '', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
'2022-02-22 06:40:59', '2022-04-21 08:37:25', '1', 'admin', 'admin', '2022-04-15 10:29:31'),
(10011, 'a3', NULL, NULL, '', '', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
'2022-02-22 06:40:59', '2022-04-21 08:37:25', '1', 'admin', 'admin', '2022-04-15 10:29:31'),
(10012, 'a4', NULL, NULL, '', '', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
'2022-02-22 06:41:00', '2022-04-21 08:37:25', '1', 'admin', 'admin', '2022-04-15 10:29:31'),
(10013, 'a5', NULL, NULL, '', '', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
'2022-02-22 06:41:00', '2022-04-21 08:37:25', '2', 'admin', 'admin', '2022-04-15 10:29:31'),
(10014, 'a6', NULL, NULL, '', '', 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
'2022-02-22 06:41:00', '2022-04-21 08:37:25', '2', 'admin', 'admin', '2022-04-15 10:29:31'),
(10026, 'abc123', '123456', 'abc123', '18989898990', 'abc@qq.com',
'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', '2022-04-20 07:27:20',
'2022-04-21 10:00:55', '1', '', 'admin', '2022-04-20 07:27:19');
UNLOCK TABLES;


DROP TABLE IF EXISTS `sys_user_role`;
CREATE TABLE `sys_user_role` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int NOT NULL COMMENT '用户id',
`role_id` int DEFAULT NULL COMMENT '角色id',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='用户-角色关联表';


LOCK TABLES `sys_user_role` WRITE;
INSERT INTO `sys_user_role` (`id`, `user_id`, `role_id`)
VALUES (3, 10004, 2),
(4, 10004, 3),
(9, 10005, 1),
(10, 10005, 2),
(11, 10008, 4),
(12, 10003, 1),
(13, 10007, 1),
(17, 10009, 4),
(29, 10026, 4);
UNLOCK TABLES;

创建好数据库

1
mysql> create database template;

运行第一次

1
mvnd8 clean spring-boot:run

看到以下日志结果,说明我们的数据库已经初始化完成,也可以在其他界面工具中查看。

1
[INFO] [stdout] 2022-03-01 18:28:30.609  INFO 78535 --- [  restartedMain] o.f.core.internal.command.DbMigrate      : Successfully applied 1 migration to schema `template`, now at version v1.0 (execution time 00:00.369s)

访问http://localhost:8080/swagger-ui/index.html,可以看到swaggerui的自动支持,只是目前我们没有任何接口。

参考:

基于SpringBoot-shiro-vue的权限管理思路