前面两节我们已经上手了Mybatis Plus,已经可以进行查询和分页查询了,在讲解后续功能之前,有一个重要的功能需要先掌握,那就是代码自动生成。
如果你用过mybatis,应该都用过mybatis的代码自动生成,Mybatis Plus同样支持代码生成。
主要步骤:
1、配置pom文件,引入依赖jar包;
2、配置生成策略,文件生成路径、生成的表、生成的包等规则。
先来配置pom
<?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">
<!--<parent>
<artifactId>studyspringcloud</artifactId>
<groupId>com.itzhimei</groupId>
<version>1.0-SNAPSHOT</version>
</parent>-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>study-mybatis-plus-itzhimei</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.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.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.20</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.10.RELEASE</version>
</dependency>
</dependencies>
</project>
继承子spring boot,引入了mysql、velocity模板引擎、lombok、swagger、spring。
引入这么 多,是因为我在配置生成策略的时候,使用了这些功能,比如生成entity,使用lombok直接就是@Data注解的实体,省去了get、set方法。
controller直接就声明为RestController。
import com.baomidou.mybatisplus.annotation.IdType;
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.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.net.URL;
import java.util.Objects;
/**
* @Description:
* @Auther: itzhimei
*/
public class CG {
//代码生成器
private static AutoGenerator mpg = new AutoGenerator();
//全局配置
private static GlobalConfig gc = new GlobalConfig();
//作者、包名、去除表前缀
private static final String author = "itzhimei";
private static final String package_name = "com.itzhimei";
private static final String TABLE_PREFIX = "T_";
//数据库
private static final String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT";
private static final String driverName = "com.mysql.cj.jdbc.Driver";
private static final String userName = "root";
private static final String password = "test";
private static final String table_name = "T_USER";
public static void main(String[] args){
// 数据源配置
setDataSource();
// 全局配置
setGlobalConfig();
// 策略配置
setStrategy();
//执行
mpg.execute();
}
private static void setStrategy() {
StrategyConfig strategy = new StrategyConfig();
// 类名:Tb_userController -> TbUserController
strategy.setNaming(NamingStrategy.underline_to_camel);
// 属性名:start_time -> startTime
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// lombok 代替 setter/getter等方法
strategy.setEntityLombokModel(true);
// 设置Controller为RestController
strategy.setRestControllerStyle(true);
//由数据库该表生成
strategy.setInclude(table_name);
//去除表前缀
strategy.setTablePrefix(TABLE_PREFIX);
mpg.setStrategy(strategy);
}
private static void setGlobalConfig() {
URL urlPath = Thread.currentThread().getContextClassLoader().getResource("");
String projectPath = Objects.requireNonNull(urlPath).getPath().replace("target/classes", "src/main/java");
gc.setOutputDir(projectPath);//代码生成位置
gc.setFileOverride(true);//覆盖已有文件
gc.setAuthor(author);
gc.setSwagger2(true);
gc.setIdType(IdType.AUTO);//主键ID类型
gc.setDateType(DateType.ONLY_DATE);//设置时间类型为Date
mpg.setGlobalConfig(gc);
PackageConfig pc = new PackageConfig();// 包配置
pc.setParent(package_name);
mpg.setPackageInfo(pc);
}
private static void setDataSource() {
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(url);
dsc.setDriverName(driverName);
dsc.setUsername(userName);
dsc.setPassword(password);
mpg.setDataSource(dsc);
}
}
执行main函数之后,就按照指定包名生成了Controller、Service、ServiceImpl、Dao(mapper、和mapperxml)、entity。
需要注意的有两点:
1、jdbc url要配置时区
url = “jdbc:mysql://localhost:3306/test?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=GMT”;
serverTimezone=GMT,不配置执行会报错。
2、jdbc的驱动
driverName = “com.mysql.cj.jdbc.Driver”
新版本mysql,我这里用的是mysql8.0.20,驱动的包路径带cj,不是以前的com.mysql.jdbc.Driver
参考: