从零搭建自己的SpringBoot后台框架(九)

释放双眼,带上耳机,听听看~!

一:添加所需依赖

由于我们上篇文章讲过使用通用mapper,所以这里我们除了需要添加mybatis-generator之外还需要添加通用mapper提供的generator依赖,其次我们还需要引入两个常用jar包


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
1<!--代码生成器-->
2<dependency>
3   <groupId>org.mybatis.generator</groupId>
4   <artifactId>mybatis-generator-core</artifactId>
5   <version>1.3.5</version>
6   <scope>test</scope>
7</dependency>
8
9<dependency>
10   <groupId>tk.mybatis</groupId>
11   <artifactId>mapper-generator</artifactId>
12   <version>1.0.0</version>
13</dependency>
14
15<!--常用库依赖 -->
16<dependency>
17   <groupId>org.apache.commons</groupId>
18   <artifactId>commons-lang3</artifactId>
19   <version>3.5</version>
20</dependency>
21<dependency>
22   <groupId>com.google.guava</groupId>
23   <artifactId>guava</artifactId>
24   <version>22.0</version>
25</dependency>
26

二:创建系统日志表


1
2
3
4
5
6
7
8
9
10
11
12
13
14
1CREATE TABLE `system_log` (
2  `id` varchar(32) NOT NULL,
3  `description` varchar(50) DEFAULT NULL COMMENT '日志信息描述',
4  `method` varchar(20) DEFAULT NULL COMMENT '方法名称',
5  `log_type` varchar(10) DEFAULT NULL COMMENT '日志类型 0是正常,1是异常',
6  `request_ip` varchar(30) DEFAULT NULL COMMENT '请求的ip',
7  `exception_code` varchar(50) DEFAULT NULL COMMENT '异常错误码',
8  `exception_detail` varchar(255) DEFAULT NULL COMMENT '异常详情',
9  `params` varchar(1000) DEFAULT NULL COMMENT '请求参数',
10  `user_id` varchar(32) DEFAULT NULL COMMENT '请求的用户id',
11  `create_time` datetime DEFAULT NULL,
12  PRIMARY KEY (`id`)
13) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统日志表';
14

三:创建系统常用变量文件夹

创建core→constant→ProjectConstant


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
1package com.example.demo.core.constant;
2
3public class ProjectConstant {
4
5   // 项目基础包名称
6   public static final String BASE_PACKAGE = "com.example.demo";
7
8   // Model所在包
9   public static final String MODEL_PACKAGE = BASE_PACKAGE + ".model";
10
11   // Mapper所在包
12   public static final String MAPPER_PACKAGE = BASE_PACKAGE + ".dao";
13
14   // Service所在包
15   public static final String SERVICE_PACKAGE = BASE_PACKAGE + ".service";
16
17   // ServiceImpl所在包
18   public static final String SERVICE_IMPL_PACKAGE = SERVICE_PACKAGE + ".impl";
19
20   // Controller所在包
21   public static final String CONTROLLER_PACKAGE = BASE_PACKAGE + ".controller";
22
23   // Mapper插件基础接口的完全限定名
24   public static final String MAPPER_INTERFACE_REFERENCE = BASE_PACKAGE + ".core.universal.Mapper";
25
26}
27

四:创建src\test\java\com\example\demo\CodeGenerator.java


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
1package com.example.demo;
2
3import com.example.demo.core.constant.ProjectConstant;
4import com.google.common.base.CaseFormat;
5import org.apache.commons.lang3.StringUtils;
6import org.mybatis.generator.api.MyBatisGenerator;
7import org.mybatis.generator.config.*;
8import org.mybatis.generator.internal.DefaultShellCallback;
9
10import java.util.ArrayList;
11import java.util.List;
12
13/**
14 * @Description: 代码生成器,根据数据表名称生成对应的Model、Mapper简化开发。
15 * @author phubing
16 * @date 2019/4/23 20:28
17 */
18public class CodeGenerator {
19
20   // JDBC配置,请修改为你项目的实际配置
21   private static final String JDBC_URL = "jdbc:mysql://localhost:3333/demo";
22   private static final String JDBC_USERNAME = "root";
23   private static final String JDBC_PASSWORD = "123456";
24   private static final String JDBC_DIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
25   private static final String JAVA_PATH = "src/main/java"; // java文件路径
26   private static final String RESOURCES_PATH = "src/main/resources";// 资源文件路径
27
28   /**
29    * genCode("输入表名","输入自定义Model名称");
30    * 如果想创建所有表,请输入"%"
31    * @param args
32    */
33   public static void main(String[] args) {
34      genCode("system_log");
35   }
36
37   /**
38    * 通过数据表名称生成代码,Model 名称通过解析数据表名称获得,下划线转大驼峰的形式。 如输入表名称 "t_user_detail" 将生成
39    * TUserDetail、TUserDetailMapper、TUserDetailService ...
40    *
41    * @param tableNames
42    *            数据表名称...
43    */
44   public static void genCode(String... tableNames) {
45      for (String tableName : tableNames) {
46         genCode(tableName, null);
47      }
48   }
49
50   /**
51    * 通过数据表名称,和自定义的 Model 名称生成代码 如输入表名称 "t_user_detail" 和自定义的 Model 名称 "User"
52    * 将生成 User、UserMapper、UserService ...
53    *
54    * @param tableName
55    *            数据表名称
56    * @param modelName
57    *            自定义的 Model 名称
58    */
59   public static void genCode(String tableName, String modelName) {
60      genModelAndMapper(tableName, modelName);
61   }
62
63   public static void genModelAndMapper(String tableName, String modelName) {
64      Context context = getContext();
65
66      JDBCConnectionConfiguration jdbcConnectionConfiguration = getJDBCConnectionConfiguration();
67      context.setJdbcConnectionConfiguration(jdbcConnectionConfiguration);
68
69      PluginConfiguration pluginConfiguration = getPluginConfiguration();
70      context.addPluginConfiguration(pluginConfiguration);
71
72      JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = getJavaModelGeneratorConfiguration();
73      context.setJavaModelGeneratorConfiguration(javaModelGeneratorConfiguration);
74
75      SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = getSqlMapGeneratorConfiguration();
76      context.setSqlMapGeneratorConfiguration(sqlMapGeneratorConfiguration);
77
78      JavaClientGeneratorConfiguration javaClientGeneratorConfiguration =getJavaClientGeneratorConfiguration();
79      context.setJavaClientGeneratorConfiguration(javaClientGeneratorConfiguration);
80
81      TableConfiguration tableConfiguration = new TableConfiguration(context);
82      tableConfiguration.setTableName(tableName);
83      tableConfiguration.setDomainObjectName(modelName);
84      context.addTableConfiguration(tableConfiguration);
85
86      List<String> warnings;
87      MyBatisGenerator generator;
88      try {
89         Configuration config = new Configuration();
90         config.addContext(context);
91         config.validate();
92         boolean overwrite = true;
93         DefaultShellCallback callback = new DefaultShellCallback(overwrite);
94         warnings = new ArrayList<>();
95         generator = new MyBatisGenerator(config, callback, warnings);
96         generator.generate(null);
97      } catch (Exception e) {
98         throw new RuntimeException("生成Model和Mapper失败", e);
99      }
100
101      if (generator.getGeneratedJavaFiles().isEmpty() || generator.getGeneratedXmlFiles().isEmpty()) {
102         throw new RuntimeException("生成Model和Mapper失败:" + warnings);
103      }
104      if (StringUtils.isEmpty(modelName)){
105         modelName = tableNameConvertUpperCamel(tableName);
106      }
107      System.out.println(modelName + ".java 生成成功");
108      System.out.println(modelName + "Mapper.java 生成成功");
109      System.out.println(modelName + "Mapper.xml 生成成功");
110   }
111
112   private static Context getContext(){
113      Context context = new Context(ModelType.FLAT);
114      context.setId("Potato");
115      context.setTargetRuntime("MyBatis3Simple");
116      context.addProperty(PropertyRegistry.CONTEXT_BEGINNING_DELIMITER, "`");
117      context.addProperty(PropertyRegistry.CONTEXT_ENDING_DELIMITER, "`");
118      return context;
119   }
120
121   private static JDBCConnectionConfiguration getJDBCConnectionConfiguration(){
122      JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
123      jdbcConnectionConfiguration.setConnectionURL(JDBC_URL);
124      jdbcConnectionConfiguration.setUserId(JDBC_USERNAME);
125      jdbcConnectionConfiguration.setPassword(JDBC_PASSWORD);
126      jdbcConnectionConfiguration.setDriverClass(JDBC_DIVER_CLASS_NAME);
127      return jdbcConnectionConfiguration;
128   }
129
130   private static PluginConfiguration getPluginConfiguration(){
131      PluginConfiguration pluginConfiguration = new PluginConfiguration();
132      pluginConfiguration.setConfigurationType("tk.mybatis.mapper.generator.MapperPlugin");
133      pluginConfiguration.addProperty("mappers", ProjectConstant.MAPPER_INTERFACE_REFERENCE);
134      return pluginConfiguration;
135   }
136
137   private static JavaModelGeneratorConfiguration getJavaModelGeneratorConfiguration(){
138      JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
139      javaModelGeneratorConfiguration.setTargetProject(JAVA_PATH);
140      javaModelGeneratorConfiguration.setTargetPackage(ProjectConstant.MODEL_PACKAGE);
141      javaModelGeneratorConfiguration.addProperty("enableSubPackages","true");
142      javaModelGeneratorConfiguration.addProperty("trimStrings","true");
143      return javaModelGeneratorConfiguration;
144   }
145
146   private static SqlMapGeneratorConfiguration getSqlMapGeneratorConfiguration(){
147      SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
148      sqlMapGeneratorConfiguration.setTargetProject(RESOURCES_PATH);
149      sqlMapGeneratorConfiguration.setTargetPackage("mapper");
150      return sqlMapGeneratorConfiguration;
151   }
152
153   private static JavaClientGeneratorConfiguration getJavaClientGeneratorConfiguration(){
154      JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
155      javaClientGeneratorConfiguration.setTargetProject(JAVA_PATH);
156      javaClientGeneratorConfiguration.setTargetPackage(ProjectConstant.MAPPER_PACKAGE);
157      javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
158      return javaClientGeneratorConfiguration;
159   }
160
161   private static String tableNameConvertUpperCamel(String tableName) {
162      return CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName.toLowerCase());
163   }
164}
165

五:功能测试

在CodeGenerator中右键run

ok,创建成功

给TA打赏
共{{data.count}}人
人已打赏
安全技术

c++ list, vector, map, set 区别与用法比较

2022-1-11 12:36:11

安全经验

Canonical 内核安全更新,相关 Ubuntu 版本应尽快升级

2018-12-24 11:12:22

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索