七.SpringBoot整合MyBatis代码自动生成插件

一、简介

1.1 MyBatis Generator介绍

MyBatis Generator 是MyBatis 官方出品的一款,用来自动生成MyBatis的 mapper、dao、entity 的框架,让我们省去规律性最强的一部分最基础的代码编写。

1.2 MyBatis Generator使用

MyBatis Generator的使用方式有4种:

  • 命令行生成
  • Maven方式生成
  • 使用Ant任务生成
  • 使用Java代码生成

其中推荐使用Maven方式进行代码生成,因为集成和使用比较简单。

1.3 开发环境

MySQL:5.0.12

MyBatis Generator:1.3.7

Maven:4.0

IDEA:2018.2

二、代码自动生成配置

上面介绍了使用MyBatis Generator的几种方式,其中最推荐使用的是Maven方式,所以下面我们来看Maven方式的MyBatis代码生成,分为四步:

Step1:添加依赖

配置pom.xml文件,增加依赖和配置生成文件(“generatorConfig.xml”)路径:

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.7</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>Generate MyBatis Artifacts</id>
            <phase>package</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <!--允许移动生成的文件 -->
        <verbose>true</verbose>
        <!-- 是否覆盖 -->
        <overwrite>true</overwrite>
        <!-- 自动生成的配置 -->
        <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
    </configuration>
</plugin>

Step2:添加配置文件

根据上面在pom里的配置,我们需要添加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>  
<!-- ���ݿ�����-->  
    <classPathEntry  location="E:\MavenRepository\mysql\mysql-connector-java\5.1.30\mysql-connector-java-5.1.30.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">  
        <commentGenerator>  
            <property name="suppressDate" value="true"/>  
            <!-- �Ƿ�ȥ���Զ����ɵ�ע�� true���� �� false:�� -->  
            <property name="suppressAllComments" value="true"/>  
        </commentGenerator>  
        <!--���ݿ�����URL���û��������� -->  
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/flybbs" userId="root" password="root">
        </jdbcConnection>  
        <javaTypeResolver>  
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>  
        <!-- ����ģ�͵İ�����λ��-->  
        <javaModelGenerator targetPackage="com.neusoft.domain" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>  
            <property name="trimStrings" value="true"/>  
        </javaModelGenerator>  
        <!-- ����ӳ���ļ��İ�����λ��-->  
        <sqlMapGenerator targetPackage="com.neusoft.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>  
        </sqlMapGenerator>  
        <!-- ����DAO�İ�����λ��-->  
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.neusoft.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>  
        </javaClientGenerator>  
        <!-- Ҫ���ɵı� tableName�����ݿ��еı�������ͼ�� domainObjectName��ʵ������-->  
        <table tableName="tab_user" domainObjectName="User2" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <!--<table tableName="tab_topic_category" domainObjectName="TopicCategory" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="tab_topic" domainObjectName="Topic" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="tab_comment" domainObjectName="Comment" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="tab_user_collect_topic" domainObjectName="UserCollectTopic" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="tab_user_message" domainObjectName="UserMessage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="tab_user_qiandao" domainObjectName="UserQiandao" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <!--<table tableName="tab_user_message" domainObjectName="UserMessage" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->

    </context>  
</generatorConfiguration>

其中数据库连接的配置,是从application.properties直接读取的。

Step3:点击Maven生成代码

如果你使用的是IDEA,点击最右侧的Maven Projects => 点击mybatis-generator => 右键mybatis-generator:generate => Run Maven Build,如下图所示:

468490-c19ad7a68be02253.png
image.png

正常控制台输出“BUILD SUCCESS”说明生成已经成功了,如果出现错误,根据错误提示信息排除处理错误即可。

猜你喜欢

转载自blog.csdn.net/weixin_34138139/article/details/87546222