mybatis逆向工程之maven工程

心已入冬 提交于 2019-12-25 03:48:49

一、在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>cn.mybatis.generator</groupId>
    <artifactId>mybatis-generator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mysql-connector-java.version>8.0.15</mysql-connector-java.version>
        <mybatis-generator-core.version>1.3.7</mybatis-generator-core.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector-java.version}</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>${mybatis-generator-core.version}</version>
        </dependency>
    </dependencies>
 
    <build>
         <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                    <version>3.3</version>
                </plugin>
                <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>8.0.15</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                         <!--配置文件的路径-->
                         <configurationFile>
                             src/main/resources/generatorConfig.xml
                         </configurationFile>
                         <overwrite>true</overwrite>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

二、在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="spring" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
        <commentGenerator>
            <!-- 这个元素用来去除指定生成的注释中是否包含生成的日期 false:表示保护 -->
            <!-- 如果生成日期,会造成即使修改一个字段,整个实体类所有属性都会发生变化,
                 不利于版本控制,所以设置为true 
            -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
            connectionURL="jdbc:mysql://192.168.1.106:3306/spring?nullCatalogMeansCurrent=true" 
            userId="soldier"
            password="soldier,./">
        </jdbcConnection>
        <javaTypeResolver>
            <!-- This property is used to specify whether MyBatis Generator should 
                force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- model的路径 -->
        <javaModelGenerator targetPackage="cn.spring.pojo"
            targetProject="target">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!--mapper接口路径-->
        <sqlMapGenerator targetPackage="cn.spring.mapper"
            targetProject="target">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!--mapper.xml路径-->
        <javaClientGenerator type="XMLMAPPER"
            targetPackage="cn.spring.mapper" 
            targetProject="target">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>
        <!-- tables -->
        <table tableName="items" domainObjectName="Items"></table>
        <table tableName="orderdetail" domainObjectName="Orderdetail"></table>
        <table tableName="orders" domainObjectName="Orders"></table>
        <table tableName="user" domainObjectName="User"></table>
    </context>
</generatorConfiguration>

 三、右击进入run as 点击maven build  如下图输入:mybatis-generator:generate

  

四、出现如下图所示表示成功

 

======================================== 参考资料 ========================================

【1】https://www.cnblogs.com/youcong/p/8232620.html

【2】https://blog.csdn.net/qq_37057095/article/details/81127276

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!