代码生成器插件(mybatis-generator)

代码生成器插件

1. 新建工程

建议:正式项目和代码生成分别创建一个项目

复制了上次的javaEE工程规范化的最终工程

初始pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.teach</groupId>
    <artifactId>mybatis-generator</artifactId>
    <version>0.1</version>
    <packaging>war</packaging>
    
    <!-- 自定义配置信息 -->
    <properties>
        <charset>UTF-8</charset>
        <jdk.version>1.8</jdk.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <argLine>-Dfile.encoding=UTF-8</argLine>
        <slf4j.version>1.7.25</slf4j.version>
    </properties>
​
    <dependencies>
        
    </dependencies>
    
    
    <build>
​
        <plugins>
            <!-- 工程编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                    <encoding>${charset}</encoding>
                </configuration>
            </plugin>
​
        </plugins>
    </build>
​
</project>

2. 使用代码生成插件

2.1. 添加插件依赖

 <!-- 插件运行命令:mybatis-generator:generate -X -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <!-- 插件依赖的数据库驱动 -->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>5.1.28</version>
                    </dependency>
                </dependencies>
            </plugin>

2.2. 编写插件配置文件

文件位置:src/main/resources/generatorConfig.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="context1">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 注释开关 -->
            <property name="suppressAllComments" value="false"/>
        </commentGenerator>
​
        <jdbcConnection 
            connectionURL="jdbc:mysql://127.0.0.1:3306/user_product_news"
            driverClass="com.mysql.jdbc.Driver"
            password="" 
            userId="root" />
            
        <javaModelGenerator 
            targetPackage="com.teach.entity" 
            targetProject="src/main/java" />
            
        <sqlMapGenerator 
            targetPackage="com.teach.mapper"
            targetProject="src/main/resources" />
        <javaClientGenerator 
            targetPackage="com.teach.mapper"
            targetProject="src/main/java" 
            type="XMLMAPPER" />
​
        <!-- 下面添加需要自动生成MyBatis代码的表 -->
        <!-- 
        <table schema="" tableName="news" domainObjectName="News" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
         -->
        <table schema="" tableName="%" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>
    </context>
</generatorConfiguration>

3. 运行代码生成器

mvn mybatis-generator:generate -X

4.上面是笔记,下面是我的配置文件

generatorConfig.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="context1">
		<commentGenerator>
			<property name="suppressDate" value="flase" />
			<!-- 注释开关 -->
			<property name="suppressAllComments" value="false" />
		</commentGenerator>
		<jdbcConnection
			connectionURL="jdbc:mysql://121.4.253.60:3306/bkpt?useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=GMT%2B8"
			driverClass="com.mysql.jdbc.Driver" 
			password="123456"
			userId="bkpt" />
		<javaModelGenerator 
			targetPackage="com.hx.entity"
			targetProject="src/main/java" />
		<sqlMapGenerator 
			targetPackage="com.hx.mapper"
			targetProject="src/main/resources" />
		<javaClientGenerator 
			targetPackage="com.hx.mapper"
			targetProject="src/main/java" 
			type="XMLMAPPER" />
		<!-- 下面添加需要自动生成MyBatis代码的表 -->
		<!-- <table schema="" tableName="news" domainObjectName="News" enableCountByExample="false" 
			enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/> -->
		<table schema="bkpt" tableName="%" enableCountByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false"
			enableUpdateByExample="false" />
	</context>
</generatorConfiguration>

 pom.xml:

<?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"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hx</groupId>
    <artifactId>bkpt</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!-- 自定义配置信息 -->
    <properties>
        <charset>UTF-8</charset>
        <jdk.version>1.8</jdk.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <argLine>-Dfile.encoding=UTF-8</argLine>
        <slf4j.version>1.7.25</slf4j.version>
    </properties>

    <dependencies>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.5.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>2.5.0-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
    </dependencies>


    <build>

        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <!-- 插件依赖的数据库驱动 -->
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.19</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus是一个MyBatis的增强工具,在MyBatis的基础上进行了扩展,提供了许多实用的功能,如分页、性能分析等。而MyBatis-Plus GeneratorMyBatis-Plus的代码生成器,可以根据数据库表自动生成对应的Java文件。 MyBatis-Plus Generator默认生成的Java文件包括实体类、Mapper接口、Mapper XML文件、Service接口、ServiceImpl实现类。但是,有时我们需要生成其他类型的Java文件,比如DTO、VO、Convertor等。这时候,我们可以通过自定义模板来实现。 以下是自定义模板生成DTO、VO、Convertor的步骤: 1. 修改MyBatis-Plus Generator配置文件 在MyBatis-Plus Generator配置文件中,找到table的配置项,添加以下两个配置项: ```xml <table schema="" tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <!-- 自定义模板路径 --> <property name="dtoTargetPackage" value="com.example.demo.dto"/> <property name="dtoTargetProject" value="src/main/java"/> <property name="voTargetPackage" value="com.example.demo.vo"/> <property name="voTargetProject" value="src/main/java"/> <property name="convertorTargetPackage" value="com.example.demo.convertor"/> <property name="convertorTargetProject" value="src/main/java"/> </table> ``` 其中,dtoTargetPackage、dtoTargetProject分别表示DTO类的包名和生成路径;voTargetPackage、voTargetProject分别表示VO类的包名和生成路径;convertorTargetPackage、convertorTargetProject分别表示Convertor类的包名和生成路径。 2. 编写自定义模板 在MyBatis-Plus Generator配置文件中,找到context的配置项,添加以下三个配置项: ```xml <context id="default" targetRuntime="MyBatis3"> <!-- 自定义模板 --> <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" /> <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> <plugin type="com.github.chitgoksar.mybatismbplus.generator.plugins.DtoPlugin" /> <plugin type="com.github.chitgoksar.mybatismbplus.generator.plugins.VoPlugin" /> <plugin type="com.github.chitgoksar.mybatismbplus.generator.plugins.ConvertorPlugin" /> </context> ``` 其中,DtoPlugin、VoPlugin、ConvertorPlugin分别表示生成DTO、VO、Convertor类的插件。 接着,创建相应的模板文件,如下: DTO模板: ```java package ${dtoPackage}; import lombok.Data; @Data public class ${dtoName} { // TODO: 添加DTO属性 } ``` VO模板: ```java package ${voPackage}; import lombok.Data; @Data public class ${voName} { // TODO: 添加VO属性 } ``` Convertor模板: ```java package ${convertorPackage}; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; import ${entityPackage}.${entityName}; import ${dtoPackage}.${dtoName}; import ${voPackage}.${voName}; @Mapper public interface ${convertorName} { ${convertorName} INSTANCE = Mappers.getMapper(${convertorName}.class); ${dtoName} toDto(${entityName} entity); ${entityName} toEntity(${dtoName} dto); ${voName} toVo(${entityName} entity); } ``` 其中,${dtoPackage}、${voPackage}、${convertorPackage}分别表示生成的DTO、VO、Convertor类的包名;${dtoName}、${voName}、${convertorName}、${entityName}分别表示生成的类名和实体类名。 3. 运行MyBatis-Plus GeneratorMyBatis-Plus Generator配置文件中,找到generator的配置项,运行MyBatis-Plus Generator即可生成相应的Java文件。 ```xml <generatorConfiguration> <!-- 配置数据源 --> <dataSource type="com.alibaba.druid.pool.DruidDataSource"> <!-- 数据库连接配置 --> </dataSource> <!-- 配置生成的Java文件 --> <context id="default" targetRuntime="MyBatis3"> <!-- 自定义模板 --> <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" /> <plugin type="org.mybatis.generator.plugins.ToStringPlugin" /> <plugin type="com.github.chitgoksar.mybatismbplus.generator.plugins.DtoPlugin" /> <plugin type="com.github.chitgoksar.mybatismbplus.generator.plugins.VoPlugin" /> <plugin type="com.github.chitgoksar.mybatismbplus.generator.plugins.ConvertorPlugin" /> </context> </generatorConfiguration> ``` 通过以上步骤,我们可以自定义模板生成DTO、VO、Convertor等Java文件,提高开发效率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值