1. 程式人生 > >idea建立mybatis maven逆向工程

idea建立mybatis maven逆向工程

mybatis逆向工程可以通過資料庫的表,直接生成對應的實體類以及mapper.java mapper.xml檔案,可以大大提高我們的開發效率

建立逆向工程的步驟:
1. 建立maven專案
2. 在pom.xml核心配置檔案中加入mybatis逆向工程外掛

 <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId
>
mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin
>
</plugins> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources
>
</build>

3.為專案新增依賴 -> 資料庫驅動依賴

   <dependencies>
       <dependency>
           <groupId>mysql</groupId>
           <artifactId>mysql-connector-java</artifactId>
           <version>5.1.34</version>
       </dependency>
   </dependencies>

4.建立逆向工程的配置檔案 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>
    <!-- 配置mysql 驅動jar包路徑.用了絕對路徑 -->
    <classPathEntry
            location="E:\maven\LocalWareHouse\mysql\mysql-connector-java\5.1.34\mysql-connector-java-5.1.34.jar" />

    <context id="scm_mysql_tables" targetRuntime="MyBatis3">
        <!-- 防止生成的程式碼中有很多註釋,加入下面的配置控制 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
            <property name="suppressDate" value="true" />
        </commentGenerator>

        <!-- 資料庫連線 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/easybuy?characterEncoding=UTF-8"
                        userId="root"
                        password="root">
        </jdbcConnection>

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

        <!-- 資料表對應的model層  -->
        <javaModelGenerator targetPackage="com.hd.entity"
                            targetProject="src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- sql mapper 對映配置檔案 -->
        <sqlMapGenerator targetPackage="com.hd.mapper"
                         targetProject="src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- mybatis3中的mapper介面 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.hd.mapper"
                             targetProject="src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 資料表進行生成操作 schema:相當於庫名; tableName:表名;
            domainObjectName:對應的DO
        -->
        <table schema="mybatis" tableName="easybuy_news" domainObjectName="News"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

5.雙擊下圖對應位置,自動生成程式碼
這裡寫圖片描述