mybatisPlus 代码生成器
前言
平时写的pojo实体类都比较繁琐,特别是当表多的时候,要一个个对着写,还可能会写错,那么mybatisplus可以为我们解决这些问题,我们不仅不需要写实体类并且mapper和service都能自动生成,为我们节省了时间。
使用步骤
1.导入所需要的依赖
2.配置数据源,mybaitsplus
3.编写代码生成器类
4.测试结果
1.导入所需要的依赖
pom.xml
<!--mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--实体类的帮助注解 例如getset(@Data)-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!--代码生成器-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<!--模板引擎-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.30</version>
</dependency>
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>3.3.2.RELEASE</version>
</dependency>
2.配置数据源,mybaitsplus
这里我使用的是yml进行配置
application.yml
spring:
datasource:
username: 用户名
password: 密码
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://url:3306/数据库名?userUnicode=true&characterEncoding=gbk&serverTimezone=UTC&useSSL=false
# 数据源其他配置
initialSize: 1
minIdle: 3
maxActive: 20
initialization-mode: ALWAYS
# 配置获取连接等待超时的时间
maxWait: 120000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 120000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 30000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
jackson:
data-format: yyyy-MM-dd HH:mm
time-zone: GMT+8
#设置开发环境
profiles:
active: dev
mybatis-plus:
global-config:
db-config:
#配置逻辑删除
logic-delete-value: 1
logic-not-delete-value: 0
configuration:
#开配置日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3.编写代码生成器类
TableCode.java
public class TableCode {
public static void main(String[] args) { // 需要构建一个 代码自动生成器 对象
AutoGenerator mpg = new AutoGenerator(); // 配置策略
// 1、全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("RT");//设置作者名
gc.setOpen(false);
gc.setFileOverride(false); // 是否覆盖
gc.setServiceName("%sService"); // 去Service的I前缀
gc.setIdType(IdType.ID_WORKER);
gc.setDateType(DateType.ONLY_DATE);
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);
//2、设置数据源
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://url:3306/数据库名?userUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("用户名");
dsc.setPassword("密码");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
//3、包的配置
PackageConfig pc = new PackageConfig();
//下面都是自己命名
pc.setModuleName("");
pc.setParent("com.railtransit");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setService("service");
pc.setController("Controller");
mpg.setPackageInfo(pc);
//4、策略配置
StrategyConfig strategy = new StrategyConfig();
//下面可以选择该数据库中哪些表进行代码的生成
strategy.setInclude("ml_data","station_all","trips_count","trips_daycount","trips_instationname","trips_month","trips_morning","trips_night","trips_od","trips_outstationname","trips_price","trips_sitetosite","trips_week","users_age","users_gender","workday_count"); // 设置要映射的表名
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true); // 自动lombok;
//以下可以根据自己的需求来做,就不详细说明是什么了
//strategy.setLogicDeleteFieldName("deleted"); // 自动填充配置
//TableFill gmtCreate = new TableFill("create_time", FieldFill.INSERT);
//TableFill gmtModified = new TableFill("update_time", FieldFill.INSERT_UPDATE);
//ArrayList<TableFill> tableFills = new ArrayList<>();
//tableFills.add(gmtCreate);
//tableFills.add(gmtModified);
//strategy.setTableFillList(tableFills); // 乐观锁
//strategy.setVersionFieldName("version");
strategy.setRestControllerStyle(true);
strategy.setControllerMappingHyphenStyle(true); // localhost:8080/hello_id_2
mpg.setStrategy(strategy);
mpg.execute(); //执行
}
}
4.测试结果
执行Table.class中的main方法即可
结果:
记录我的学习笔记