|
| 1 | +#基于`SpringBoot`集成`Mybatis-Plus`实现代码生成器 |
| 2 | + |
| 3 | +##1. 引入所需依赖 |
| 4 | + |
| 5 | +```xml |
| 6 | + <dependencies> |
| 7 | + <dependency> |
| 8 | + <groupId>org.springframework.boot</groupId> |
| 9 | + <artifactId>spring-boot-starter-web</artifactId> |
| 10 | + </dependency> |
| 11 | + |
| 12 | + <dependency> |
| 13 | + <groupId>org.springframework.boot</groupId> |
| 14 | + <artifactId>spring-boot-starter-test</artifactId> |
| 15 | + <scope>test</scope> |
| 16 | + </dependency> |
| 17 | + |
| 18 | +<!--lombok工具--> |
| 19 | + <dependency> |
| 20 | + <groupId>org.projectlombok</groupId> |
| 21 | + <artifactId>lombok</artifactId> |
| 22 | + <optional>true</optional> |
| 23 | + </dependency> |
| 24 | + |
| 25 | +<!--mybatis-plus--> |
| 26 | + <dependency> |
| 27 | + <groupId>com.baomidou</groupId> |
| 28 | + <artifactId>mybatis-plus-boot-starter</artifactId> |
| 29 | + <version>3.3.2</version> |
| 30 | + </dependency> |
| 31 | + |
| 32 | +<!--代码生成器--> |
| 33 | + <dependency> |
| 34 | + <groupId>com.baomidou</groupId> |
| 35 | + <artifactId>mybatis-plus-generator</artifactId> |
| 36 | + <version>3.3.2</version> |
| 37 | + </dependency> |
| 38 | + |
| 39 | +<!--逆向工程需要模板引擎--> |
| 40 | + <dependency> |
| 41 | + <groupId>org.freemarker</groupId> |
| 42 | + <artifactId>freemarker</artifactId> |
| 43 | + <version>2.3.30</version> |
| 44 | + </dependency> |
| 45 | + |
| 46 | +<!--mysql依赖--> |
| 47 | + <dependency> |
| 48 | + <groupId>mysql</groupId> |
| 49 | + <artifactId>mysql-connector-java</artifactId> |
| 50 | + <scope>runtime</scope> |
| 51 | + </dependency> |
| 52 | + |
| 53 | +<!--连接池--> |
| 54 | + <dependency> |
| 55 | + <groupId>com.alibaba</groupId> |
| 56 | + <artifactId>druid</artifactId> |
| 57 | + <version>1.1.20</version> |
| 58 | + </dependency> |
| 59 | + </dependencies> |
| 60 | +``` |
| 61 | + |
| 62 | +##2. application配置文件信息 |
| 63 | + |
| 64 | +```yml |
| 65 | +server: |
| 66 | +port:8081 |
| 67 | +spring: |
| 68 | +datasource: |
| 69 | +username:root |
| 70 | +password:root |
| 71 | +driver-class-name:com.mysql.cj.jdbc.Driver |
| 72 | +url:jdbc:mysql://localhost:3306/sb?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai |
| 73 | +type:com.alibaba.druid.pool.DruidDataSource |
| 74 | + |
| 75 | +mybatis-plus: |
| 76 | +global-config: |
| 77 | +db-config: |
| 78 | +table-prefix:tb_# 数据库表名的前缀 |
| 79 | +type-aliases-package:vim.msjava.examples.pojo# 实体类所在包 |
| 80 | +mapper-locations:classpath*:/mapper/**Mapper.xml# **Mapper.xml 文件所在位置 |
| 81 | +``` |
| 82 | +
|
| 83 | +## 3. 启动类的扫描配置 |
| 84 | +
|
| 85 | +```java |
| 86 | +package vip.msjava.examples; |
| 87 | + |
| 88 | +import org.mybatis.spring.annotation.MapperScan; |
| 89 | +import org.springframework.boot.SpringApplication; |
| 90 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 91 | + |
| 92 | +@SpringBootApplication |
| 93 | +@MapperScan(basePackages = "vip.msjava.example.mapper") |
| 94 | +public class SpringbootApplication { |
| 95 | + |
| 96 | +public static void main(String[] args) { |
| 97 | +SpringApplication.run(SpringbootApplication.class, args); |
| 98 | +} |
| 99 | + |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +##4. 代码生成工具类 |
| 104 | + |
| 105 | +```java |
| 106 | +packagevip.msjava.examples.generator; |
| 107 | + |
| 108 | + |
| 109 | +importcom.baomidou.mybatisplus.core.exceptions.MybatisPlusException; |
| 110 | +importcom.baomidou.mybatisplus.core.toolkit.StringPool; |
| 111 | +importcom.baomidou.mybatisplus.core.toolkit.StringUtils; |
| 112 | +importcom.baomidou.mybatisplus.generator.AutoGenerator; |
| 113 | +importcom.baomidou.mybatisplus.generator.InjectionConfig; |
| 114 | +importcom.baomidou.mybatisplus.generator.config.*; |
| 115 | +importcom.baomidou.mybatisplus.generator.config.po.TableInfo; |
| 116 | +importcom.baomidou.mybatisplus.generator.config.rules.NamingStrategy; |
| 117 | +importcom.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; |
| 118 | + |
| 119 | +importjava.util.ArrayList; |
| 120 | +importjava.util.List; |
| 121 | +importjava.util.Scanner; |
| 122 | +/** |
| 123 | + *@author msJava |
| 124 | + *@Description: 基于MyBatis-Plus 自动生成 代码 |
| 125 | +*/ |
| 126 | +publicclassMyBatisPlusGenerator { |
| 127 | +/** |
| 128 | + * 读取控制台内容 |
| 129 | +*/ |
| 130 | +publicstaticStringscanner(Stringtip) { |
| 131 | +Scanner scanner=newScanner(System.in); |
| 132 | +StringBuilder help=newStringBuilder(); |
| 133 | + help.append("请输入"+ tip+":"); |
| 134 | +System.out.println(help.toString()); |
| 135 | +if (scanner.hasNext()) { |
| 136 | +String ipt= scanner.next(); |
| 137 | +if (StringUtils.isNotEmpty(ipt)) { |
| 138 | +return ipt; |
| 139 | + } |
| 140 | + } |
| 141 | +thrownewMybatisPlusException("请输入正确的"+ tip+"!"); |
| 142 | + } |
| 143 | + |
| 144 | +publicstaticvoidmain(String[]args) { |
| 145 | +// 代码生成器 |
| 146 | +AutoGenerator mpg=newAutoGenerator(); |
| 147 | + |
| 148 | +// 全局配置 |
| 149 | +GlobalConfig gc=newGlobalConfig(); |
| 150 | +String projectPath=System.getProperty("user.dir"); |
| 151 | + gc.setOutputDir(projectPath+"/src/main/java"); |
| 152 | +// gc.setOutputDir("D:\\test"); |
| 153 | +// 注释中的作者信息 |
| 154 | + gc.setAuthor("微信公众号: msJava , 一起实战学Java!"); |
| 155 | + gc.setOpen(false); |
| 156 | +// gc.setSwagger2(true); // 实体属性 Swagger2 注解 |
| 157 | + gc.setServiceName("%sService"); |
| 158 | + mpg.setGlobalConfig(gc); |
| 159 | + |
| 160 | +// 数据源配置 |
| 161 | +DataSourceConfig dsc=newDataSourceConfig(); |
| 162 | + dsc.setUrl("jdbc:mysql://localhost:3306/sb?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=UTC"); |
| 163 | +// dsc.setSchemaName("public"); |
| 164 | + dsc.setDriverName("com.mysql.cj.jdbc.Driver"); |
| 165 | + dsc.setUsername("root"); |
| 166 | + dsc.setPassword("root"); |
| 167 | + mpg.setDataSource(dsc); |
| 168 | + |
| 169 | +// 包配置 |
| 170 | +PackageConfig pc=newPackageConfig(); |
| 171 | + pc.setModuleName(null); |
| 172 | +// 这个需要根据你项目的包 修改 |
| 173 | + pc.setParent("vip.msjava.examples"); |
| 174 | + mpg.setPackageInfo(pc); |
| 175 | + |
| 176 | +// 自定义配置 |
| 177 | +InjectionConfig cfg=newInjectionConfig() { |
| 178 | +@Override |
| 179 | +publicvoidinitMap() { |
| 180 | +// to do nothing |
| 181 | + } |
| 182 | + }; |
| 183 | + |
| 184 | +// 如果模板引擎是 freemarker |
| 185 | +String templatePath="/templates/mapper.xml.ftl"; |
| 186 | +// 如果模板引擎是 velocity |
| 187 | +// String templatePath = "/templates/mapper.xml.vm"; |
| 188 | + |
| 189 | +// 自定义输出配置 |
| 190 | +List<FileOutConfig> focList=newArrayList<>(); |
| 191 | +// 自定义配置会被优先输出 |
| 192 | + focList.add(newFileOutConfig(templatePath) { |
| 193 | +@Override |
| 194 | +publicStringoutputFile(TableInfotableInfo) { |
| 195 | +// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!! |
| 196 | +return projectPath+"/src/main/resources/mapper/" |
| 197 | ++"/"+ tableInfo.getEntityName()+"Mapper"+StringPool.DOT_XML; |
| 198 | + } |
| 199 | + }); |
| 200 | + |
| 201 | + cfg.setFileOutConfigList(focList); |
| 202 | + mpg.setCfg(cfg); |
| 203 | + |
| 204 | +// 配置模板 |
| 205 | +TemplateConfig templateConfig=newTemplateConfig(); |
| 206 | + |
| 207 | + templateConfig.setXml(null); |
| 208 | + mpg.setTemplate(templateConfig); |
| 209 | + |
| 210 | +// 策略配置 |
| 211 | +StrategyConfig strategy=newStrategyConfig(); |
| 212 | + strategy.setNaming(NamingStrategy.underline_to_camel); |
| 213 | + strategy.setColumnNaming(NamingStrategy.underline_to_camel); |
| 214 | + strategy.setEntityLombokModel(true); |
| 215 | + strategy.setRestControllerStyle(true); |
| 216 | + strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); |
| 217 | + strategy.setControllerMappingHyphenStyle(true); |
| 218 | +// 数据库 表明前缀 也可以不配置 |
| 219 | + strategy.setTablePrefix("tb_");// 设置表前缀 |
| 220 | + mpg.setStrategy(strategy); |
| 221 | + mpg.setTemplateEngine(newFreemarkerTemplateEngine()); |
| 222 | + mpg.execute(); |
| 223 | + } |
| 224 | + |
| 225 | + |
| 226 | +} |
| 227 | + |
| 228 | +``` |
| 229 | + |
| 230 | +##5. 启动`MyBatisPlusGenerator`的Main方法 |
| 231 | + |
| 232 | +**输入表名即可,如果一次生成多个表信息,按照提示即可;** |
| 233 | + |
| 234 | +**最终的项目结果如下图所示:** |
| 235 | + |
| 236 | + |