基于Mybatis plus 自动代码生成器的实现代码
作者:夏天的尾巴ljh 时间:2023-11-24 10:40:51
1.使用的是maven项目,添加依赖
<!-- mybatis-plus begin -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>2.2.0</version>
</dependency>
还有数据库的连接
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>compile</scope>
</dependency>
最后是源码
import com.baomidou.mybatisplus.generator.AutoGenerator;
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.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.jupiter.api.Test;
/**
* mybatis-plus 自动生成代码
*
* @author Terry
* @version 1.0
* @date 2018-05-16 09:35
*/
public class SimpleMp {
@Test
public void generateCode() {
//指定包名
String packageName = "com.hciot.hhhh";
//user -> UserService, 设置成true: user -> IUserService
boolean serviceNameStartWithI = false;
//指定生成的表名
String[] tableNames = new String[]{"data_air_sensor_co", "order_product", "relation_device_gateway"};
generateByTables(serviceNameStartWithI, packageName, tableNames);
}
/**
* 根据表自动生成
*
* @param serviceNameStartWithI 默认为false
* @param packageName 包名
* @param tableNames 表名
* @author Terry
*/
private void generateByTables(boolean serviceNameStartWithI, String packageName, String... tableNames) {
//配置数据源
DataSourceConfig dataSourceConfig = getDataSourceConfig();
// 策略配置
StrategyConfig strategyConfig = getStrategyConfig(tableNames);
//全局变量配置
GlobalConfig globalConfig = getGlobalConfig(serviceNameStartWithI);
//包名配置
PackageConfig packageConfig = getPackageConfig(packageName);
//自动生成
atuoGenerator(dataSourceConfig, strategyConfig, globalConfig, packageConfig);
}
/**
* 集成
*
* @param dataSourceConfig 配置数据源
* @param strategyConfig 策略配置
* @param config 全局变量配置
* @param packageConfig 包名配置
* @author Terry
*/
private void atuoGenerator(DataSourceConfig dataSourceConfig, StrategyConfig strategyConfig, GlobalConfig config, PackageConfig packageConfig) {
new AutoGenerator()
.setGlobalConfig(config)
.setDataSource(dataSourceConfig)
.setStrategy(strategyConfig)
.setPackageInfo(packageConfig)
.execute();
}
/**
* 设置包名
*
* @param packageName 父路径包名
* @return PackageConfig 包名配置
* @author Terry
*/
private PackageConfig getPackageConfig(String packageName) {
return new PackageConfig()
.setParent(packageName)
.setXml("mapper")
.setMapper("dao")
.setController("controller")
.setEntity("entity");
}
/**
* 全局配置
*
* @param serviceNameStartWithI false
* @return GlobalConfig
* @author Terry
*/
private GlobalConfig getGlobalConfig(boolean serviceNameStartWithI) {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig
.setBaseColumnList(true)
.setBaseResultMap(true)
.setActiveRecord(false)
.setAuthor("Terry")
//设置输出路径
.setOutputDir(getOutputDir("mybatis-plus"))
.setFileOverride(true);
if (!serviceNameStartWithI) {
//设置service名
globalConfig.setServiceName("%sService");
}
return globalConfig;
}
/**
* 返回项目路径
*
* @param projectName 项目名
* @return 项目路径
* @author Terry
*/
private String getOutputDir(String projectName) {
String path = this.getClass().getClassLoader().getResource("").getPath();
int index = path.indexOf(projectName);
return path.substring(1, index) + projectName + "/src/main/java/";
}
/**
* 策略配置
*
* @param tableNames 表名
* @return StrategyConfig
* @author Terry
*/
private StrategyConfig getStrategyConfig(String... tableNames) {
return new StrategyConfig()
// 全局大写命名 ORACLE 注意
.setCapitalMode(true)
.setEntityLombokModel(false)
// 表名、字段名、是否使用下划线命名(默认 false)
.setDbColumnUnderline(true)
//从数据库表到文件的命名策略
.setNaming(NamingStrategy.underline_to_camel)
//需要生成的的表名,多个表名传数组
.setInclude(tableNames);
}
/**
* 配置数据源
*
* @return 数据源配置 DataSourceConfig
* @author Terry
*/
private DataSourceConfig getDataSourceConfig() {
String dbUrl = "jdbc:mysql://localhost:3306/test";
return new DataSourceConfig().setDbType(DbType.MYSQL)
.setUrl(dbUrl)
.setUsername("root")
.setPassword("root")
.setDriverName("com.mysql.jdbc.Driver");
}
/**
* 根据表自动生成
*
* @param packageName 包名
* @param tableNames 表名
* @author Terry
*/
@SuppressWarnings("unused")
private void generateByTables(String packageName, String... tableNames) {
generateByTables(true, packageName, tableNames);
}
}
总结
以上所述是小编给大家介绍的基于Mybatis plus 自动代码生成器的实现代码,希望对大家有所帮助.
来源:https://blog.csdn.net/weixin_41685100/article/details/80342246
标签:mybatis,plus,代码,生成器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot+jwt实现token登陆权限认证的实现
2021-12-22 12:02:46
Java 关键字break和continue的使用说明
2023-07-22 03:50:59
idea导入工程时不能导入maven项目不能加入tomcatServer的原因
2023-06-13 05:35:11
![](https://img.aspxhome.com/file/2023/9/96259_0s.jpg)
Kotlin Navigation可视化开发详解
2022-10-18 10:55:56
![](https://img.aspxhome.com/file/2023/4/111714_0s.png)
Spring实现处理跨域请求代码详解
2023-11-25 12:28:34
Spring 容器初始化 register 与 refresh方法
2021-09-19 21:20:56
![](https://img.aspxhome.com/file/2023/1/98401_0s.png)
解决try-catch捕获异常信息后Spring事务失效的问题
2022-11-15 03:17:33
Java中super和this关键字详解
2023-03-18 15:49:08
![](https://img.aspxhome.com/file/2023/4/63054_0s.png)
SpringMVC + servlet3.0 文件上传的配置和实现代码
2023-08-08 16:42:43
使用Spring事件机制实现异步的方法
2023-06-23 06:47:08
![](https://img.aspxhome.com/file/2023/2/91062_0s.png)
Spring Aop 如何获取参数名参数值
2022-09-08 17:00:41
C#怎样才能将XML文件导入SQL Server
2022-02-16 17:51:13
深入了解java NIO之Selector(选择器)
2022-02-01 16:39:17
Android基于ViewPager+Fragment实现左右滑屏效果的方法
2023-11-16 10:50:38
java实现解析二进制文件的方法(字符串、图片)
2023-04-18 19:07:51
java实现简单年龄计算器
2022-01-28 02:23:34
![](https://img.aspxhome.com/file/2023/1/63041_0s.png)
java基础-数组扩容详解
2022-05-24 00:34:58
Android Studio下Flutter环境搭建图文教程
2023-11-06 00:08:03
![](https://img.aspxhome.com/file/2023/5/138895_0s.jpg)
Spring Boot自定义Starter组件开发实现配置过程
2022-05-31 16:07:12
![](https://img.aspxhome.com/file/2023/5/65765_0s.png)
SpringBoot中Dozer的使用小结
2023-11-25 01:24:38