SpringBoot整合MybatisPlus【超详细】

小鱼儿 2022-11-17 10:25 430阅读 0赞

SpringBoot整合MybatisPlus【超详细】

  • 创建个SpringBoot项目
    • 写个HelloController测试下
    • 使用代码生成器生成代码
    • 添加所需的依赖
    • CodeGenerator
    • 运行代码生成器,在控制台输入想要生成的表

创建个SpringBoot项目

勾选生所需的依赖:
在这里插入图片描述
我把application的后缀改为.yml了,方便些。
在这里插入图片描述
pom.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.4.4</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.keafmd</groupId>
  12. <artifactId>springboot-mybatisplus</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springboot-mybatisplus</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-jdbc</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-devtools</artifactId>
  31. <scope>runtime</scope>
  32. <optional>true</optional>
  33. </dependency>
  34. <dependency>
  35. <groupId>mysql</groupId>
  36. <artifactId>mysql-connector-java</artifactId>
  37. <scope>runtime</scope>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.projectlombok</groupId>
  41. <artifactId>lombok</artifactId>
  42. <optional>true</optional>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.springframework.boot</groupId>
  46. <artifactId>spring-boot-starter-test</artifactId>
  47. <scope>test</scope>
  48. </dependency>
  49. </dependencies>
  50. <build>
  51. <plugins>
  52. <plugin>
  53. <groupId>org.springframework.boot</groupId>
  54. <artifactId>spring-boot-maven-plugin</artifactId>
  55. <configuration>
  56. <excludes>
  57. <exclude>
  58. <groupId>org.projectlombok</groupId>
  59. <artifactId>lombok</artifactId>
  60. </exclude>
  61. </excludes>
  62. </configuration>
  63. </plugin>
  64. </plugins>
  65. </build>
  66. </project>

因为我们配置了数据源,所以需要在application.yml中配置下数据源,不然会起不来,我顺便也改了下端口。

application.yml:

  1. server:
  2. port: 80
  3. spring:
  4. datasource:
  5. url: jdbc:mysql://127.0.0.1:3306/ssm-java1?useSSL=false&&characterEncoding=UTF-8
  6. driver-class-name: com.mysql.cj.jdbc.Driver
  7. username: root
  8. password: 18044229

写个HelloController测试下

HelloController:

  1. package com.keafmd.controller;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. /** * Keafmd * * @ClassName: HelloController * @Description: * @author: 牛哄哄的柯南 * @Date: 2021-04-09 11:11 * @Blog: https://keafmd.blog.csdn.net/ */
  5. @RestController
  6. public class HelloController {
  7. @RequestMapping("/hello")
  8. public String hello(){
  9. return "keafmd";
  10. }
  11. }

运行启动类,访问:http://127.0.0.1/hello
在这里插入图片描述
到此证明SpringBoot没有问题。

使用代码生成器生成代码

添加所需的依赖

pom.xml中添加以下依赖:

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-generator</artifactId>
  4. <scope>test</scope>
  5. <version>3.4.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.freemarker</groupId>
  9. <artifactId>freemarker</artifactId>
  10. <scope>test</scope>
  11. <version>2.3.31</version>
  12. </dependency>

由于代码生成器并不会在生产环境使用,只是在开发环境中使用了下。所以我们把代码生成器写在test包中即可,依赖的使用场景也定义成test即可。

CodeGenerator

CodeGenerator:

  1. package com.keafmd.mp;
  2. import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
  3. import com.baomidou.mybatisplus.core.toolkit.StringPool;
  4. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  5. import com.baomidou.mybatisplus.generator.AutoGenerator;
  6. import com.baomidou.mybatisplus.generator.InjectionConfig;
  7. import com.baomidou.mybatisplus.generator.config.*;
  8. import com.baomidou.mybatisplus.generator.config.po.TableInfo;
  9. import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
  10. import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Scanner;
  14. /** * Keafmd * * @ClassName: CodeGenerator * @Description: * @author: 牛哄哄的柯南 * @date: 2021-03-23 21:47 */
  15. // 演示例子,执行 main 方法控制台输入模块表名回车自动生成对应项目目录中
  16. public class CodeGenerator {
  17. /** * <p> * 读取控制台内容 * </p> */
  18. public static String scanner(String tip) {
  19. Scanner scanner = new Scanner(System.in);
  20. StringBuilder help = new StringBuilder();
  21. help.append("请输入" + tip + ":");
  22. System.out.println(help.toString());
  23. if (scanner.hasNext()) {
  24. String ipt = scanner.next();
  25. if (StringUtils.isNotBlank(ipt)) {
  26. return ipt;
  27. }
  28. }
  29. throw new MybatisPlusException("请输入正确的" + tip + "!");
  30. }
  31. public static void main(String[] args) {
  32. // 代码生成器
  33. AutoGenerator mpg = new AutoGenerator();
  34. // 全局配置
  35. GlobalConfig gc = new GlobalConfig();
  36. String projectPath = System.getProperty("user.dir");
  37. // System.out.println("projectPath = " + projectPath);
  38. gc.setOutputDir(projectPath + "/src/main/java");
  39. // gc.setOutputDir("D:\\test");
  40. gc.setAuthor("关注公众号:牛哄哄的柯南");
  41. gc.setOpen(false);
  42. // gc.setSwagger2(true); 实体属性 Swagger2 注解
  43. gc.setServiceName("%sService");
  44. mpg.setGlobalConfig(gc);
  45. // 数据源配置
  46. DataSourceConfig dsc = new DataSourceConfig();
  47. dsc.setUrl("jdbc:mysql://localhost:3306/ssm-java1?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC");
  48. // dsc.setSchemaName("public");
  49. dsc.setDriverName("com.mysql.cj.jdbc.Driver");
  50. dsc.setUsername("root");
  51. dsc.setPassword("18044229");
  52. mpg.setDataSource(dsc);
  53. // 包配置
  54. PackageConfig pc = new PackageConfig();
  55. pc.setModuleName(null);
  56. pc.setParent("com.keafmd");
  57. mpg.setPackageInfo(pc);
  58. // 自定义配置
  59. InjectionConfig cfg = new InjectionConfig() {
  60. @Override
  61. public void initMap() {
  62. // to do nothing
  63. }
  64. };
  65. // 如果模板引擎是 freemarker
  66. String templatePath = "/templates/mapper.xml.ftl";
  67. // 如果模板引擎是 velocity
  68. // String templatePath = "/templates/mapper.xml.vm";
  69. // 自定义输出配置
  70. List<FileOutConfig> focList = new ArrayList<>();
  71. // 自定义配置会被优先输出
  72. focList.add(new FileOutConfig(templatePath) {
  73. @Override
  74. public String outputFile(TableInfo tableInfo) {
  75. // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
  76. return projectPath + "/src/main/resources/mapper/"
  77. + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
  78. }
  79. });
  80. cfg.setFileOutConfigList(focList);
  81. mpg.setCfg(cfg);
  82. // 配置模板
  83. TemplateConfig templateConfig = new TemplateConfig();
  84. templateConfig.setXml(null);
  85. mpg.setTemplate(templateConfig);
  86. // 策略配置
  87. StrategyConfig strategy = new StrategyConfig();
  88. strategy.setNaming(NamingStrategy.underline_to_camel);
  89. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
  90. strategy.setEntityLombokModel(true);
  91. strategy.setRestControllerStyle(true);
  92. strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
  93. strategy.setControllerMappingHyphenStyle(true);
  94. strategy.setTablePrefix("m_");
  95. mpg.setStrategy(strategy);
  96. mpg.setTemplateEngine(new FreemarkerTemplateEngine());
  97. mpg.execute();
  98. }
  99. }

运行代码生成器,在控制台输入想要生成的表

在这里插入图片描述

这样就会生成一些包及相应的代码,注意CodeGenerator中的相关代码(如数据库的,包名的)需要该成你们需要的。

在这里插入图片描述

以上就是SpringBoot整合MybatisPlus【超详细】的全部内容。

看完如果对你有帮助,感谢点赞支持!
如果你是电脑端的话,看到右下角的 “一键三连” 了吗,没错点它[哈哈]

在这里插入图片描述

加油!

共同努力!

Keafmd

发表评论

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

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

相关阅读