天天看點

關于在eclipse中如何使用mybatis-generator插件進行代碼自動生成的操作總結

功能:自動生成dao層、pojo層、Mapper.xml檔案

1、先下載下傳插件

    點選 Help---->Eclipse Marketplace..--->搜尋  mybatis 安裝generator插件   記住安裝的是哪個版本

2、在項目的“src/java/resources”目錄下在pom.xml檔案中<bulid>、<plugins>标簽中加入如下代碼:

    版本号是你下載下傳安裝的那個

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

3、建立datasource.properties檔案

db.driverLocation=F:/Maven/repository/mysql-connector-java-5.1.6-bin.jar
db.driverClassName=com.mysql.jdbc.Driver

#db.url=jdbc:mysql://192.1.1.1:3306/mmall?characterEncoding=utf-8
db.url=jdbc:mysql://127.0.0.1:3306/mmall?useUnicode=true&characterEncoding=utf-8
db.username=root
db.password=root


db.initialSize = 20
db.maxActive = 50
db.maxIdle = 20
db.minIdle = 10
db.maxWait = 10
db.defaultAutoCommit = true
db.minEvictableIdleTimeMillis = 3600000           

    注意:

mysql-connector-java-5.1.6-bin.jar           

    一定是要以"-bin.jar"結尾的資料庫驅動包

    如果是直接在電腦上複制檔案存儲路徑,一定要把“\”要改成“/”,否則會報錯的噢

4、在項目的“src/java/resources”目錄下建立mybatisGenerator.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="datasource.properties"></properties>

    <!--指定特定資料庫的jdbc驅動jar包的位置-->
    <classPathEntry location="${db.driverLocation}"/>

    <context id="default" targetRuntime="MyBatis3">

        <!-- optional,旨在建立class時,對注釋進行控制 -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--jdbc的資料庫連接配接 -->
        <jdbcConnection
                driverClass="${db.driverClassName}"
                connectionURL="${db.url}"
                userId="${db.username}"
                password="${db.password}">
        </jdbcConnection>


        <!-- 非必需,類型處理器,在資料庫類型和java類型之間的轉換控制-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>


        <!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類
            targetPackage     指定生成的model生成所在的包名
            targetProject     指定在該項目下所在的路徑
        -->
        <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java">-->
        <javaModelGenerator targetPackage="com.jike.pojo" targetProject="market/src/main/java">
            <!-- 是否允許子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否對model添加 構造函數 -->
            <property name="constructorBased" value="true"/>
            <!-- 是否對類CHAR類型的列的資料進行trim操作 -->
            <property name="trimStrings" value="true"/>
            <!-- 建立的Model對象是否 不可改變  即生成的Model對象不會有 setter方法,隻有構造方法 -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!--mapper映射檔案生成所在的目錄 為每一個資料庫的表生成對應的SqlMap檔案 -->
        <!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">-->
        <sqlMapGenerator targetPackage="mappers" targetProject="market/src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- 用戶端代碼,生成易于使用的針對Model對象和XML配置檔案 的代碼
                type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper對象
                type="MIXEDMAPPER",生成基于注解的Java Model 和相應的Mapper對象
                type="XMLMAPPER",生成SQLMap XML檔案和獨立的Mapper接口
        -->

        <!-- targetPackage:mapper接口dao生成的位置 -->
        <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".\src\main\java">-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.jike.dao" targetProject="market/src/main/java">
            <!-- enableSubPackages:是否讓schema作為包的字尾 -->
            <property name="enableSubPackages" value="false" />
        </javaClientGenerator>


        <table tableName="mmall_shipping" domainObjectName="Shipping" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_cart" domainObjectName="Cart" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_cart_item" domainObjectName="CartItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_pay_info" domainObjectName="PayInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
        <table tableName="mmall_product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
            <!-- 此處因為在資料庫中 detail、sub_images兩個字段的類型是 txt類型,是以需要轉換成VARCHAR
            	 以對應實體類中的String類型
             -->
            <columnOverride column="detail" jdbcType="VARCHAR" />
            <columnOverride column="sub_images" jdbcType="VARCHAR" />
            <!--  -->
        </table>
        <table tableName="mmall_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>


        <!-- Harry mybatis插件的搭建 -->
    </context>
</generatorConfiguration>           

    注意:

targetProject="market/src/main/java"           

    這個路徑一定要以自己的項目名開頭,否則會找不到。

5、選中mybatisGenerator.xml檔案 右鍵  Run as --->Run Mybatis Generator,就可以了。

另推薦幾個可供大家參考的部落格:

    https://blog.csdn.net/dings503/article/details/53506030

這個兄弟,是在github上下載下傳的generator插件,通過依賴注入。不過我沒弄好,要有弄好的請跟我說說,他這個是不要在eclipse中下載下傳安裝generator插件了嗎?

https://blog.csdn.net/qq_33802316/article/details/78002990

這個跟我思路差不多,大家也可以看看

繼續閱讀