本文介紹一下用maven工具如何生成mybatis的代碼及映射的文件。
一、配置maven pom.xml 文件
在pom.xml增加以下插件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<build> <finalname>zsxt</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> |
配置好maven插件,下面需要配置插件需要配置文件
二、在maven項目下的src/main/resources 目錄下建立名為maven的項目配置文件存放路徑如下圖:generatorconfig.xml和generator.properties配置文件,
maven的項目配置文件存放路徑如下圖:
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
<?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" ></properties> <!--指定特定數據庫的jdbc驅動jar包的位置--> <classpathentry location= "${jdbc.driverlocation}" /> <context id= "default" targetruntime= "mybatis3" > <!-- optional,旨在創建 class 時,對注釋進行控制 --> <commentgenerator> <property name= "suppressdate" value= "true" /> <property name= "suppressallcomments" value= "true" /> </commentgenerator> <!--jdbc的數據庫連接 --> <jdbcconnection driverclass= "${jdbc.driverclass}" connectionurl= "${jdbc.connectionurl}" userid= "${jdbc.userid}" password= "${jdbc.password}" > </jdbcconnection> <!-- 非必需,類型處理器,在數據庫類型和java類型之間的轉換控制--> <javatyperesolver> <property name= "forcebigdecimals" value= "false" /> </javatyperesolver> <!-- model模型生成器,用來生成含有主鍵key的類,記錄類 以及查詢example類 targetpackage 指定生成的model生成所在的包名 targetproject 指定在該項目下所在的路徑 --> <javamodelgenerator targetpackage= "com.slx.zsxt.model" targetproject= "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= "com.slx.zsxt.mapper" targetproject= "src/main/java" > <property name= "enablesubpackages" value= "false" /> </sqlmapgenerator> <!-- 客戶端代碼,生成易于使用的針對model對象和xml配置文件 的代碼 type= "annotatedmapper" ,生成java model 和基于注解的mapper對象 type= "mixedmapper" ,生成基于注解的java model 和相應的mapper對象 type= "xmlmapper" ,生成sqlmap xml文件和獨立的mapper接口 --> <javaclientgenerator targetpackage= "com.slx.zsxt.dao" targetproject= "src/main/java" type= "xmlmapper" > <property name= "enablesubpackages" value= "true" /> </javaclientgenerator> <table tablename= "reguser" domainobjectname= "user" enablecountbyexample= "false" enableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" selectbyexamplequeryid= "false" > </table> <table tablename= "adminuser" domainobjectname= "admin" enablecountbyexample= "false" enableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" selectbyexamplequeryid= "false" > </table> <table tablename= "configinfo" domainobjectname= "confinfo" enablecountbyexample= "false" enableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" selectbyexamplequeryid= "false" > </table> <table tablename= "grade" domainobjectname= "grade" enablecountbyexample= "false" enableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" selectbyexamplequeryid= "false" > </table> <table tablename= "gradelog" domainobjectname= "gradelog" enablecountbyexample= "false" enableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" selectbyexamplequeryid= "false" > </table> <table tablename= "reginfo" domainobjectname= "reginfo" enablecountbyexample= "false" enableupdatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" selectbyexamplequeryid= "false" > </table> </context> </generatorconfiguration> |
generator.propertites代碼如下:
1
2
3
4
5
|
jdbc.driverlocation=e:\\mvn_home\\mysql\\mysql-connector-java\\ 5.1 . 20 \\mysql-connector-java- 5.1 . 20 .jar jdbc.driverclass=com.mysql.jdbc.driver jdbc.connectionurl=jdbc:mysql: ///zsxt jdbc.userid=root jdbc.password= 123456 |
三、在intellij idea添加一個“run運行”選項,使用maven運行mybatis-generator-maven-plugin插件
點擊 菜單run中edit configurations,會出現
點擊+號,選擇maven,會出現
在name和commond line分別填上如上圖所示,apply和ok
最后點擊generator,生成model,mapper,dao
逆向工程生成結果如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/for_my_life/article/details/51228098