教你怎么用SpringBoot+Mybati-Plus快速搭建代码
目录
- 前言
- 一、搭建一个springboot项目
- 二、配置依赖
- 三、配置application.yml文件
- 四、启动类
- 五、运行main方法
- 六、项目结构
前言 先放一个官网吧,其实本案例就是根据官网案例来的,只是进行了修改配置。
Mybatis-plus官网
一、搭建一个springboot项目 这一步应该不用多说了,使用idea可以非常方便的就搭建出来了。
二、配置依赖
4.0.0 org.springframework.boot spring-boot-starter-parent2.5.0 com.xxx mybatis-plus0.0.1-SNAPSHOT mybatis-plus Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-weborg.projectlombok lomboktrue mysql mysql-connector-javaruntimecom.baomidou mybatis-plus-boot-starter3.3.1 com.baomidou mybatis-plus-generator3.3.0 org.freemarker freemarker2.3.31 org.springframework.boot spring-boot-maven-plugin
三、配置application.yml文件
#我使用的是mysql8.0,所以驱动书写有点区别。spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/school?useUnicode=true&useSSL=false&characterEncoding=utf8username: rootpassword: 自己数据库密码
【教你怎么用SpringBoot+Mybati-Plus快速搭建代码】
四、启动类 根据官网,我们需要写一个启动类来生成这些文件
package com.xxx.mybatisplus.generator; import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.generator.AutoGenerator; import com.baomidou.mybatisplus.generator.InjectionConfig; import com.baomidou.mybatisplus.generator.config.*; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy; import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class CodeGenerator {/*** * 读取控制台内容*
*/public static String scanner(String tip) {Scanner scanner = new Scanner(System.in); StringBuilder help = new StringBuilder(); help.append("请输入" + tip + ":"); System.out.println(help.toString()); if (scanner.hasNext()) {String ipt = scanner.next(); if (StringUtils.isNotBlank(ipt)) {return ipt; }}throw new MybatisPlusException("请输入正确的" + tip + "!"); }public static void main(String[] args) {// 代码生成器AutoGenerator mpg = new AutoGenerator(); // 全局配置GlobalConfig gc = new GlobalConfig(); String projectPath = System.getProperty("user.dir"); gc.setOutputDir(projectPath + "/src/main/java"); //作者gc.setAuthor("程序逸"); //打开输出目录gc.setOpen(false); // gc.setSwagger2(true); 实体属性 Swagger2 注解mpg.setGlobalConfig(gc); // 数据源配置DataSourceConfig dsc = new DataSourceConfig(); dsc.setUrl("jdbc:mysql://localhost:3306/school?useUnicode=true&useSSL=false&characterEncoding=utf8"); // dsc.setSchemaName("public"); dsc.setDriverName("com.mysql.cj.jdbc.Driver"); dsc.setUsername("root"); dsc.setPassword("自己密码"); mpg.setDataSource(dsc); // 包配置PackageConfig pc = new PackageConfig(); //pc.setModuleName(scanner("模块名")); pc.setParent("com.xxx.mybatisplus").setEntity("pojo").setMapper("mapper").setService("service").setServiceImpl("serviceimpl").setController("controller"); mpg.setPackageInfo(pc); // 自定义配置InjectionConfig cfg = new InjectionConfig() {@Overridepublic void initMap() {// to do nothing}}; // 如果模板引擎是 freemarkerString templatePath = "/templates/mapper.xml.ftl"; // 如果模板引擎是 velocity// String templatePath = "/templates/mapper.xml.vm"; // 自定义输出配置ListfocList = new ArrayList<>(); // 自定义配置会被优先输出focList.add(new FileOutConfig(templatePath) {@Overridepublic String outputFile(TableInfo tableInfo) {// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML; }}); /*cfg.setFileCreate(new IFileCreate() {@Overridepublic boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {// 判断自定义文件夹是否需要创建checkDir("调用默认方法创建的目录,自定义目录用"); if (fileType == FileType.MAPPER) {// 已经生成 mapper 文件判断存在,不想重新生成返回 falsereturn !new File(filePath).exists(); }// 允许生成模板文件return true; }}); */cfg.setFileOutConfigList(focList); mpg.setCfg(cfg); // 配置模板TemplateConfig templateConfig = new TemplateConfig(); // 配置自定义输出模板//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别// templateConfig.setEntity("templates/entity2.java"); // templateConfig.setService(); // templateConfig.setController(); templateConfig.setXml(null); mpg.setTemplate(templateConfig); // 策略配置StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true); strategy.setRestControllerStyle(true); // 写于父类中的公共字段strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); strategy.setControllerMappingHyphenStyle(true); strategy.setTablePrefix("t_"); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine()); mpg.execute(); }}
五、运行main方法 运行该main方法,然后在控制台输入你想生成的表,用,隔开。
![教你怎么用SpringBoot+Mybati-Plus快速搭建代码](https://img.it610.com/image/info11/8684b33009424825813c15ad7540e91b.jpg)
文章图片
六、项目结构
![教你怎么用SpringBoot+Mybati-Plus快速搭建代码](https://img.it610.com/image/info11/54224098ede646b7810bac2cd1bb7383.jpg)
文章图片
到此这篇关于教你怎么用SpringBoot+Mybati-Plus快速搭建代码的文章就介绍到这了,更多相关SpringBoot+Mybati-Plus搭建代码内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
推荐阅读
- Docker应用:容器间通信与Mariadb数据库主从复制
- 遇到一哭二闹三打滚的孩子,怎么办┃山伯教育
- JS中的各种宽高度定义及其应用
- 七年之痒之后
- 由浅入深理解AOP
- 开花店的前景怎么样()
- 【译】20个更有效地使用谷歌搜索的技巧
- 涉毒患者(新诗)
- 参保人员因患病来不及到指定的医疗机构就医,能否报销医疗费用()
- mybatisplus如何在xml的连表查询中使用queryWrapper