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

冷不防 2023-08-17 15:30 221阅读 0赞

一:添加所需依赖

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

  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. <dependency>
  9. <groupId>tk.mybatis</groupId>
  10. <artifactId>mapper-generator</artifactId>
  11. <version>1.0.0</version>
  12. </dependency>
  13. <!--常用库依赖 -->
  14. <dependency>
  15. <groupId>org.apache.commons</groupId>
  16. <artifactId>commons-lang3</artifactId>
  17. <version>3.5</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>com.google.guava</groupId>
  21. <artifactId>guava</artifactId>
  22. <version>22.0</version>
  23. </dependency>

二:创建系统日志表

  1. CREATE 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='系统日志表';

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

创建core→constant→ProjectConstant

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

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

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

五:功能测试

CodeGenerator中右键run

format_png

format_png 1

ok,创建成功

发表评论

表情:
评论列表 (有 0 条评论,221人围观)

还没有评论,来说两句吧...

相关阅读