1. 程式人生 > >使用 mybatis-Generator 自動生成DAO、Model、Mapping相關檔案

使用 mybatis-Generator 自動生成DAO、Model、Mapping相關檔案

1、Maven專案

2、配置generatorConfig.xml檔案

3、在pom.xml中配置外掛

 

  2、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="default" targetRuntime="MyBatis3Simple"> <!--建立class時,對註釋進行控制--> <commentGenerator> <property name="suppressDate" value="true" /> <!--去除註釋--> <property name="suppressAllComments"
value="true"/> </commentGenerator> <!--jdbc的資料庫連線--> <jdbcConnection driverClass="org.mariadb.jdbc.Driver" connectionURL="jdbc:mariadb://localhost:3306/data_test" userId="oukele" password="oukele"> </jdbcConnection
> <!-- Model模型生成器 targetPackage -> 指定生成的model生成所在的包名 targetProject -> 指定在該專案下所在的路徑 --> <javaModelGenerator targetPackage="com.oukele.model" targetProject="src/main/java"> <!-- 是否對類CHAR型別的列的資料進行trim操作 --> <property name="trimStrings" value="true" /> </javaModelGenerator> <!--Mapper對映檔案生成所在的目錄 為每一個數據庫的表生成對應的SqlMap檔案--> <sqlMapGenerator targetPackage="DynamicSql.xml" targetProject="src/main/resources"/> <!-- 客戶端程式碼,生成易於使用的針對Model物件和XML配置檔案 的程式碼 type="ANNOTATEDMAPPER",生成Java Model 和基於註解的Mapper物件 type="MIXEDMAPPER",生成基於註解的Java Model 和相應的Mapper物件 type="XMLMAPPER",生成SQLMap XML檔案和獨立的Mapper介面 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.oukele.dao1" targetProject="src/main/java"/> <!-- tableName 表名 % -> 全部表 --> <table tableName="%"> <generatedKey column="id" sqlStatement="Mysql"/> </table> </context> </generatorConfiguration>
View Code

  3、在pom.xml檔案中配置外掛

<?xml version="1.0" encoding="UTF-8"?>
<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>com.oukele</groupId>
    <artifactId>mybatisdemo</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

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

        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>2.3.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>

                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.mariadb.jdbc</groupId>
                        <artifactId>mariadb-java-client</artifactId>
                        <version>2.3.0</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>
View Code

 

 使用外掛,自動生成檔案

 

 

結果:

 

詳細資訊:請點選此處。