mybatis-generator的配置

pom.xml文件

<!--mybatis逆向生成工具的核心jar-->
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.5</version>
      </plugin>

generatorConfig.xml

注意只能用這個generatorConfig名字,若是是其它的名字識別不了java

<?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:/Maven/repo/m2/mysql/mysql-connector-java/5.1.27/mysql-connector-java-5.1.27.jar"/>
    <!--生成對象環境 targetRuntime= MyBatis3:默認的值,還有一種是MyBatis3Simple-->

    <context id="mybatis" targetRuntime="MyBatis3Simple">
        <commentGenerator>
            <property name="suppressDate" value="false"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--數據庫相關設置-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/spring"
                        userId="root"
                        password="123"/>

        <!--實體放置目錄-->
        <javaModelGenerator targetPackage="cn.yanxi.model"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--mybatis映射xml文件放置位置-->
        <sqlMapGenerator targetPackage="config.mapping"
                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!--mapper接口-->
        <javaClientGenerator targetPackage="cn.yanxi.dao"
                             targetProject="src/main/java"
                             type="XMLMAPPER">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
 <!--能夠配置多個表-->
        <table tableName="user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

javaClientGenerator的type值mysql

  • type:選擇怎麼生成mapper接口(在MyBatis3/MyBatis3Simple下):
  • ANNOTATEDMAPPER:會生成使用Mapper接口+Annotation的方式建立(SQL生成在annotation中),不會生成對應的XML;
  • MIXEDMAPPER:使用混合配置,會生成Mapper接口,並適當添加合適的Annotation,可是XML會生成在XML中;
  • XMLMAPPER:會生成Mapper接口,接口徹底依賴XML;
注意,若是context是MyBatis3Simple:只支持ANNOTATEDMAPPER和XMLMAPPER

table參數說明

  • domainObjectName:若是不設置,使用表名做爲domain類的名字;
  • enableInsert(默認true):指定是否生成insert語句;
  • enableSelectByPrimaryKey(默認true):指定是否生成按照主鍵查詢對象的語句(就是getById或get);
  • enableSelectByExample(默認true):MyBatis3Simple爲false,指定是否生成動態查詢語句;
  • enableUpdateByPrimaryKey(默認true):指定是否生成按照主鍵修改對象的語句(即update);
  • enableDeleteByPrimaryKey(默認true):指定是否生成按照主鍵刪除對象的語句(即delete);
  • enableDeleteByExample(默認true):MyBatis3Simple爲false,指定是否生成動態刪除語句;
  • enableCountByExample(默認true):MyBatis3Simple爲false,指定是否生成動態查詢總條數語句(用於分頁的總條數查詢);
  • enableUpdateByExample(默認true):MyBatis3Simple爲false,指定是否生成動態修改語句(只修改對象中不爲空的屬性);
  • modelType:參考context元素的defaultModelType,至關於覆蓋;

參照於網上的一些博文spring

###運行 測試 輸入圖片說明sql

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building ssmTest Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- mybatis-generator-maven-plugin:1.3.5:generate (default-cli) @ ssmTest ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.741 s
[INFO] Finished at: 2016-11-19T13:45:23+08:00
[INFO] Final Memory: 10M/155M
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0
相關文章
相關標籤/搜索