一、MyBatis Plus 与 MyBatis 的代码生成器比较
- MPG中提供了大量的自定义设置,生成的代码完全可以满足各类型的需求
- 表名及字段命名的策略选择
- 在MPG中,数据库表名和字段名可以选择驼峰命名也可以选择下滑线命名,MyBatis Plus中dbColumnUnderline配置是默认开启的。MPG建议采用驼峰命名,这样字段就不用做映射直接和实体类及实体类属性对应,可以避免产生映射性能损耗;使用下划线命名则要开启驼峰转换规则
- MPG 可以生成Entity实体类、Mapper接口、Mapper映射文件、Service层、Controller层
- MBG 可以生成Entity实体类、Mapper接口、Mapper映射文件
关于MyBatis 的代码生成器 MBG 可以参考 Data Access 之 MyBatis(七)- MBG & PageHelper
二、MyBatis Plus 代码生成器 MPG
工程搭建
使用IDEA创建Maven工程 mybatis-plus-mpg,使用到的相关依赖以及Spring和MyBatis Plus全局配置文件以及数据库日志的文件可以参考 Data Acces 之 MyBatis Plus(一)- BaseMapper CRUD(Part A) 中创建的mybatis-plus工程。
除以上提到的依赖,要运行MPG还需要MyBatis Plus代码生成器依赖、模板引擎依赖,MPG默认使用的是Apache的Velocity模板,这里使用Freemarker模板引擎。
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency> 复制代码
由于MPG会生成Controller层,所以需要加入Spring Web MVC的相关依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-version}</version> </dependency> 复制代码
MPG生成器代码
public class GeneratorApp { private static final String JDBC_URL = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=Asia/Shanghai&allowMultiQueries=true"; private static final String JDBC_USERNAME = "root"; private static final String JDBC_PASSWORD = "root"; // 代码生成器 @Test public void generator(){ FastAutoGenerator.create(JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD) .globalConfig(builder -> { builder.author("Jingnan") // 设置作者 //.enableSwagger() // 开启 swagger 模式 .fileOverride() // 覆盖已生成文件 .outputDir("src/main/java"); // 指定输出目录 }) .packageConfig(builder -> { builder.parent("com") // 设置父包名 .moduleName("lilith") // 设置父包模块名 .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "src/main/resources/mappers/")); // 设置mapperXml生成路径 }) .strategyConfig(builder -> { builder.addInclude("t_tesla").addInclude("porsche") // 设置需要生成的表名 .addTablePrefix("t_", "c_"); // 设置过滤表前缀 }) .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板 .execute(); } } 复制代码
generator方法中的代码从MyBatis Plus官网拷贝,仅仅做了数据库连接信息以及包名信息的修改
运行generator方法
根据控制台输出的日志,可以看出已经完成了代码生成
查看目录结构
MPG自动生成了controller层、service层、mapper接口以及mapper XML文件
如果想要一次性生成多个表的对应代码可以直接addInclude("porsche")即可
更多配置可以参考MyBatis Plus 代码生成器
三、测试 MPG 生成的代码
测试TeslaMapper
新建TeslaMapperTest,对TeslaMapper接口进行测试
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:application.xml") public class TeslaMapperTest { @Resource private TeslaMapper teslaMapper; @Test public void insert(){ Tesla tesla = new Tesla(); tesla.setName("Model 3P 2022"); tesla.setVehicleType("四门轿车"); tesla.setPrice(270000.00); tesla.setFactory("特斯拉上海超级工厂"); int count = teslaMapper.insert(tesla); System.out.println("执行INSERT操作后更新的行数:" + count); } @Test public void selectById(){ Tesla tesla = teslaMapper.selectById(6); System.out.println("执行SELECT查询到的数据:" + tesla); } @Test public void update(){ Tesla tesla = new Tesla(); tesla.setId(1166057516); tesla.setFactory("特斯拉柏林超级工厂"); int count = teslaMapper.updateById(tesla); System.out.println("执行UPDATE操作后更新的行数:" + count); } @Test public void delete(){ int count = teslaMapper.deleteById(1166057516); System.out.println("执行DELTE操作后更新的行数:" + count); } } 复制代码
执行insert方法
执行selectById方法
执行udpdate方法
执行delete方法
测试ITeslaService
新建ITeslaService的测试类ITeslaServiceTest
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:application.xml") public class ITeslaServiceTest { @Resource private ITeslaService teslaService; @Test public void insert(){ Tesla tesla = new Tesla(); tesla.setName("Roadster 2022"); tesla.setPrice(1200000.00); tesla.setVehicleType("跑车"); tesla.setFactory("加州弗拉蒙特超级工厂"); boolean save = teslaService.save(tesla); System.out.println("是否保存成功:" + save); System.out.println("保存成功的记录的ID为:" + tesla.getId()); } @Test public void getById(){ Tesla tesla = teslaService.getById(1166057517); System.out.println("根据ID获取的记录为:" + tesla); } @Test public void update(){ Tesla tesla = new Tesla(); tesla.setId(1166057517); tesla.setName("赛博皮卡 2022"); boolean update = teslaService.update(tesla, null); System.out.println("是否更新成功:" + update); } @Test public void remove(){ boolean remove = teslaService.removeById(1166057517); System.out.println("是否移除成功:" + remove); } } 复制代码
执行save方法
执行getById方法
执行update方法
执行remove方法