资讯专栏INFORMATION COLUMN

SpringBoot整合MybatisPlus的简单教程(简单整合)

duan199226 / 1844人阅读

摘要:最近在研究,顺便就会看看数据库连接这一块的知识,所以当我发现有通用和这两款网络上比较火的简化开发的优秀软件之后。先创建一个的项目,可以参考我之前的文章的简单教程一项目的创建。打开文件,将最新的相关的包都引用进来。

</>复制代码

  1. 最近在研究springboot,顺便就会看看数据库连接这一块的知识 ,所以当我发现有通用Mapper和MybatisPlus这两款网络上比较火的简化mybatis开发的优秀软件之后。就都想试一下,看看哪一款比较适合自己。

先创建一个springboot的项目,可以参考我之前的文章Spring Boot 的简单教程(一) Spring Boot 项目的创建。

创建好springboot之后就需要整合mybatis和mybatis-plus了。
打开pom.xml文件,将最新的mybatis相关的包都引用进来。

</>复制代码

  1. mysql
  2. mysql-connector-java
  3. runtime
  4. org.projectlombok
  5. lombok
  6. true
  7. com.baomidou
  8. mybatis-plus-boot-starter
  9. 3.1.1
  10. com.baomidou
  11. mybatis-plus-generator
  12. 3.1.1
  13. org.freemarker
  14. freemarker
  15. 2.3.28

需要对application.yml进行相关的配置。

</>复制代码

  1. #端口号
  2. server:
  3. port: 8088
  4. #数据库的配置信息
  5. spring:
  6. datasource:
  7. url: jdbc:mysql://localhost:3306/*** #自己的数据库名称
  8. username: root
  9. password: 123456
  10. mybatis:
  11. #开启驼峰命名法
  12. configuration:
  13. map-underscore-to-camel-case: true
  14. mybatis-plus:
  15. # xml地址
  16. mapper-locations: classpath:mapper/*Mapper.xml
  17. # 实体扫描,多个package用逗号或者分号分隔
  18. type-aliases-package: *** #自己的实体类地址
  19. configuration:
  20. # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
  21. log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

自动生成模块的方法,在相应的位置上添加上自己的一些包名就可以运行生成相应的Entity、Mapper、Mapper XML、Service、Controller 等各个模块的代码。

</>复制代码

  1. public class CodeGenerator {
  2. /**
  3. *

  4. * 读取控制台内容
  5. *

  6. */
  7. public static String scanner(String tip) {
  8. Scanner scanner = new Scanner(System.in);
  9. StringBuilder help = new StringBuilder();
  10. help.append("请输入" + tip + ":");
  11. System.out.println(help.toString());
  12. if (scanner.hasNext()) {
  13. String ipt = scanner.next();
  14. if (StringUtils.isNotEmpty(ipt)) {
  15. return ipt;
  16. }
  17. }
  18. throw new MybatisPlusException("请输入正确的" + tip + "!");
  19. }
  20. public static void main(String[] args) {
  21. // 代码生成器
  22. AutoGenerator mpg = new AutoGenerator();
  23. // 全局配置
  24. GlobalConfig gc = new GlobalConfig();
  25. String projectPath = System.getProperty("user.dir");
  26. gc.setOutputDir(projectPath + "/src/main/java");
  27. gc.setAuthor("jobob");
  28. gc.setOpen(false);
  29. // gc.setSwagger2(true); 实体属性 Swagger2 注解
  30. mpg.setGlobalConfig(gc);
  31. // 数据源配置
  32. DataSourceConfig dsc = new DataSourceConfig();
  33. dsc.setUrl("jdbc:mysql://localhost:3306/***?useUnicode=true&useSSL=false&characterEncoding=utf8");
  34. // dsc.setSchemaName("public");
  35. dsc.setDriverName("com.mysql.cj.jdbc.Driver");
  36. dsc.setUsername("root");
  37. dsc.setPassword("***");
  38. mpg.setDataSource(dsc);
  39. // 包配置
  40. PackageConfig pc = new PackageConfig();
  41. //这里有个模块名的配置,可以注释掉不用。
  42. // pc.setModuleName(scanner("模块名"));
  43. pc.setParent("com.zhouxiaoxi.www");
  44. mpg.setPackageInfo(pc);
  45. // 自定义配置
  46. InjectionConfig cfg = new InjectionConfig() {
  47. @Override
  48. public void initMap() {
  49. // to do nothing
  50. }
  51. };
  52. // 如果模板引擎是 freemarker
  53. String templatePath = "/templates/mapper.xml.ftl";
  54. // 如果模板引擎是 velocity
  55. // String templatePath = "/templates/mapper.xml.vm";
  56. // 自定义输出配置
  57. List focList = new ArrayList<>();
  58. // 自定义配置会被优先输出
  59. focList.add(new FileOutConfig(templatePath) {
  60. @Override
  61. public String outputFile(TableInfo tableInfo) {
  62. // 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
  63. return projectPath + "/src/main/resources/mapper/"
  64. // + + pc.getModuleName() + 如果放开上面的模块名,这里就有一个模块名了
  65. + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
  66. }
  67. });
  68. /*
  69. cfg.setFileCreate(new IFileCreate() {
  70. @Override
  71. public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
  72. // 判断自定义文件夹是否需要创建
  73. checkDir("调用默认方法创建的目录");
  74. return false;
  75. }
  76. });
  77. */
  78. cfg.setFileOutConfigList(focList);
  79. mpg.setCfg(cfg);
  80. // 配置模板
  81. TemplateConfig templateConfig = new TemplateConfig();
  82. // 配置自定义输出模板
  83. //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
  84. // templateConfig.setEntity("templates/entity2.java");
  85. // templateConfig.setService();
  86. // templateConfig.setController();
  87. templateConfig.setXml(null);
  88. mpg.setTemplate(templateConfig);
  89. // 策略配置
  90. StrategyConfig strategy = new StrategyConfig();
  91. //数据库表映射到实体的明明策略
  92. strategy.setNaming(NamingStrategy.underline_to_camel);
  93. //数据库表字段映射到实体的命名策略, 未指定按照 naming 执行
  94. strategy.setColumnNaming(NamingStrategy.underline_to_camel);
  95. //自定义继承的Entity类全称,带包名
  96. // strategy.setSuperEntityClass("***");
  97. strategy.setEntityLombokModel(true);
  98. strategy.setRestControllerStyle(true);
  99. //自定义继承的Controller类全称,带包名
  100. // strategy.setSuperControllerClass("***");
  101. strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
  102. //自定义基础的Entity类,公共字段(可添加更多)
  103. // strategy.setSuperEntityColumns("id");
  104. //驼峰转连字符
  105. strategy.setControllerMappingHyphenStyle(true);
  106. //表前缀
  107. // strategy.setTablePrefix(pc.getModuleName() + "_");
  108. mpg.setStrategy(strategy);
  109. mpg.setTemplateEngine(new FreemarkerTemplateEngine());
  110. mpg.execute();
  111. }
  112. }

在生成的controller里面添加对应的方法启动就可以正常进行访问了。

当然还需要在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:

</>复制代码

  1. @SpringBootApplication
  2. @MapperScan("***.*.mapper") //对应你的mapper存放的地址
  3. public class Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(QuickStartApplication.class, args);
  6. }
  7. }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77666.html

相关文章

  • springboot系列】springboot整合独立模块Druid + mybatis-plus

    摘要:申请连接时执行检测连接是否有效,做了这个配置会降低性能。作者在版本中使用,通过监控界面发现有缓存命中率记录,该应该是支持。允许和不允许单条语句返回多个数据集取决于驱动需求使用列标签代替列名称。需要驱动器支持。将自动映射所有复杂的结果。 项目github地址:https://github.com/5-Ason/aso... 具体可看 ./db/db-mysql 模块 本文主要实现的是对...

    RobinTang 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<