idea maven mybatis generator 自動(dòng)生成代碼的實(shí)例講解
安裝過程步驟可以看上面的博文,里面介紹得很詳細(xì)。
二、建數(shù)據(jù)表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
drop table if exists `t_user`; create table `t_user` ( `id` varchar( 100 ) not null , `username` varchar( 20 ) default null , `password` varchar( 20 ) default null , `headerpic` varchar( 60 ) default null , `email` varchar( 60 ) default null , `sex` varchar( 2 ) default null , `create_time` datetime default null , `update_time` timestamp not null default current_timestamp on update current_timestamp, `is_delete` int ( 1 ) default null , `address` varchar( 200 ) default null , `telephone` varchar( 15 ) default null , primary key (`id`) ) engine=innodb default charset=utf8; |
三、idea創(chuàng)建maven項(xiàng)目
1、點(diǎn)擊create new project-》maven-》create from archetype->maven-archetype-webapp,然點(diǎn)擊next,步驟如圖:
2、填寫groupid和artifactid:(這兩個(gè)參數(shù)值都是自己定義的),下面這段文字,是網(wǎng)上抄來(lái)的,以便大家更好地了解這兩個(gè)參數(shù)。
groupid和artifactid被統(tǒng)稱為“坐標(biāo)”是為了保證項(xiàng)目唯一性而提出的,如果你要把你項(xiàng)目弄到maven本地倉(cāng)庫(kù)去,你想要找到你的項(xiàng)目就必須根據(jù)這兩個(gè)id去查找。
一般分為多個(gè)段,這里我只說(shuō)兩段,第一段為域,第二段為公司名稱。域又分為org、com、cn等等許多,其中org為非營(yíng)利組織,com為商業(yè)組織。舉個(gè)apache公司的tomcat項(xiàng)目例子:這個(gè)項(xiàng)目的groupid是org.apache,它的域是org(因?yàn)閠omcat是非營(yíng)利項(xiàng)目),公司名稱是apache,artigactid是tomcat。
比如我創(chuàng)建一個(gè)項(xiàng)目,我一般會(huì)將groupid設(shè)置為cn.laok,cn表示域?yàn)橹袊?guó),laok是我個(gè)人姓名縮寫,artifactid設(shè)置為testproj,表示你這個(gè)項(xiàng)目的名稱是testproj,依照這個(gè)設(shè)置,你的包結(jié)構(gòu)最好是cn.laok.testproj打頭的,如果有個(gè)userdao,它的全路徑就是cn.laok.testproj.dao.userdao
3、點(diǎn)擊next,配置maven信息,如圖:
4、點(diǎn)擊next,填寫項(xiàng)目名稱,如圖:
5、創(chuàng)建完成后,項(xiàng)目的結(jié)構(gòu)如圖,在生成代碼之前,不需要?jiǎng)?chuàng)建其他文件夾,但是需要把resources文件夾設(shè)置成resources root(右鍵點(diǎn)擊resources文件夾-》mark directory as->resources root)
四、配置pom.xml和generatorconfig.xml
1、在pom.xml中加入以下配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<build> <finalname>create-code</finalname> <plugins> <plugin> <groupid>org.mybatis.generator</groupid> <artifactid>mybatis-generator-maven-plugin</artifactid> <version> 1.3 . 2 </version> <configuration> <verbose> true </verbose> <overwrite> true </overwrite> </configuration> </plugin> </plugins> </build> |
2、在resources源文件夾下面創(chuàng)建generatorconfig.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<?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> <classpathentry location= "d:/java/lib/mysql-connector-java-5.1.43-bin.jar" /> <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> <!-- 這個(gè)元素用來(lái)去除指定生成的注釋中是否包含生成的日期 false :表示保護(hù) --> <!-- 如果生成日期,會(huì)造成即使修改一個(gè)字段,整個(gè)實(shí)體類所有屬性都會(huì)發(fā)生變化,不利于版本控制,所以設(shè)置為 true --> <property name= "suppressdate" value= "true" /> <!-- 是否去除自動(dòng)生成的注釋 true :是 : false :否 --> <property name= "suppressallcomments" value= "false" /> </commentgenerator> <!--數(shù)據(jù)庫(kù)鏈接url,用戶名、密碼 --> <jdbcconnection driverclass= "com.mysql.jdbc.driver" connectionurl= "jdbc:mysql://localhost:3306/article" userid= "root" 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= "com.test.model" targetproject= "target" > <property name= "enablesubpackages" value= "true" /> <property name= "trimstrings" value= "true" /> </javamodelgenerator> <!-- 生成映射文件的包名和位置 文件夾自己定義--> <sqlmapgenerator targetpackage= "com.test.mapping" targetproject= "target" > <property name= "enablesubpackages" value= "true" /> </sqlmapgenerator> <!-- 生成dao的包名和位置 文件夾自己定義--> <javaclientgenerator type= "xmlmapper" targetpackage= "com.test.dao" implementationpackage= "com.test.dao.impl" targetproject= "target" > <property name= "enablesubpackages" value= "true" /> </javaclientgenerator> <!-- 要生成哪些表 --> <table tablename= "t_user" domainobjectname= "user" enablecountbyexample= "false" enableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" selectbyexamplequeryid= "false" ></table> </context> </generatorconfiguration> |
3、配置完成后,一定要點(diǎn)擊build->rebuild project,生成target文件夾,不然生產(chǎn)代碼的時(shí)候是生產(chǎn)在target文件下下面,沒有這個(gè)文件夾會(huì)報(bào)錯(cuò),當(dāng)然也可以配置生成在其他文件夾下面。項(xiàng)目結(jié)構(gòu)如圖:
特別注意的一點(diǎn):一定要在配置文件中加入本地的mysql-connector-java-5.1.43-bin.jar,
下載地址:https://dev.mysql.com/downloads/connector/j/
然后解壓到本地,我的配置如下:<classpathentry location="d:/java/lib/mysql-connector-java-5.1.43-bin.jar" />
這個(gè)需要大家根據(jù)自己存放的路徑配置。
五、執(zhí)行生成代碼
1、點(diǎn)擊run->edit configurations,如圖:
2、之后彈出運(yùn)行配置框,為當(dāng)前配置配置一個(gè)名稱,這里其名為"generator",然后在 “command line” 選項(xiàng)中輸入“mybatis-generator:generate -e”
這里加了“-e ”選項(xiàng)是為了讓該插件輸出詳細(xì)信息,這樣可以幫助我們定位問題。
3、配置完成后,點(diǎn)擊run-》run generator,不出意外的話,在控制臺(tái)中會(huì)出現(xiàn)build success的info信息。完整的效果如圖所示:
有寫得不對(duì)的地方,煩請(qǐng)各位大佬指正,非常感謝。
以上這篇idea maven mybatis generator 自動(dòng)生成代碼(實(shí)例講解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/jamespan23/p/8064176.html