IDEA Maven Mybatis generator 自動生成代碼

1.創建Maven項目

1、點擊create new project-》maven-》create from archetype->maven-archetype-webapp,然點擊next,步驟如圖:

2、填寫groupId和ArtifactId:(這兩個參數值都是自己定義的)groupid和artifactId被統稱爲“座標”是爲了保證項目唯一性而提出的,如果你要把你項目弄到maven本地倉庫去,你想要找到你的項目就必須根據這兩個id去查找。

一般分爲多個段,這裏我只說兩段,第一段爲域,第二段爲公司名稱。域又分爲org、com、cn等等許多,其中org爲非營利組織,com爲商業組織。舉個apache公司的tomcat項目例子:這個項目的groupId是org.apache,它的域是org(因爲tomcat是非營利項目),公司名稱是apache,artigactId是tomcat。

3、點擊next,配置maven信息,如圖:(本人使用自己配置的maven,也可使用idea默認集成的)

PS:maven默認倉庫在國外,下載速度慢,所以需要在setting.xml文件中配置鏡像.這裏用的是阿里雲鏡像,找到< mirrors> < /mirrors>選項,在裏面添加以下代碼:

<mirror>
  <id>alimaven</id>
  <name>aliyun maven</name>
  <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  <mirrorOf>central</mirrorOf>        
</mirror>

4、點擊next,填寫項目名稱,如圖:

5、創建完成後,項目的結構如圖,在生成代碼之前,不需要創建其他文件夾,但是需要把resources文件夾設置成Resources Root(右鍵點擊resources文件夾-》Mark Directory As->Resources Root)

2.在pom文件中添加plugin

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.7</version>
    <configuration>
        <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
        <verbose>true</verbose>
        <overwrite>true</overwrite>
    </configuration>
</plugin>

3.添加依賴jar包

        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.6</version>
        </dependency>

4.添加properties文件

driverUrl=C:\\Users\\15581\\.m2\\repository\\mysql\\mysql-connector-java\\5.1.6\\mysql-connector-java-5.1.6.jar
driverClass=com.mysql.jdbc.Driver
connectionURL=jdbc:mysql://localhost:3306/jxcshgl
userId=root
password=root
targetPackageModel=cn.zyr.pojo
targetProjectModel=src\\main\\java
targetPackageMapper=mapping
targetProjectMapper=src\\main\\resources
targetPackageDao=cn.zyr.dao
targetPackageDaoImpl=cn.zyr.dao.impl
targetProjectDao=src\\main\\java
tableName=erp_customer

5.添加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>
    <properties resource="generator.properties"/>
    <classPathEntry location="${driverUrl}" />
    <context id="test" targetRuntime="MyBatis3">
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
        <commentGenerator>
            <!-- 這個元素用來去除指定生成的註釋中是否包含生成的日期 false:表示保護 -->
            <!-- 如果生成日期,會造成即使修改一個字段,整個實體類所有屬性都會發生變化,不利於版本控制,所以設置爲true -->
            <property name="suppressDate" value="true" />
            <!-- 是否去除自動生成的註釋 true:是 : false:否 -->
            <property name="suppressAllComments" value="false" />
        </commentGenerator>
        <!--數據庫鏈接URL,用戶名、密碼 -->
        <jdbcConnection driverClass="${driverClass}"
                        connectionURL="${connectionURL}" userId="${userId}" password="${password}">
        </jdbcConnection>
        <javaTypeResolver>
            <!-- This property is used to specify whether MyBatis Generator should
             force the use of java.math.BigDecimal for DECIMAL and NUMERIC fields, -->
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>
        <!-- 生成模型的包名和位置 文件夾自己定義-->
        <javaModelGenerator targetPackage="cn.zyr.pojo"
                            targetProject="src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置 文件夾自己定義-->
        <sqlMapGenerator targetPackage="mapping"
                         targetProject="src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>
        <!-- 生成DAO的包名和位置 文件夾自己定義-->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="cn.zyr.dao"
                             implementationPackage="cn.zyr.dao.impl"
                             targetProject="src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!-- 要生成哪些表 -->
        <table tableName="erp_customer" domainObjectName="customer"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="fasle" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
    </context>

</generatorConfiguration>

一定要在配置文件中加入本地的mysql-connector-java-5.1.43-bin.jar,若同本文使用maven引入,則如下操作即可找到驅動路徑:

找到mysql-connector-java-5.1.6.jar郵件Copy Path即可!!!

6.執行自動生成

1、點擊run->Edit configurations,如圖:

2、之後彈出運行配置框,爲當前配置配置一個名稱,這裏其名爲"generator",然後在 “Command line” 選項中輸入

mybatis-generator:generate -e

這裏加了“-e ”選項是爲了讓該插件輸出詳細信息,這樣可以幫助我們定位問題。

3、配置完成後,點擊run-》run generator,不出意外的話,控制檯會出現BUILD SUCCESS的info信息。完整的效果如圖所示:

此時所需要的dao、pojo、mapper都生成到對應路徑下,如圖:

有寫得不對的地方,煩請各位大佬指正,非常感謝。

以上這篇IDEA Maven Mybatis generator 自動生成代碼就是coder_周分享給大家的全部內容了,希望能給大家一個參考。

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