情景介紹
編寫代碼的過程中,我們總希望能有一個插件能快速生成公用的相似的代碼。感覺mybatis-generator用起來不錯。下面就來總結一下它的使用方法。
使用步驟
一、新建generator.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
51
52
|
<?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> <!-- 引入配置文件 --> <!--todo 注意!!!自動生成代碼 要把這句放開,鏈接數據庫--> <properties resource= "jdbc.properties" /> <!-- 指定數據連接驅動jar地址 --> <classpathentry location= "f:\svn_info\cloudtree\trustzhyq\src\e3izm\src\main\webapp\web-inf\lib\mysql-connector-java-5.1.29.jar" /> <context id= "context" targetruntime= "mybatis3" > <commentgenerator> <!-- 是否去除自動生成的注釋 true :是 : false :否 --> <property name= "suppressallcomments" value= "true" /> <property name= "suppressdate" value= "true" /> </commentgenerator> <!-- 數據庫的相關配置 --> <jdbcconnection driverclass= "${driverclasss}" connectionurl= "${jdbcurl}" userid= "${username}" password= "${password}" /> <javatyperesolver> <property name= "forcebigdecimals" value= "false" /> </javatyperesolver> <!-- 實體類生成的位置 --> <javamodelgenerator targetpackage= "com.trust.e3izm.ressvc.entity" targetproject= "src/main/java" > <property name= "enablesubpackages" value= "false" /> <property name= "trimstrings" value= "true" /> </javamodelgenerator> <!-- *mapper.xml 文件的位置 ,targetpackage:包名,targetproject:項目下的路徑--> <sqlmapgenerator targetpackage= "ressvc" targetproject= "src/main/resources/mapper" > <property name= "enablesubpackages" value= "false" /> </sqlmapgenerator> <!-- mapper 接口文件的位置 --> <javaclientgenerator targetpackage= "com.trust.e3izm.ressvc.dao" targetproject= "src/main/java" type= "xmlmapper" > <property name= "enablesubpackages" value= "false" /> </javaclientgenerator> <!-- 配置表信息 --> <!--第三方服務類型--> <table schema= "e3iz" tablename= "thirdptysvc_type" domainobjectname= "thirdptysvc_type" enablecountbyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" enableupdatebyexample= "false" > </table> <!--xxtable--> <!--如果生成n個表,那就將上面的那段table代碼copy n份--> </context> </generatorconfiguration> |
二、在pom.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
|
<plugins> <plugin> <!--mybatis-generator插件,用于自動生成mapper和pojo--> <groupid>org.mybatis.generator</groupid> <artifactid>mybatis-generator-maven-plugin</artifactid> <version> 1.3 . 2 </version> <configuration> <!--配置文件的位置--> <configurationfile>src/main/resources/generatorconfig.xml</configurationfile> <verbose> true </verbose> <overwrite> true </overwrite> </configuration> <executions> <execution> <id>generate mybatis artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupid>org.mybatis.generator</groupid> <artifactid>mybatis-generator-core</artifactid> <version> 1.3 . 2 </version> </dependency> </dependencies> </plugin> </plugins> |
注意
1.這段代碼要放到
1
2
3
4
|
<build> <finalname>e3izm</finalname> <!-- 將上面這段代碼放到pom.xml文件的這個位置--> </build> |
2.maven2下載關于generator,maven依賴包下載不下來,需要更改為maven3才能下載下來
maven3更改.png
三、新建maven運行器
maven.png
1
2
|
<!-- 配置的運行命令--> mybatis-generator:generate -e |
好了,大功告成,運行maven運行器即可!
如果有什么問題,可以閱讀官方文檔。
mybatis生成器官方文檔
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://www.jianshu.com/p/d019c9880d25