用maven來搭建並使用mybatis-generator

一、配置項目的pom.xml

        <plugins>

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <configurationFile>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>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.5</version>
                    </dependency>
                </dependencies>
            </plugin>

     <plugins>

二、配置generatorConfig.xml

在src/main/resources下新建generatorConfig.xml,放在哪一個目錄要根據第一步配置的 <configurationFile>

<?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:\mysql-connector-java-5.0.5.jar" />
    <context id="context1">
        <commentGenerator>
            <!-- 是否去除自動生成的註釋 true:是 : false:否 --> 
           <property name="suppressDate" value="true" />
            <property name="suppressAllComments" value="true" />
        </commentGenerator>   

          <!-- connectionURL:要寫你自己新建的數據庫的名字,確保數據庫中已經建好了表,用戶名,密碼要正確 --> 
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
            connectionURL="jdbc:mysql://127.0.0.1:3306/crm  userId="root"
            password="123456" />
        <!-- 生成實體類 實體bean文件,需要在src/main/java建立目錄com.abc.po-->
        <javaModelGenerator targetPackage="com.abc.po"
            targetProject="src/main/java" />
        <!-- mapper xml文件,需要在src/main/java建立目錄com.abc.dao-->
        <sqlMapGenerator targetPackage="com.abc.dao"
            targetProject="src/main/java" />
        <!-- mapper 接口文件,需要在src/main/java建立目錄com.abc.mapper -->
        <javaClientGenerator targetPackage="com.abc.mapper"
            targetProject="src/main/java" type="XMLMAPPER" /> 

       <!-- 要與數據庫表的名稱一致,enableCountByExample表示是否生成對應的example,false:不生成 -->
        <table schema="" tableName="user_info" enableCountByExample="false"
            enableUpdateByExample="false" enableDeleteByExample="false"
            enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table schema="" tableName="view_info" enableCountByExample="false"
            enableUpdateByExample="false" enableDeleteByExample="false"
            enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

三、使用命令:maven  mybatis-generator:generate 

點擊項目,右鍵maven--->maven build--->輸入命令mybatis-generator:generate 等待後臺結果。

注意:可能會出現錯誤,一般是數據庫驅動的路徑不對,找不到generatorConfig.xml配置文件,沒有預先建立好相應的目錄,如src/main/java下建立目錄com.abc.mapper。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章