springboot3.0整合mybatis-flex实现逆向工程的示例代码
作者:小神话ing
逆向工程先创建数据库表,由框架负责根据数据库表,自动生成mybatis所要执行的代码,本文就来介绍一下springboot mybatis-flex逆向工程,感兴趣的可以了解一下
什么是mybatis逆向工程?
mybatis需要程序员编写SQL语句。mybatis官网提供了逆向工程,针对对于单表可自动生成mybatis所要执行的代码(mapper.java、mapper.xml、pojo…)
实际开发中常用的逆向工程方式:
由数据库的表生成代码,之所以强调“单表”两个字,是因为mybatis逆向工程生成的Mapper接口所进行的操作都是针对单表的。
引入基本依赖
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>5.0.1</version> </dependency> <dependency> <groupId>com.mybatis-flex</groupId> <artifactId>mybatis-flex-spring-boot-starter</artifactId> <version>1.5.3</version> </dependency> <dependency> <groupId>com.mybatis-flex</groupId> <artifactId>mybatis-flex-codegen</artifactId> <version>1.5.3</version> </dependency>
mybatis-flex yml配置
mybatis-flex: # 启动时检查是否存在 MyBatis XML 文件,默认不检查 check-config-location: false # 是否控制台打印 MyBatis-Flex 的 LOGO 及版本号 global-config: print-banner: true normal-value-of-logic-delete: 0 deleted-value-of-logic-delete: 1
生成器,修改为自己的配置即可
package gen; import com.mybatisflex.codegen.Generator; import com.mybatisflex.codegen.config.GlobalConfig; import com.zaxxer.hikari.HikariDataSource; import org.junit.Test; /** * @author 程序员星星 * @date 2023/7/27 * @Description */ public class Codegen { //数据库名和模块名(这里数据库名和模块名一样) private static final String databaseName = "five_cube_test"; //主机 ip private static final String host = "192.168.157.129"; //数据库用户名 private static final String username = "root"; //数据库密码 private static final String password = "root"; //生成代码的路径 private static final String path = "D:\\myProject\\my\\test_mybatis_flex"; //表名 private static final String tableName = "user"; //作者 private static final String author = "lzx"; //父包模块名 private static final String moduleName = "mybatisFlex"; //逻辑删除字段 private static final String logicDeleteColumn = "status"; //乐观锁字段 private static final String versionColumn = "version"; @Test public void genCode() { //配置数据源 HikariDataSource dataSource = new HikariDataSource(); dataSource.setJdbcUrl("jdbc:mysql://" + host + ":3306/" + databaseName + "?characterEncoding=utf-8"); dataSource.setUsername(username); dataSource.setPassword(password); //创建配置内容,两种风格都可以。 GlobalConfig globalConfig = createGlobalConfigUseStyle2(); //通过 datasource 和 globalConfig 创建代码生成器 Generator generator = new Generator(dataSource, globalConfig); //生成代码 generator.generate(); } public static GlobalConfig createGlobalConfigUseStyle2() { //创建配置内容 GlobalConfig globalConfig = new GlobalConfig(); //设置根包 globalConfig.getPackageConfig() .setSourceDir(path + "/src/main/java") .setBasePackage("com." + author + "." + moduleName) .setMapperXmlPath(path + "/src/main/java/com/" + author + "/" + moduleName + "/mapper/xml"); globalConfig.getJavadocConfig() .setAuthor(author); //设置表前缀和只生成哪些表,setGenerateTable 未配置时,生成所有表 globalConfig.getStrategyConfig() // .setTablePrefix("tb_") .setLogicDeleteColumn(logicDeleteColumn) .setVersionColumn(versionColumn) .setGenerateTable(tableName);//生成多个表的写法"sys_user","sys_dept" //设置生成 entity 并启用 Lombok globalConfig.enableEntity() .setWithLombok(true); //设置生成 mapper globalConfig.enableMapper(); //设置生成 mapper.xml globalConfig.enableMapperXml(); //设置生成service globalConfig.enableService(); globalConfig.enableServiceImpl(); //设置生成controller globalConfig.enableController(); //设置生成tableDef globalConfig.enableTableDef(); return globalConfig; } }
config配置类(可选)
package com.lzx.mybatisFlex.config; import com.mybatisflex.core.FlexGlobalConfig; import com.mybatisflex.core.audit.AuditManager; import com.mybatisflex.core.audit.ConsoleMessageCollector; import com.mybatisflex.core.audit.MessageCollector; import com.mybatisflex.spring.boot.MyBatisFlexCustomizer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.Configuration; /** * @author 程序员星星 * @date 2023/6/8 * @Description */ @Configuration public class MyBatisFlexConfiguration implements MyBatisFlexCustomizer { private static final Logger logger = LoggerFactory.getLogger("mybatis-flex-sql"); @Override public void customize(FlexGlobalConfig flexGlobalConfig) { //开启审计功能 AuditManager.setAuditEnable(true); //设置 SQL 审计收集器 MessageCollector collector = new ConsoleMessageCollector(); AuditManager.setMessageCollector(collector); //设置 SQL 审计收集器 AuditManager.setMessageCollector(auditMessage -> logger.info("{},{}ms", auditMessage.getFullSql(), auditMessage.getElapsedTime()) ); } }
到此这篇关于springboot3.0整合mybatis-flex实现逆向工程的示例代码的文章就介绍到这了,更多相关springboot mybatis-flex逆向工程内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!