2016.7.12 eclipse和IDEA中mybatis generator插件的安裝與使用


 Eclipse中的安裝

1.下載插件

2.將插件generator的features和plugins里的東西都拷貝到eclipse的文件夾features和plugins下。

3.重啟eclipse,驗證是否安裝成功。

 

Eclipse中的使用

1.新建一個generatorConfig文件

 

2.generatorConfig.xml文件的配置

  • jdbcConnection ---數據庫鏈接URL、用戶名、密碼
  • javaModelGenerator---生成模型的包名和位置,就是mybatis 里面用的一些entity 類的存放路徑配置
  • sqlMapGenerator ---生成的映射文件報名和位置,就是對應mybatis 的寫sql 語句的xml文件的存放路徑配置
  • javaClientGenerator---生成DAO的包名和位置,就是mybatis 里面dao 接口的存放路徑
  • table---這個配置項是配置在項目中操作的數據庫表

 

(1)pom.xml中添加依賴

 

 

(2)運行項目,執行run as -》 maven install 

執行成功后,倉庫里會下載下來這些依賴包。(如果有的話,就一直skip然后就build success了)

 

(3)找到postgresql的jar包位置

這個jar包的位置會在后面的配置文件generatorConfig.xml中用到。

E:\lyh\file\repository\org\postgresql\postgresql\9.4-1206-jdbc41\...
<classPathEntry location="E:\lyh\file\repository\org\postgresql\postgresql\9.4-1206-jdbc41\postgresql-9.4-1206-jdbc41.jar"/>

 

 

(4)查看表所在的數據庫信息

<jdbcConnection 
        driverClass="org.postgresql.Driver" 
        connectionURL="jdbc:postgresql: //10.15.10.14:5432/postgres
        userId=" postgres
        password=" admin" />

 

<table tableName=" globalpage" domainObjectName=" GlobalInfo"/>

(5)配置generatorConfig.xml文件

 

 

(6)配置完后運行mybatisConfig.xml

點擊mybatisConfig.xml,右鍵選擇generate mybatis ....

 

(7)查看結果

 

(8)查看詳細代碼

 

 

 

疑問:這里有些東西多出來的不知道干嘛~

后來百度知道,這是generator自動生成的example,如果不想要的話,可以在配置文件里配置一下,參看另一篇博文:

2016.7.14 去掉Mybatis Generator生成的一堆 example

 

 

 

還有一個:現在是默認不分頁的!還要繼續完成分頁功能。(可以看到mapper文件里沒有分頁語句)

 

IDEA中的安裝

pom.xml增加配置,見下面。

 

IDEA中的使用

先確保數據庫連接ok,並且表已經建好。

建表語句:

 

 

1.pom.xml中增加mybatis-generator的配置

 1 <build>  2 <finalName>mmall</finalName>  3 <plugins>  4 <plugin>  5 <groupId>org.mybatis.generator</groupId>  6 <artifactId>mybatis-generator-maven-plugin</artifactId>  7 <version>1.3.2</version>  8 <configuration>  9 <verbose>true</verbose> 10 <overwrite>true</overwrite> 11 </configuration> 12 </plugin> 13  ... 14 </build>

 

2.generatorConfig.xml

新建文件 : src/main/resources/generatorConfig.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE generatorConfiguration
 3         PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 4         "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
 5 
 6 <generatorConfiguration>
 7     <!--導入屬性配置-->
 8     <properties resource="datasource.properties"></properties>
 9 
10     <!--指定特定數據庫的jdbc驅動jar包的位置-->
11     <classPathEntry location="${db.driverLocation}"/>
12 
13     <context id="default" targetRuntime="MyBatis3">
14 
15         <!-- optional,旨在創建class時,對注釋進行控制 -->
16         <commentGenerator>
17             <property name="suppressDate" value="true"/>
18             <property name="suppressAllComments" value="true"/>
19         </commentGenerator>
20 
21         <!--jdbc的數據庫連接 -->
22         <jdbcConnection
23                 driverClass="${db.driverClassName}"
24                 connectionURL="${db.url}"
25                 userId="${db.username}"
26                 password="${db.password}">
27         </jdbcConnection>
28 
29 
30         <!-- 非必需,類型處理器,在數據庫類型和java類型之間的轉換控制-->
31         <javaTypeResolver>
32             <property name="forceBigDecimals" value="false"/>
33         </javaTypeResolver>
34 
35 
36         <!-- Model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢Example類
37             targetPackage     指定生成的model生成所在的包名
38             targetProject     指定在該項目下所在的路徑
39         -->
40         <!--<javaModelGenerator targetPackage="com.mmall.pojo" targetProject=".\src\main\java">-->
41         <javaModelGenerator targetPackage="com.mmall.pojo" targetProject="./src/main/java">
42             <!-- 是否允許子包,即targetPackage.schemaName.tableName -->
43             <property name="enableSubPackages" value="false"/>
44             <!-- 是否對model添加 構造函數 -->
45             <property name="constructorBased" value="true"/>
46             <!-- 是否對類CHAR類型的列的數據進行trim操作 -->
47             <property name="trimStrings" value="true"/>
48             <!-- 建立的Model對象是否 不可改變  即生成的Model對象不會有 setter方法,只有構造方法 -->
49             <property name="immutable" value="false"/>
50         </javaModelGenerator>
51 
52         <!--mapper映射文件生成所在的目錄 為每一個數據庫的表生成對應的SqlMap文件 -->
53         <!--<sqlMapGenerator targetPackage="mappers" targetProject=".\src\main\resources">-->
54         <sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
55             <property name="enableSubPackages" value="false"/>
56         </sqlMapGenerator>
57 
58         <!-- 客戶端代碼,生成易於使用的針對Model對象和XML配置文件 的代碼
59                 type="ANNOTATEDMAPPER",生成Java Model 和基於注解的Mapper對象
60                 type="MIXEDMAPPER",生成基於注解的Java Model 和相應的Mapper對象
61                 type="XMLMAPPER",生成SQLMap XML文件和獨立的Mapper接口
62         -->
63 
64         <!-- targetPackage:mapper接口dao生成的位置 -->
65         <!--<javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject=".\src\main\java">-->
66         <javaClientGenerator type="XMLMAPPER" targetPackage="com.mmall.dao" targetProject="./src/main/java">
67             <!-- enableSubPackages:是否讓schema作為包的后綴 -->
68             <property name="enableSubPackages" value="false" />
69         </javaClientGenerator>
70 
71 
72         <table tableName="mmall_shipping" domainObjectName="Shipping" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
73         <table tableName="mmall_cart" domainObjectName="Cart" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
74         <table tableName="mmall_cart_item" domainObjectName="CartItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
75         <table tableName="mmall_category" domainObjectName="Category" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
76         <table tableName="mmall_order" domainObjectName="Order" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
77         <table tableName="mmall_order_item" domainObjectName="OrderItem" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
78         <table tableName="mmall_pay_info" domainObjectName="PayInfo" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
79         <table tableName="mmall_product" domainObjectName="Product" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
80             <!-- 數據庫的表中這兩個字段用的text,mybatis不同版本生成的不一樣,所以改為VARCHAR -->
81             <columnOverride column="detail" jdbcType="VARCHAR" />
82             <columnOverride column="sub_images" jdbcType="VARCHAR" />
83         </table>
84         <table tableName="mmall_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
85 
86 
87         <!-- geelynote mybatis插件的搭建 -->
88     </context>
89 </generatorConfiguration>
90 
91 gerneratorConfig.xml
generatorConfig.xml

 

重點說明的幾個點(其他都采用默認配置):

 

 

 

 

 

3.datasource.properties

 generatorConfig.xml中用到了datasource.properties,在同目錄新增文件datasource.properties。

 

注意這里的driverLocation:

1 db.driverLocation=E:/lyh/file/repository/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar 2 db.driverClassName=com.mysql.jdbc.Driver 3 db.url=jdbc:mysql://localhost:3306/mmall_learning?characterEncoding=utf-8 4 db.username=root 5 db.password=****

 

在pom.xml中配置過mysql的驅動,所以只要去maven的本地倉庫里找到地址就行:

 

 

 

4.執行mybatis-generator:generate

 

 運行之后:

 

同樣,也是沒有分頁相關語句的。


免責聲明!

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



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