Intellij IDEA 中使用 MyBatis-generator 自動生成 MyBatis 代碼

一、IDEA建立maven工程(略)java

二、 在maven項目的pom.xml 添加mybatis-generator-maven-plugin 插件和MySQL數據庫驅動依賴mysql

<build>
  <plugins>
    <plugin>
      <groupId>org.mybatis.generator</groupId>
      <artifactId>mybatis-generator-maven-plugin</artifactId>
      <version>1.3.5</version>
      <configuration>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
      </configuration>
    </plugin>
  </plugins>
</build>

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.34</version>
</dependency>

三、在maven項目下的src/main/resources 目錄下創建名爲 generatorConfig.xml的配置文件,做爲mybatis-generator-maven-plugin 插件的執行目標,模板以下:sql

<?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>
    <!-- 配置mysql 驅動jar包路徑.用了絕對路徑 -->
    <classPathEntry location="/Users/wangyongzhi/.m2/repository/mysql/mysql-connector-java/5.1.34/mysql-connector-java-5.1.34.jar" />
  
    <context id="wangyongzhi_mysql_tables" targetRuntime="MyBatis3">
        <!-- 防止生成的代碼中有不少註釋,加入下面的配置控制 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
            <property name="suppressDate" value="true" />
        </commentGenerator>

        <!-- 數據庫鏈接 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8"
                        userId="root"
                        password="123456">
        </jdbcConnection>

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

        <!-- 數據表對應的model層  -->
        <javaModelGenerator targetPackage="me.zhige.domain" targetProject="src">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!-- sql mapper 映射配置文件 -->
        <sqlMapGenerator targetPackage="me.zhige.mapper"  targetProject="src">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!-- mybatis3中的mapper接口 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="me.zhige.dao"  targetProject="src">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 數據表進行生成操做 schema:至關於庫名; tableName:表名; domainObjectName:對應的DO -->
        <table schema="test" tableName="users" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

    </context>
</generatorConfiguration>

四、使用maven運行mybatis-generator-maven-plugin插件:工程名->Plugins->mybatis-generator->mybatis-generator:generate->Run Maven Build數據庫

圖片發自簡書App

五、自動生成的結構以下:mybatis

img

福利:我這裏有一些電子書的存貨,估計有上萬本吧,各個類型的都有,關注個人公衆號「志哥的成長筆記」(ID:zhige-me),後臺回覆 「閱讀」 獲取。掃描下方二維碼關注。app

相關文章
相關標籤/搜索