Fork me on GitHub

mybatis-generator使用

mybatis-generate使用

第一步导入依赖

 <plugin>
        <!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.2</version>
        <configuration>
          <!--配置文件的位置-->
          <configurationFile>src/main/resources/mybatis-generator-config.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.2</version>
          </dependency>
        </dependencies>
      </plugin>

第二步配置 mybatis-generator-config.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>

  <!-- 本地数据库驱动程序jar包的全路径 -->
  <classPathEntry
    location="D:\repo\mysql\mysql-connector-java\5.1.47\mysql-connector-java-5.1.47.jar"/>

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

    <!-- 数据库的相关配置 -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver"
      connectionURL="jdbc:mysql://xxxx"
      userId="root"
      password="xxxx"/>

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

    <!-- 实体类生成的位置 -->
    <javaModelGenerator targetPackage="com.xxx" targetProject="src/main/resources">
    <property name="enableSubPackages" value="false"/>
    <property name="trimStrings" value="true"/>
    </javaModelGenerator>

    <!-- *Mapper.xml 文件的位置 -->
    <sqlMapGenerator targetPackage="com.xxx"
      targetProject="src/main/resources">
      <property name="material" value="false"/>
    </sqlMapGenerator>

    <!-- Mapper 接口文件的位置 -->
    <javaClientGenerator targetPackage="com.xxxx"
      targetProject="src/main/resources" type="XMLMAPPER">
      <property name="material" value="false"/>
    </javaClientGenerator>

    <!-- 相关表的配置 -->
    <!--<table tableName="db_xxx" enableCountByExample="false"-->
      <!--enableDeleteByExample="false"-->
      <!--enableSelectByExample="false"-->
      <!--enableUpdateByExample="false">-->

      <table tableName="db_shop.tbl_shop_wechat_advertorial"
        domainObjectName="material"
        enableCountByExample="false"
        enableUpdateByExample="false"
        enableDeleteByExample="false"
        enableSelectByExample="false"
        selectByExampleQueryId="false">
    </table>
  </context>
</generatorConfiguration>

第三步 使用插件编译

第四步 保持表中字段命名方式

<property name="useActualColumnNames" value="true" />

posted @ 2018-12-29 12:10  MgicalFool  阅读(189)  评论(0编辑  收藏  举报