SpringBoot中使用mybatis-generator自動生產


步驟:

1.在pom.xml中添加插件配置

<plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <dependencies>
                    <dependency>
                        <groupId> mysql</groupId>
                        <artifactId> mysql-connector-java</artifactId>
                        <version> 5.1.39</version>
                    </dependency>
                    <dependency>
                        <groupId>org.mybatis.generator</groupId>
                        <artifactId>mybatis-generator-core</artifactId>
                        <version>1.3.5</version>
                    </dependency>
                </dependencies>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!--允許移動生成的文件 -->
                    <verbose>true</verbose>
                    <!-- 是否覆蓋 -->
                    <overwrite>true</overwrite>
                    <!-- 自動生成的配置 -->
                    <configurationFile>
                        src/main/resources/mybatis-generator.xml</configurationFile>
                </configuration>
            </plugin>

2.在resources文件夾中添加mybatis-generator.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="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--數據庫鏈接地址賬號密碼-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/longhubang" userId="root" password="hongda$123456">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--生成Model類存放位置-->
        <javaModelGenerator targetPackage="com.example.scheduleService.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成映射文件存放位置-->
        <sqlMapGenerator targetPackage="com.example.scheduleService.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--生成Dao類存放位置-->
        <!-- 客戶端代碼,生成易於使用的針對Model對象和XML配置文件 的代碼
                type="ANNOTATEDMAPPER",生成Java Model 和基於注解的Mapper對象
                type="MIXEDMAPPER",生成基於注解的Java Model 和相應的Mapper對象
                type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口
        -->
        <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="com.example.scheduleService.dao" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!--生成對應表及類名-->
        <!--<table tableName="stocktradeinfo" domainObjectName="StockTradeInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
        <table tableName="theme" domainObjectName="Theme" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="stockTheme" domainObjectName="StockTheme" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    </context>
</generatorConfiguration>

上面的是不帶xml的配置,

生成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">
  <!-- 第一種mybatis逆向生成xml配置 -->
<generatorConfiguration>

<context id="sqlserverTables" targetRuntime="MyBatis3">
  <!-- 生成的pojo,將implements Serializable-->
  <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
  <commentGenerator>
    <!-- 是否去除自動生成的注釋 true:是 : false:否 -->
    <property name="suppressAllComments" value="true" />
  </commentGenerator>

  <!-- 數據庫鏈接URL、用戶名、密碼 -->
  <jdbcConnection driverClass="com.mysql.jdbc.Driver"
    connectionURL="jdbc:mysql://18.16.200.42:3306/personnel-dev"
    userId="root"
    password="shitou$root">
  </jdbcConnection>

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

  <!--
  生成model模型,對應的包路徑,以及文件存放路徑(targetProject),targetProject可以指定具體的路徑,如./src/main/java,
  也可以使用“MAVEN”來自動生成,這樣生成的代碼會在target/generatord-source目錄下
  -->
  <!--<javaModelGenerator targetPackage="com.joey.mybaties.test.pojo" targetProject="MAVEN">-->
  <javaModelGenerator targetPackage="com.jsy.order.mybatis.entity" targetProject="./src/main/java">
    <property name="enableSubPackages" value="true"/>
    <!-- 從數據庫返回的值被清理前后的空格  -->
    <property name="trimStrings" value="true" />
  </javaModelGenerator>

  <!--對應的mapper.xml文件  -->
  <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
    <property name="enableSubPackages" value="true"/>
  </sqlMapGenerator>

  <!-- 對應的Mapper接口類文件 -->
  <javaClientGenerator type="XMLMAPPER" targetPackage="com.jsy.order.mybatis.dao" targetProject="./src/main/java">
    <property name="enableSubPackages" value="true"/>
  </javaClientGenerator>


  <!--生成對應表及類名-->
  <!--<table tableName="stocktradeinfo" domainObjectName="StockTradeInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>-->
  <table tableName="tb_order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>

</context>
</generatorConfiguration>

 

3.根據配置創建對應的model,mapper,dao文件夾

4.使用maven中的mybatis-generator:generate根據數據庫里面表生產相關的類,Mapper

注意:

這里面自動生產的Mapper

1.沒有@Mapper注解

2.在insert的時候會帶上主鍵ID

 

如何給生成字段為駝峰值或者跟表字段名稱一樣

  <table tableName="configInfo" domainObjectName="ConfigInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <property name="useActualColumnNames" value="true"/>
        </table>

添加屬性useActualColumnNames為true,那么生成的對象字段就跟表一樣

 

命令生成代碼:

mvn mybatis-generator:generate

 

 

http://www.cnblogs.com/hyyq/p/7087620.html

http://blog.csdn.net/yezhuanxu/article/details/53483130

http://blog.csdn.net/gzg1001/article/details/51935948


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM