简介
之前有文章Mybatis-Generator使用,现在体验了Mybatis-Plus-Generator
,真心觉得有了它开发效率提高太多了,用起来是真的爽,爽!
参考:
https://baomidou.com/pages/779a6e/
https://blog.csdn.net/qq_20185737/article/details/121456598
代码
注意
适用版本:mybatis-plus-generator 3.5.1
及其以上版本,对历史版本不兼容!3.5.1 以下的请参考 代码生成器旧
依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.6.6</version> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency>
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.1</version> </dependency>
<dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.1</version> </dependency>
<dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.3</version> </dependency>
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency>
</dependencies>
|
yml
因为我习惯的三字段(valid
、create_time
、update_time
),所以配置有逻辑删除
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| server: port: 8080 servlet: context-path: /
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/sl?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true username: root password: root
mybatis-plus: configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl mapper-locations: classpath*:mapper/**/*Mapper.xml global-config: db-config: logic-not-delete-value: 0 logic-delete-value: 1 logic-delete-field: valid
|
Generator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
public class Generator { public static void main(String[] args) { FastAutoGenerator.create("jdbc:mysql://localhost:3306/sl", "root", "root") .globalConfig(builder -> { builder.author("wnhyang") .outputDir(System.getProperty("user.dir") + "/src/main/java") .commentDate("yyyy-MM-dd hh:mm:ss") .dateType(DateType.TIME_PACK) .fileOverride() .disableOpenDir(); }) .packageConfig(builder -> { builder.parent("com.wnh.shortlink") .moduleName("") .entity("entity") .service("service") .serviceImpl("service.impl") .mapper("mapper") .xml("mapper") .controller("controller") .other("utils") .pathInfo(Collections.singletonMap(OutputFile.mapperXml, System.getProperty("user.dir") + "/src/main/resources/mapper")); }) .strategyConfig(builder -> { builder.addInclude("url_map") .addTablePrefix("t_", "c_")
.mapperBuilder() .superClass(BaseMapper.class) .formatMapperFileName("%sMapper") .enableMapperAnnotation() .formatXmlFileName("%sXml")
.serviceBuilder() .formatServiceFileName("%sService") .formatServiceImplFileName("%sServiceImpl")
.entityBuilder() .enableLombok() .disableSerialVersionUID() .logicDeleteColumnName("valid") .naming(NamingStrategy.underline_to_camel) .columnNaming(NamingStrategy.underline_to_camel) .addTableFills( new Column("create_time", FieldFill.INSERT), new Column("update_time", FieldFill.INSERT_UPDATE) ) .enableTableFieldAnnotation()
.controllerBuilder() .formatFileName("%sController") .enableRestStyle(); }) .templateEngine(new VelocityTemplateEngine())
.execute(); } }
|
mybatis-plus
提供的自动填充功能,参考官网文档即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| @Slf4j @Component public class MyMetaObjectHandler implements MetaObjectHandler {
@Override public void insertFill(MetaObject metaObject) { log.info("start insert fill ...."); this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now()); this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); }
@Override public void updateFill(MetaObject metaObject) { log.info("start update fill ...."); this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now()); } }
|
启动类
添加扫描即可
1 2 3 4 5 6 7 8 9 10 11
|
@MapperScan("com.wnh.shortlink.mapper") @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
|
使用Mybatis-Plus
的体验就是爽,开发效率太高了(又重复了一遍😂)