Spring Cloud 中使用Mybatis Plus的generator 生成数据访问层代码
项目集成了Mybatis Plus后,一些数据库访问层的重复代码可交给代码生成器生成,可大大减轻开发的工作量。 代码生成工具很多,上文中既然集成的Mybatis Plus,本文就使用Mybatis Plus 自带的generator生成代码。可参照
一、新建generator项目
二、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0
</modelVersion>
<parent>
<groupId>com.marvin
</groupId>
<artifactId>demo-spring-cloud-dependencies
</artifactId>
<version>1.0.0-SNAPSHOT
</version>
<relativePath>../demo-spring-cloud-dependencies/pom.xml
</relativePath>
</parent>
<artifactId>demo-spring-cloud-generat
</artifactId>
<version>1.0.0-SNAPSHOT
</version>
<packaging>jar
</packaging>
<name>demo-spring-cloud-generat
</name>
<description>demo spring cloud 代码生产工具
</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot
</groupId>
<artifactId>spring-boot-starter-web
</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok
</groupId>
<artifactId>lombok
</artifactId>
<optional>true
</optional>
</dependency>
<dependency>
<groupId>com.baomidou
</groupId>
<artifactId>mybatis-plus-extension
</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>org.apache.velocity
</groupId>
<artifactId>velocity-engine-core
</artifactId>
<version>2.1
</version>
</dependency>
<dependency>
<groupId>mysql
</groupId>
<artifactId>mysql-connector-java
</artifactId>
</dependency>
</dependencies>
</project>
三、代码生成配置类
package com
.marvin
.demo
.spring
.cloud
.generat
;
public class GeneratorConstants {
public static final String PROJECT_DIR
= "/Users/maihaixian/IdeaProjects/demo-spring-cloud/demo-spring-cloud-member";
public static final String JAVA_DIR
= "/src/main/java";
public static final String AUTOR
= "Marvin";
public static final String DB_DRIVER
= "com.mysql.cj.jdbc.Driver";
public static final String DB_URL
= "jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf8&serverTimezone=Asia/Shanghai";
public static final String DB_USER_NAME
= "root";
public static final String DB_PASSWORD
= "123456";
public static final String TABLES
= "tb_user,sys_user";
public static final String MODULE
= "member";
public static final String PARENT_PACKAGE
= "com.marvin.demo.spring.cloud";
}
四、代码生成类
package com
.marvin
.demo
.spring
.cloud
.generat
;
import com
.baomidou
.mybatisplus
.annotation
.DbType
;
import com
.baomidou
.mybatisplus
.generator
.AutoGenerator
;
import com
.baomidou
.mybatisplus
.generator
.config
.*
;
import com
.baomidou
.mybatisplus
.generator
.config
.rules
.DateType
;
import com
.baomidou
.mybatisplus
.generator
.config
.rules
.NamingStrategy
;
import org
.apache
.commons
.lang3
.StringUtils
;
public class CodeGenerator {
public static void main(String
[] args
) {
AutoGenerator mpg
= new AutoGenerator();
GlobalConfig gc
= new GlobalConfig();
gc
.setOutputDir(GeneratorConstants
.PROJECT_DIR
+ GeneratorConstants
.JAVA_DIR
);
gc
.setFileOverride(true);
gc
.setActiveRecord(true);
gc
.setEnableCache(false);
gc
.setBaseResultMap(true);
gc
.setBaseColumnList(true);
gc
.setAuthor(GeneratorConstants
.AUTOR
);
gc
.setDateType(DateType
.ONLY_DATE
);
gc
.setMapperName("%sMapper");
gc
.setXmlName("%sMapper");
gc
.setServiceName("%sService");
gc
.setServiceImplName("%sServiceImpl");
gc
.setControllerName("%sController");
mpg
.setGlobalConfig(gc
);
DataSourceConfig dsc
= new DataSourceConfig();
dsc
.setDbType(DbType
.MYSQL
);
dsc
.setDriverName(GeneratorConstants
.DB_DRIVER
);
dsc
.setUrl(GeneratorConstants
.DB_URL
);
dsc
.setUsername(GeneratorConstants
.DB_USER_NAME
);
dsc
.setPassword(GeneratorConstants
.DB_PASSWORD
);
mpg
.setDataSource(dsc
);
StrategyConfig strategy
= new StrategyConfig();
strategy
.setNaming(NamingStrategy
.underline_to_camel
);
strategy
.setInclude(StringUtils
.split(GeneratorConstants
.TABLES
, ","));
strategy
.setEntityLombokModel(true);
mpg
.setStrategy(strategy
);
PackageConfig pc
= new PackageConfig();
pc
.setModuleName(GeneratorConstants
.MODULE
);
pc
.setParent(GeneratorConstants
.PARENT_PACKAGE
);
mpg
.setPackageInfo(pc
);
TemplateConfig tc
= new TemplateConfig();
mpg
.setTemplate(tc
);
mpg
.execute();
}
}
执行main方法,在member项目中生成对应代码。
如需修改模板,可以到mybatisplus的generator的源码中的template目录下复制到项目的resource/templates目录下修改