背景:MyBatis属于一种半自动的ORM框架,它需要程序员自己编写sql语句和映射文件,但是编写映射文件和sql语句很容易出错,所以mybatis官方提供了Generator生成器,自动生成Entity+Dao+Mapper

MyBatis Generator官网地址:http://mybatis.org/generator/index.html

一、在对应路径下新建generatorConfig.xml配置文件:D:\github\huaxi-test\huaxitest\src\test\resources\generatorConfig.xml

使用Mybatis-generator自动生成Mapper.xml、dao、entity_mysql

 

 配置文件标签的顺序一定要对,不然会报下面这个错,坑得一逼

[ERROR] XML Parser Error on line 55:元素类型为 "context" 的内容必须匹配"(property*,plugin*,commentGenerator?,(connectionFactory|jdbcConnection),javaTypeResolver?,javaModelGenerator,sqlMapGenerator?,javaClientGenerator?,table+)"

 

<?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="D:\maven\.m2\repository\mysql\mysql-connector-java\8.0.25\mysql-connector-java-8.0.25.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.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://rm-2vcgyh64om3x3bd2beo.mysql.cn-chengdu.rds.aliyuncs.com:3306/pre_test?serverTimezone=UTC&amp;useSSL=false"
                        userId="pre_hxgy"
                        password="XHZYJuiRyJqWO*4v">
        </jdbcConnection>

        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- 生成模型Entity的包名和位置,根据实际修改-->
        <javaModelGenerator targetPackage="com.huaxi.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- 生成映射文件mapper.xml的包名和位置-->
        <sqlMapGenerator targetPackage="mapper"  targetProject="src/test/resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- 生成DAO的包名和位置,根据实际修改-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.huaxi.dao"  targetProject="src/main/java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <table tableName="auto_test_date" domainObjectName="AutoTestDate"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false" />
    </context>
</generatorConfiguration>
<classPathEntry location="D:\maven\.m2\repository\mysql\mysql-connector-java\8.0.25\mysql-connector-java-8.0.25.jar" />
这个值取的地方参考:

使用Mybatis-generator自动生成Mapper.xml、dao、entity_java_02

二、在pom.xml文件中添加依赖包,加到这个节点中<build><plugins> ......</plugins></build>

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>${basedir}/src/test/resources/generatorConfig.xml
                    </configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
            </plugin>

三、依次选中Maven-----plugins 双击运行mybatis-generator:generate,自动生成对应的mapper.xml,entity,dao

使用Mybatis-generator自动生成Mapper.xml、dao、entity_xml_03