1. 写在前面
本文介绍使用MyBatis-Plus
代码生成器自动生成Entity
、Service
、ServiceImpl
、Mapper
、Controller
和xml
代码。
项目采用SpringBoot
集成,数据库对接Oracle
。
自定义xml
文件生成位置,放到resources/mapper
目录下。
2. 准备工作
使用Maven
管理依赖Jar
包,先安装Oracle
驱动包到本地仓库。(提供ojdbc6-11.1.0.7.0.jar)
参考安装命令:mvn install:install-file -Dfile=D:\xxx\lib\ojdbc6-11.1.0.7.0.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.1.0.7.0 -Dpackaging=jar
在pom文件中添加依赖:
- 引入
SpringBoot
,版本2.2.1.RELEASE
; - 引入
MyBatis-Plus
,版本3.4.1
; - 引入代码生成器
mybatis-plus-generator
,版本也是3.4.1
; - 代码生成器的默认模板引擎
velocity-engine-core
,版本2.2
; - 引入数据库驱动
ojdbc6
,版本11.1.0.7.0
;
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/>
</parent>
<properties>
<!-- 设置编码为 UTF-8 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<!-- 设置jre版本为 1.8 -->
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<mybatis.plus.version>3.4.1</mybatis.plus.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis.plus.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>${mybatis.plus.version}</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.1.0.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
3. 代码生成
自定义一个工具类,在main
方法中使用com.baomidou.mybatisplus.generator.AutoGenerator
类实现代码生成。
AutoGenerator
主要有四个配置:数据源配置DataSourceConfig
、包配置PackageConfig
、策略配置StrategyConfig
和全局配置GlobalConfig
。当然也可以进行模板配置TemplateConfig
和自定义配置InjectionConfig
满足个性化需求。
具体代码实现如下:
package com.road.core.tool;
import com.baomidou.mybatisplus.annotation.DbType;
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.config.ConstVal;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.io.File;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class OracleGenerator {
public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setDbType(DbType.ORACLE);
dsc.setUrl("jdbc:oracle:thin:@192.168.15.130:1521:orcl");
dsc.setDriverName("oracle.jdbc.driver.OracleDriver");
dsc.setUsername("healthbig");
dsc.setPassword("healthbig");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setEntity("com.road.core.entity");
pc.setService("com.road.core.service");
pc.setServiceImpl("com.road.core.service.impl");
pc.setMapper("com.road.core.mapper");
pc.setController("com.road.web.controller");
mpg.setPackageInfo(pc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel); // 数据库表映射到实体的命名策略
// 需要包含的表名,当enableSqlFilter为false时,允许正则表达式(与exclude二选一配置)
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setEntityLombokModel(true); // 是否为lombok模型
strategy.setRestControllerStyle(true); // 生成 @RestController 控制器
strategy.setControllerMappingHyphenStyle(true); // 驼峰转连字符
mpg.setStrategy(strategy);
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java"); // 代码生成输出目录
gc.setOpen(false); // 执行后是否打开代码生成目录
gc.setFileOverride(true); // 是否覆盖文件
mpg.setGlobalConfig(gc);
customPath(pc, mpg);
mpg.execute();
}
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入" + tip + ":");
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void customPath(PackageConfig pc, AutoGenerator mpg) throws NoSuchFieldException, IllegalAccessException {
String projectPath = System.getProperty("user.dir");
String mavenPath = "\\src\\main\\java\\";
String srcPath = projectPath+mavenPath;
Map pathInfo = new HashMap<>();
pathInfo.put(ConstVal.CONTROLLER_PATH, srcPath + pc.getController().replaceAll("\\.", StringPool.BACK_SLASH + File.separator));
pathInfo.put(ConstVal.SERVICE_PATH, srcPath + pc.getService().replaceAll("\\.", StringPool.BACK_SLASH + File.separator));
pathInfo.put(ConstVal.SERVICE_IMPL_PATH, srcPath + pc.getServiceImpl().replaceAll("\\.", StringPool.BACK_SLASH + File.separator));
pathInfo.put(ConstVal.ENTITY_PATH, srcPath + pc.getEntity().replaceAll("\\.", StringPool.BACK_SLASH + File.separator));
pathInfo.put(ConstVal.MAPPER_PATH, srcPath + pc.getMapper().replaceAll("\\.", StringPool.BACK_SLASH + File.separator));
pathInfo.put(ConstVal.XML_PATH, projectPath+"\\src\\main\\resources\\mapper\\");
pc.setPathInfo(pathInfo);
Map<String,String> packageInfo = new HashMap<>();
packageInfo.put(ConstVal.CONTROLLER, pc.getController());
packageInfo.put(ConstVal.SERVICE, pc.getService());
packageInfo.put(ConstVal.SERVICE_IMPL, pc.getServiceImpl());
packageInfo.put(ConstVal.ENTITY, pc.getEntity());
packageInfo.put(ConstVal.MAPPER, pc.getMapper());
ConfigBuilder configBuilder = new ConfigBuilder(mpg.getPackageInfo(), mpg.getDataSource(), mpg.getStrategy(), mpg.getTemplate(), mpg.getGlobalConfig());
Field packageInfoField = configBuilder.getClass().getDeclaredField("packageInfo");
packageInfoField.setAccessible(true);
packageInfoField.set(configBuilder,packageInfo);
mpg.setConfig(configBuilder);
}
}
4. 参考文档
MyBatis-Plus代码生成器快速入门 https://mybatis.plus/guide/generator.html
MyBatis-Plus代码生成器详细配置 https://mybatis.plus/config/generator-config.html
自定义代码生成路径 https://blog.csdn.net/qq_29550537/article/details/105477585