mybatis-plus 代码生成器 3.5.2

时间: 2023-11-04 21:46:34 浏览: 40
Mybatis-Plus 代码生成器是一个基于 Mybatis-Plus 的代码生成工具,可以帮助开发者快速生成 Mybatis-Plus 的 Mapper、Service、Controller 等代码,提高开发效率。在 Mybatis-Plus 3.5.2 版本中,代码生成器已经稳定可用,并且提供了更多的配置项和模板选项,可以让开发者更加灵活地生成自己需要的代码。如果你使用 Mybatis-Plus 作为数据访问层框架,那么 Mybatis-Plus 代码生成器是一个非常好的工具,可以为你的开发带来很大的便利。
相关问题

mybatis-plus 代码生成器 3.5.2 例子

以下是使用 MyBatis-Plus 代码生成器 3.5.2 自动生成代码的例子: 1. 首先,在项目中添加依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.2</version> </dependency> ``` 2. 然后,创建配置文件 generatorConfig.xml,配置生成代码的相关参数,例如: ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <context id="mysql" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useSSL=false&amp;serverTimezone=UTC" userId="root" password="123456"/> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <javaModelGenerator targetPackage="com.example.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <property name="useActualColumnNames" value="true"/> </table> </context> </generatorConfiguration> ``` 这里配置了连接数据库的参数、生成实体类、Mapper 接口和映射文件的位置等。 3. 创建一个 Java 类,使用 MyBatis-Plus 的代码生成器自动生成代码: ```java public class CodeGenerator { public static void main(String[] args) throws Exception { // 读取 generatorConfig.xml 文件 InputStream inputStream = CodeGenerator.class.getResourceAsStream("/generatorConfig.xml"); ConfigurationParser cp = new ConfigurationParser(Collections.emptyList()); Configuration config = cp.parseConfiguration(inputStream); // 创建代码生成器 DefaultShellCallback callback = new DefaultShellCallback(true); MyBatisGenerator generator = new MyBatisGenerator(config, callback, Collections.emptyList()); // 执行生成代码 generator.generate(null); } } ``` 运行该类,即可自动生成实体类、Mapper 接口和映射文件。

mybatis-plus3.5.2代码生成器

MyBatis-Plus是一个MyBatis的增强工具,在MyBatis的基础上进行了扩展,提供了更加便捷的CRUD操作、分页、乐观锁、逻辑删除等功能。而MyBatis-Plus的代码生成器则是MyBatis-Plus中的一个子模块,用于生成基于MyBatis-Plus的CRUD代码。 MyBatis-Plus的代码生成器是一个基于Velocity模板引擎的代码生成器,支持生成Java、XML、SQL脚本等文件。使用MyBatis-Plus的代码生成器可以快速生成CRUD代码,减少手动编写代码的工作量。 下面是使用MyBatis-Plus3.5.2代码生成器生成代码的步骤: 1. 添加MyBatis-Plus的依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.2</version> </dependency> ``` 2. 编写代码生成器配置文件 在src/main/resources目录下创建generator.properties文件,编写代码生成器的配置信息,例如: ```properties # 数据库配置 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis_plus?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 jdbc.username=root jdbc.password=root # 代码生成器配置 outputDir=D:/code-generator author=MyBatis-Plus fileOverride=true openDir=false ``` 其中,jdbc.driver、jdbc.url、jdbc.username、jdbc.password是数据库的配置信息,outputDir是生成文件的输出目录,author是代码的作者,fileOverride是是否覆盖已有文件,openDir是是否打开输出目录。 3. 编写代码生成器模板文件 在src/main/resources/templates目录下创建模板文件,例如entity.java.vm、mapper.xml.vm、service.java.vm、serviceImpl.java.vm等,编写模板文件的内容。 模板文件中使用Velocity的语法,可以使用变量、条件语句、循环语句等,例如: ```java package ${packageName}; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; /** * ${tableComment} */ @Data @TableName("${tableName}") public class ${entityName} { #foreach($column in $table.columns) /** * ${column.columnComment} */ private ${column.javaType} ${column.fieldName}; #end } ``` 在模板文件中,$packageName、$tableComment、$tableName、$entityName、$table.columns等都是变量,会根据生成器配置文件和数据库表的信息动态替换为具体的值。 4. 运行代码生成器 编写好配置文件和模板文件后,就可以运行代码生成器了。在Java代码中使用CodeGenerator类,并传入配置文件和模板文件的路径,即可启动代码生成器,例如: ```java public class MybatisPlusGenerator { public static void main(String[] args) { String configFile = "generator.properties"; String templatePath = "/templates/%s.vm"; AutoGenerator generator = new AutoGenerator(); // 全局配置 GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setAuthor(PropertyUtil.getProperty(configFile, "author")); globalConfig.setFileOverride(Boolean.parseBoolean(PropertyUtil.getProperty(configFile, "fileOverride"))); globalConfig.setOpen(Boolean.parseBoolean(PropertyUtil.getProperty(configFile, "openDir"))); globalConfig.setOutputDir(PropertyUtil.getProperty(configFile, "outputDir")); generator.setGlobalConfig(globalConfig); // 数据源配置 DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig.setDbType(DbType.MYSQL); dataSourceConfig.setDriverName(PropertyUtil.getProperty(configFile, "jdbc.driver")); dataSourceConfig.setUrl(PropertyUtil.getProperty(configFile, "jdbc.url")); dataSourceConfig.setUsername(PropertyUtil.getProperty(configFile, "jdbc.username")); dataSourceConfig.setPassword(PropertyUtil.getProperty(configFile, "jdbc.password")); generator.setDataSource(dataSourceConfig); // 包配置 PackageConfig packageConfig = new PackageConfig(); packageConfig.setParent(PropertyUtil.getProperty(configFile, "package")); generator.setPackageInfo(packageConfig); // 自定义配置 InjectionConfig injectionConfig = new InjectionConfig() { @Override public void initMap() { } }; injectionConfig.setFileOutConfigList(getFileOutConfigList(templatePath)); generator.setCfg(injectionConfig); // 配置模板 TemplateConfig templateConfig = new TemplateConfig(); templateConfig.setXml(null); generator.setTemplate(templateConfig); // 策略配置 StrategyConfig strategyConfig = new StrategyConfig(); strategyConfig.setNaming(NamingStrategy.underline_to_camel); strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel); strategyConfig.setEntityLombokModel(true); strategyConfig.setRestControllerStyle(true); strategyConfig.setInclude(PropertyUtil.getProperty(configFile, "tableNames").split(",")); generator.setStrategy(strategyConfig); generator.execute(); } private static List<FileOutConfig> getFileOutConfigList(String templatePath) { List<FileOutConfig> fileOutConfigList = new ArrayList<>(); fileOutConfigList.add(new FileOutConfig(templatePath) { @Override public String outputFile(TableInfo tableInfo) { return String.format("%s/%s%s", PropertyUtil.getProperty("generator.properties", "outputDir"), tableInfo.getEntityName(), this.getSuffix()); } }); return fileOutConfigList; } } ``` 在代码中,使用AutoGenerator类配置全局配置、数据源配置、包配置、自定义配置、模板配置、策略配置等,然后调用execute()方法即可生成代码。 总结:使用MyBatis-Plus的代码生成器可以快速生成基于MyBatis-Plus的CRUD代码,减少手动编写代码的工作量,提高开发效率。需要注意的是,生成的代码只是基础代码,需要根据具体业务进行修改和优化。

相关推荐

最新推荐

recommend-type

MyBatis-plus+达梦数据库实现自动生成代码的示例

主要介绍了MyBatis-plus+达梦数据库实现自动生成代码的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

Mybatis-plus基于redis实现二级缓存过程解析

主要介绍了Mybatis-plus基于redis实现二级缓存过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
recommend-type

springboot整合mybatis-plus逆向工程的实现

主要介绍了springboot整合mybatis-plus逆向工程的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

mybatis-plus配置控制台打印完整带参数SQL语句的实现

主要介绍了mybatis-plus配置控制台打印完整带参数SQL语句,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

MyBatis-Plus多表联合查询并且分页(3表联合)

主要介绍了MyBatis-Plus多表联合查询并且分页(3表联合),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。