Java开发学习(五十)----MyBatisPlus快速开发之代码生成器解析
阅读原文时间:2023年07月08日阅读:6

1、代码生成器原理分析

造句:

我们可以往空白内容进行填词造句,比如:

在比如:

观察我们之前写的代码,会发现其中也会有很多重复内容,比如:

那我们就想,如果我想做一个Book模块的开发,是不是只需要将红色部分的内容全部更换成Book即可,如:

所以我们会发现,做任何模块的开发,对于这段代码,基本上都是对红色部分的调整,所以我们把去掉红色内容的东西称之为模板,红色部分称之为参数,以后只需要传入不同的参数,就可以根据模板创建出不同模块的dao代码。

除了Dao可以抽取模块,其实我们常见的类都可以进行抽取,只要他们有公共部分即可。再来看下模型类的模板:

  • ① 可以根据数据库表的表名来填充

  • ② 可以根据用户的配置来生成ID生成策略

  • ③到⑨可以根据数据库表字段名称来填充

所以只要我们知道是对哪张表进行代码生成,这些内容我们都可以进行填充。

分析完后,我们会发现,要想完成代码自动生成,我们需要有以下内容:

  • 模板: MyBatisPlus已经提供了模板,也可以自己提供模板,但是麻烦,不建议

  • 数据库相关配置:读取数据库获取表和字段信息

  • 开发者自定义配置:手工配置,比如ID生成策略

2、代码生成器实现

步骤1:创建一个Maven项目

代码2:导入对应的jar包

<?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"
 &nbsp; &nbsp; &nbsp; &nbsp; xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 &nbsp; &nbsp;<modelVersion>4.0.0</modelVersion>
 &nbsp; &nbsp;<parent>
 &nbsp; &nbsp; &nbsp; &nbsp;<groupId>org.springframework.boot</groupId>
 &nbsp; &nbsp; &nbsp; &nbsp;<artifactId>spring-boot-starter-parent</artifactId>
 &nbsp; &nbsp; &nbsp; &nbsp;<version>2.5.1</version>
 &nbsp; &nbsp;</parent>
 &nbsp; &nbsp;<groupId>com.itheima</groupId>
 &nbsp; &nbsp;<artifactId>mybatisplus_04_generator</artifactId>
 &nbsp; &nbsp;<version>0.0.1-SNAPSHOT</version>
 &nbsp; &nbsp;<properties>
 &nbsp; &nbsp; &nbsp; &nbsp;<java.version>1.8</java.version>
 &nbsp; &nbsp;</properties>
 &nbsp; &nbsp;<dependencies>
 &nbsp; &nbsp; &nbsp; &nbsp;<!--spring webmvc-->
 &nbsp; &nbsp; &nbsp; &nbsp;<dependency>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<groupId>org.springframework.boot</groupId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<artifactId>spring-boot-starter-web</artifactId>
 &nbsp; &nbsp; &nbsp; &nbsp;</dependency>
​
 &nbsp; &nbsp; &nbsp; &nbsp;<!--mybatisplus-->
 &nbsp; &nbsp; &nbsp; &nbsp;<dependency>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<groupId>com.baomidou</groupId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<artifactId>mybatis-plus-boot-starter</artifactId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<version>3.4.1</version>
 &nbsp; &nbsp; &nbsp; &nbsp;</dependency>
​
 &nbsp; &nbsp; &nbsp; &nbsp;<!--druid-->
 &nbsp; &nbsp; &nbsp; &nbsp;<dependency>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<groupId>com.alibaba</groupId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<artifactId>druid</artifactId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<version>1.1.16</version>
 &nbsp; &nbsp; &nbsp; &nbsp;</dependency>
​
 &nbsp; &nbsp; &nbsp; &nbsp;<!--mysql-->
 &nbsp; &nbsp; &nbsp; &nbsp;<dependency>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<groupId>mysql</groupId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<artifactId>mysql-connector-java</artifactId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<scope>runtime</scope>
 &nbsp; &nbsp; &nbsp; &nbsp;</dependency>
​
 &nbsp; &nbsp; &nbsp; &nbsp;<!--test-->
 &nbsp; &nbsp; &nbsp; &nbsp;<dependency>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<groupId>org.springframework.boot</groupId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<artifactId>spring-boot-starter-test</artifactId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<scope>test</scope>
 &nbsp; &nbsp; &nbsp; &nbsp;</dependency>
​
 &nbsp; &nbsp; &nbsp; &nbsp;<!--lombok-->
 &nbsp; &nbsp; &nbsp; &nbsp;<dependency>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<groupId>org.projectlombok</groupId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<artifactId>lombok</artifactId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<version>1.18.12</version>
 &nbsp; &nbsp; &nbsp; &nbsp;</dependency>
​
 &nbsp; &nbsp; &nbsp; &nbsp;<!--代码生成器-->
 &nbsp; &nbsp; &nbsp; &nbsp;<dependency>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<groupId>com.baomidou</groupId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<artifactId>mybatis-plus-generator</artifactId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<version>3.4.1</version>
 &nbsp; &nbsp; &nbsp; &nbsp;</dependency>
​
 &nbsp; &nbsp; &nbsp; &nbsp;<!--velocity模板引擎-->
 &nbsp; &nbsp; &nbsp; &nbsp;<dependency>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<groupId>org.apache.velocity</groupId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<artifactId>velocity-engine-core</artifactId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<version>2.3</version>
 &nbsp; &nbsp; &nbsp; &nbsp;</dependency>
​
 &nbsp; &nbsp;</dependencies>
​
 &nbsp; &nbsp;<build>
 &nbsp; &nbsp; &nbsp; &nbsp;<plugins>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<plugin>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<groupId>org.springframework.boot</groupId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<artifactId>spring-boot-maven-plugin</artifactId>
 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</plugin>
 &nbsp; &nbsp; &nbsp; &nbsp;</plugins>
 &nbsp; &nbsp;</build>
​
</project>
​

步骤3:编写引导类

@SpringBootApplication
public class Mybatisplus04GeneratorApplication {
​
 &nbsp; &nbsp;public static void main(String[] args) {
 &nbsp; &nbsp; &nbsp; &nbsp;SpringApplication.run(Mybatisplus04GeneratorApplication.class, args);
 &nbsp;  }
​
}

步骤4:创建代码生成类

public class CodeGenerator {
 &nbsp; &nbsp;public static void main(String[] args) {
 &nbsp; &nbsp; &nbsp; &nbsp;//1.获取代码生成器的对象
 &nbsp; &nbsp; &nbsp; &nbsp;AutoGenerator autoGenerator = new AutoGenerator();
​
 &nbsp; &nbsp; &nbsp; &nbsp;//设置数据库相关配置
 &nbsp; &nbsp; &nbsp; &nbsp;DataSourceConfig dataSource = new DataSourceConfig();
 &nbsp; &nbsp; &nbsp; &nbsp;dataSource.setDriverName("com.mysql.cj.jdbc.Driver");
 &nbsp; &nbsp; &nbsp; &nbsp;dataSource.setUrl("jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC");
 &nbsp; &nbsp; &nbsp; &nbsp;dataSource.setUsername("root");
 &nbsp; &nbsp; &nbsp; &nbsp;dataSource.setPassword("root");
 &nbsp; &nbsp; &nbsp; &nbsp;autoGenerator.setDataSource(dataSource);
​
 &nbsp; &nbsp; &nbsp; &nbsp;//设置全局配置
 &nbsp; &nbsp; &nbsp; &nbsp;GlobalConfig globalConfig = new GlobalConfig();
 &nbsp; &nbsp; &nbsp; &nbsp;globalConfig.setOutputDir(System.getProperty("user.dir")+"/mybatisplus_04_generator/src/main/java"); &nbsp; &nbsp;//设置代码生成位置
 &nbsp; &nbsp; &nbsp; &nbsp;globalConfig.setOpen(false); &nbsp; &nbsp;//设置生成完毕后是否打开生成代码所在的目录
 &nbsp; &nbsp; &nbsp; &nbsp;globalConfig.setAuthor("黑马程序员"); &nbsp; &nbsp;//设置作者
 &nbsp; &nbsp; &nbsp; &nbsp;globalConfig.setFileOverride(true); &nbsp; &nbsp; //设置是否覆盖原始生成的文件
 &nbsp; &nbsp; &nbsp; &nbsp;globalConfig.setMapperName("%sDao"); &nbsp; &nbsp;//设置数据层接口名,%s为占位符,指代模块名称
 &nbsp; &nbsp; &nbsp; &nbsp;globalConfig.setIdType(IdType.ASSIGN_ID); &nbsp; //设置Id生成策略
 &nbsp; &nbsp; &nbsp; &nbsp;autoGenerator.setGlobalConfig(globalConfig);
​
 &nbsp; &nbsp; &nbsp; &nbsp;//设置包名相关配置
 &nbsp; &nbsp; &nbsp; &nbsp;PackageConfig packageInfo = new PackageConfig();
 &nbsp; &nbsp; &nbsp; &nbsp;packageInfo.setParent("com.aaa"); &nbsp; //设置生成的包名,与代码所在位置不冲突,二者叠加组成完整路径
 &nbsp; &nbsp; &nbsp; &nbsp;packageInfo.setEntity("domain"); &nbsp; &nbsp;//设置实体类包名
 &nbsp; &nbsp; &nbsp; &nbsp;packageInfo.setMapper("dao"); &nbsp; //设置数据层包名
 &nbsp; &nbsp; &nbsp; &nbsp;autoGenerator.setPackageInfo(packageInfo);
​
 &nbsp; &nbsp; &nbsp; &nbsp;//策略设置
 &nbsp; &nbsp; &nbsp; &nbsp;StrategyConfig strategyConfig = new StrategyConfig();
 &nbsp; &nbsp; &nbsp; &nbsp;strategyConfig.setInclude("tbl_user"); &nbsp;//设置当前参与生成的表名,参数为可变参数
 &nbsp; &nbsp; &nbsp; &nbsp;strategyConfig.setTablePrefix("tbl_"); &nbsp;//设置数据库表的前缀名称,模块名 = 数据库表名 - 前缀名  例如: User = tbl_user - tbl_
 &nbsp; &nbsp; &nbsp; &nbsp;strategyConfig.setRestControllerStyle(true); &nbsp; &nbsp;//设置是否启用Rest风格
 &nbsp; &nbsp; &nbsp; &nbsp;strategyConfig.setVersionFieldName("version"); &nbsp;//设置乐观锁字段名
 &nbsp; &nbsp; &nbsp; &nbsp;strategyConfig.setLogicDeleteFieldName("deleted"); &nbsp;//设置逻辑删除字段名
 &nbsp; &nbsp; &nbsp; &nbsp;strategyConfig.setEntityLombokModel(true); &nbsp;//设置是否启用lombok
 &nbsp; &nbsp; &nbsp; &nbsp;autoGenerator.setStrategy(strategyConfig);
 &nbsp; &nbsp; &nbsp; &nbsp;//2.执行生成操作
 &nbsp; &nbsp; &nbsp; &nbsp;autoGenerator.execute();
 &nbsp;  }
}

对于代码生成器中的代码内容,我们可以直接从官方文档中获取代码进行修改,https://baomidou.com/pages/d357af/#%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B

步骤5:运行程序

运行成功后,会在当前项目中生成很多代码,代码包含controller,servicemapperentity

至此代码生成器就已经完成工作,我们能快速根据数据库表来创建对应的类,简化我们的代码开发。

3、Mybatisplus中Service的CRUD

回顾我们之前业务层代码的编写,编写接口和对应的实现类:

public interface UserService{

}
​
@Service
public class UserServiceImpl implements UserService{
​
}

接口和实现类有了以后,需要在接口和实现类中声明方法

public interface UserService{
    public List<User> findAll();
}
​
@Service
public class UserServiceImpl implements UserService{
 &nbsp; &nbsp;@Autowired
 &nbsp; &nbsp;private UserDao userDao;
 &nbsp; &nbsp;
    public List<User> findAll(){
 &nbsp; &nbsp; &nbsp; &nbsp;return userDao.selectList(null);
 &nbsp;  }
}

Mybatisplus看到上面的代码以后就说这些方法也是比较固定和通用的,那来帮你抽取下,所以Mybatisplus提供了一个Service接口和实现类,分别是:IServiceServiceImpl,后者是对前者的一个具体实现。

以后我们自己写的Service就可以进行如下修改:

public interface UserService extends IService<User>{

}
​
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService{
​
}

修改以后的好处是,Mybatisplus已经帮我们把业务层的一些基础的增删改查都已经实现了,可以直接进行使用。

编写测试类进行测试:

@SpringBootTest
class Mybatisplus04GeneratorApplicationTests {
​
 &nbsp; &nbsp;private IUserService userService;
​
 &nbsp; &nbsp;@Test
 &nbsp; &nbsp;void testFindAll() {
 &nbsp; &nbsp; &nbsp; &nbsp;List<User> list = userService.list();
 &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(list);
 &nbsp;  }
​
}

思考:在Mybatisplus封装的Service层都有哪些方法可以用?

https://baomidou.com/pages/49cc81/