本文介绍一下用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
逆向工程生成结果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/for_my_life/article/details/51228098
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 92
-
2025-05-27 105
-
2025-05-29 98
-
2025-05-25 97
-
微软承认 Windows 11 下 Outlook 搜索邮件卡死问题(附临时解决方法)
2025-05-27 30
热门评论






