Mybatis的逆向工程,自动生成代码(Mapper,xml,bean)

步骤:

1. 新建一个Maven项目:

 然后导入maven依赖:

 <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!--mybatis自动生成代码插件配置-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.2</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

其中:

<configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
是反向自动生成代码的配置文件位置!
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="D:jarmysql-connector-java-5.1.38.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <!-- 生成的Java文件的编码 -->
        <property name="javaFileEncoding" value="UTF-8" />
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
            <property name="useActualColumnNames" value="true" />

        </commentGenerator>
        <!--数据库链接URL,用户名、密码
           1.一般jdbc数据库的版本6.x以上,都是com.mysql.cj.jdbc.Driver  其他的低版本就是com.mysql.jdbc.Driver
         -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/ssm_01?serverTimezone=GMT" userId="root" password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成模型的包名和位置  要生成的文件位置  -->
        <javaModelGenerator targetPackage="com.bean" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置   UserMapper.xml对应的xml文件-->
        <sqlMapGenerator targetPackage="com.mapper.mapping" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置   UserMapper.java对应的mapper文件 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
        <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
        </table>
        <table tableName="t_commodity" domainObjectName="Commodity" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

这里会有几个问题:

1)"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" 会报红,这个时候需要如下操作:

 如此就操作完成了:

 2)驱动包的导入:

<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
<classPathEntry location="D:jarmysql-connector-java-5.1.38.jar"/>

 这个文件是我们的本地文件,需要我们自己下载导入:

下载链接(MySQL):https://mvnrepository.com/artifact/mysql/mysql-connector-java

 

 这时就会下载,然后把下载完的文件的路径放到代码中:

<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
<classPathEntry location="你下载的文件路径"/>

3)数据库连接的数据:

<!--数据库链接URL,用户名、密码
1.一般jdbc数据库的版本6.x以上,都是com.mysql.cj.jdbc.Driver 其他的低版本就是com.mysql.jdbc.Driver
-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/你的数据库名称?serverTimezone=GMT" userId="你的账号" password="你的密码">
这个标签的所有东西都要相应改成你的信息

4)在数据库中创建表,然后在generatorConfig.xml中进行修改:

<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
<table tableName="t_commodity" domainObjectName="Commodity" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
</table>
下面是我一个表的截图:

5)关于生成文件的位置请仔细查看generatorConfig.xml 中相应的注释,这里不在详细说明

项目结构图如下:

 最后,在idea中:

 如果没有该按钮,请点击如下:

 

 代码就自动生成了:

!!!请看

关于 逆向代理:由于每次都要生成一次,暂时没有想到其它办法来阻止它在每次启动的时候来生成代码,我目前的做法是把它注释掉(pom.xml中的插件)

如果不注释,那么mapper.xml文件中,每次都会生成相同的代码!

 附录:

mybatis.org/dtd/mybatis-generator-config_1_0.dtd标红

Mybatis中接口和对应的mapper文件位置配置深入剖析

Mybatis中接口和对应的mapper文件位置配置详解

Spring配置中的"classpath:"与"classpath*:"的区别研究

关于mybatis无法映射到mapper.xml文件的解决方案(可能与maven有关)

关于mybatis无法映射到mapper.xml文件的解决方案(可能与maven有关)

原文地址:https://www.cnblogs.com/Anxc/p/12058143.html