mybatis 逆向工程 maven

mybatis 逆向工程:mybatis官方提供逆向工程 可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml、po..)

逆向工程开发有两种方式:1、通过Java工程,2、通过maven工程

一、Java工程(不做详细说明)

下载地址:https://github.com/mybatis/generator/releases

下载解压

二、maven工程

  Ⅰ、创建maven工程

    

  Ⅱ、添加依赖

<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 
https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.xxx.marw</groupId>
  <artifactId>mybatis-generator</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>
 <dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.4.0</version>
</dependency>
<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.4.0</version>
</dependency>
    </dependencies>

    <!--mybatis.generator逆向工程,maven版本主要配置下面信息-->
    <build>
        <plugins>
            <!--maven插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <!--mybatis-generator插件-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</version>
                <!--指定资源文件的路径-->
                <configuration>
                    <configurationFile>srcmain
esourcesgeneratorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
                <!--此插件需要依赖的jar包资源-->
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>8.0.26</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>
View Code

  Ⅲ、添加 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="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://192.168.223.129:3306/mysql_db_char"
                        userId="root"
                        password="root">
        </jdbcConnection>
        <!-- <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
            connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:orcl"
            userId="scott"
            password="wcy675600920">
        </jdbcConnection> -->

        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
            NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- targetProject:生成PO类的位置 -->
        <javaModelGenerator targetPackage="com.xxx.marw.pojo"
                            targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- targetProject:mapper映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="com.xxx.marw.mapper"
                         targetProject="src/main/resources">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </sqlMapGenerator>
        <!-- targetPackage:mapper接口生成的位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.xxx.marw.mapper"
                             targetProject="src/main/java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>

        <!-- 指定数据库表 -->
        <table tableName="item" schema="" enableCountByExample="false"
               enableDeleteByExample="false" enableUpdateByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">

        </table>
        <table tableName="orderdetail" schema="" enableCountByExample="false"
               enableDeleteByExample="false" enableUpdateByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">

        </table>
        <table tableName="orders" schema="" enableCountByExample="false"
               enableDeleteByExample="false" enableUpdateByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">

        </table>
        <table tableName="user" schema="" enableCountByExample="false"
               enableDeleteByExample="false" enableUpdateByExample="false"
               enableSelectByExample="false" selectByExampleQueryId="false">

        </table>


        <!-- 有些表的字段需要指定java类型
         <table schema="" tableName="">
            <columnOverride column="" javaType="" />
        </table> -->
    </context>
</generatorConfiguration>
View Code

  Ⅳ、运行maven工程 (mybatis-generator:generate

    

   注意:1、pom配置的MySQL依赖jar包版本和MySQL服务版本要一致,查看MySQL版本:select version() from dual;

      2、MySQL的驱动可能是这个:com.mysql.cj.jdbc.Driver 

原文地址:https://www.cnblogs.com/WarBlog/p/15001049.html