Skip to content

Commit 8625e3e

Browse files
author
Songyu
committed
增加文章springmvc整合mybatis之mybatis-generator自动生成和相关图片;重新生成tag目录
1 parent 1d9a6c5 commit 8625e3e

File tree

15 files changed

+353
-30
lines changed

15 files changed

+353
-30
lines changed
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
---
2+
layout: post
3+
title: "springmvc整合mybatis之mybatis generator自动生成"
4+
date: 2020-11-08 08:41:26 +0800
5+
tags: java
6+
description:
7+
---
8+
9+
当我们使用hibernate作为数据持久层的时候,对于实体类的生成在Idea中可以使用__Persistence__菜单进行实体类的自动生成(Persistence->Generate Persistence Mapping->By Database Schema)
10+
11+
![](/images/2020-11-08-1.jpg)
12+
13+
当使用mybatis的时候,hibernate的Persistence菜单不能使用,但是mybatis提供了另一个非常好用的插件__mybatis-generator__,可以通过maven插件的方式,一键生成实体类、接口类、Xml映射等
14+
15+
## maven配置
16+
17+
jar包引入
18+
{% highlight xml %}
19+
<dependency>
20+
<groupId>org.mybatis.generator</groupId>
21+
<artifactId>mybatis-generator-core</artifactId>
22+
<version>1.4.0</version>
23+
</dependency>
24+
<!-- mybatis通用Mapper类 -->
25+
<dependency>
26+
<groupId>tk.mybatis</groupId>
27+
<artifactId>mapper</artifactId>
28+
<version>4.1.5</version>
29+
</dependency>
30+
<!-- mybatis类 -->
31+
<dependency>
32+
<groupId>org.mybatis</groupId>
33+
<artifactId>mybatis</artifactId>
34+
<version>3.5.3</version>
35+
</dependency>
36+
<!-- mybatis+spring整合包 -->
37+
<dependency>
38+
<groupId>org.mybatis</groupId>
39+
<artifactId>mybatis-spring</artifactId>
40+
<version>2.0.5</version>
41+
</dependency>
42+
{% endhighlight %}
43+
44+
配置maven插件
45+
{% highlight xml %}
46+
<plugin>
47+
<groupId>org.mybatis.generator</groupId>
48+
<artifactId>mybatis-generator-maven-plugin</artifactId>
49+
<version>1.3.6</version>
50+
<configuration>
51+
<configurationFile>
52+
${basedir}/src/main/resources/generator/generatorConfig.xml
53+
</configurationFile>
54+
<overwrite>true</overwrite>
55+
<verbose>true</verbose>
56+
</configuration>
57+
<dependencies>
58+
<dependency>
59+
<groupId>mysql</groupId>
60+
<artifactId>mysql-connector-java</artifactId>
61+
<version>${mysql.version}</version>
62+
<scope>runtime</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>tk.mybatis</groupId>
66+
<artifactId>mapper</artifactId>
67+
<version>4.1.5</version>
68+
</dependency>
69+
</dependencies>
70+
</plugin>
71+
{% endhighlight %}
72+
73+
## 配置generator文件
74+
75+
在配置文件中,我们可以配置数据库链接、需要生成的类以及生成目录等信息,直接上配置吧,对应的配置说明都在文件注释中
76+
{% highlight xml %}
77+
<?xml version="1.0" encoding="UTF-8"?>
78+
<!DOCTYPE generatorConfiguration
79+
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
80+
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
81+
<generatorConfiguration>
82+
<!-- 引入数据库配置文件 -->
83+
<properties resource="conf/jdbc.properties"></properties>
84+
<context id="mySqlContext" defaultModelType="flat">
85+
<!-- 自动处理mysql中的关键字 -->
86+
<property name="autoDelimitKeywords" value="true"/>
87+
<!-- sql关键字中的分隔符 -->
88+
<property name="beginningDelimiter" value="`"/>
89+
<property name="endingDelimiter" value="`"/>
90+
<!-- 编码 -->
91+
<property name="javaFileEncoding" value="UTF-8"/>
92+
<!-- 继承mybatis通用类 -->
93+
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
94+
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
95+
<property name="caseSensitive" value="true"/>
96+
<property name="forceAnnotation" value="true"/>
97+
<property name="beginningDelimiter" value="`"/>
98+
<property name="endingDelimiter" value="`"/>
99+
</plugin>
100+
<!-- 阻止自动生成时间戳和MBG注释,会影响版本控制 -->
101+
<commentGenerator>
102+
<property name="suppressDate" value="true"/>
103+
</commentGenerator>
104+
<!-- 数据库信息 -->
105+
<jdbcConnection
106+
driverClass="${jdbc.driver}"
107+
connectionURL="${jdbc.url}"
108+
userId="${jdbc.username}"
109+
password="${jdbc.password}"
110+
>
111+
<property name="nullCatalogMeansCurrent" value="true"/>
112+
</jdbcConnection>
113+
<!-- 生成实体类的位置 -->
114+
<javaModelGenerator targetPackage="com.bc.entity" targetProject="src/main/java">
115+
<!-- 如果设置为true会在targetPackage的基础上,根据数据库的schema再生成一层package -->
116+
<property name="enableSubPackages" value="false"/>
117+
<!-- 在getter方法中,对String类型字段调用trim()方法 -->
118+
<property name="trimStrings" value="true"/>
119+
</javaModelGenerator>
120+
<!-- 如果用XML格式,该配置需要配置 -->
121+
<sqlMapGenerator targetPackage="mapper" targetProject="src\main\resources">
122+
<property name="enableSubPackages" value="false"/>
123+
</sqlMapGenerator>
124+
<!--
125+
mapper的生成形式
126+
XMLMAPPER:XML形式,不需要mapper注解
127+
-->
128+
<javaClientGenerator type="XMLMAPPER" targetPackage="com.bc.dao" targetProject="src/main/java"></javaClientGenerator>
129+
<!-- 配置要生成的表 -->
130+
<table tableName="bc_one" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false">
131+
<!-- 设置mysql主键 -->
132+
<generatedKey column="id" sqlStatement="MySql"></generatedKey>
133+
<!-- 匹配生成的类型名称,替换掉数据中的前缀 -->
134+
<domainObjectRenamingRule searchString="^Bc" replaceString=""></domainObjectRenamingRule>
135+
</table>
136+
</context>
137+
</generatorConfiguration>
138+
{% endhighlight %}
139+
140+
## 使用方式
141+
142+
在项目根目录中执行:
143+
{% highlight cli %}
144+
mvn mybatis-generator:generate
145+
{% endhighlight %}
146+
147+
执行成功后,会显示如下界面
148+
149+
![](/images/2020-11-08-2.png)
150+
151+
如果执行失败的话,可以在上述命令执行时候在结尾增加__-X__参数。会显示详细的执行日志
152+
153+
完成,就会发生已经生成了实体、接口类和xml映射文件
154+
155+
![](/images/2020-11-08-3.jpg)
156+
157+
每个文件内容如下:
158+
159+
### OneMapper.java
160+
161+
{% highlight java %}
162+
package com.bc.dao;
163+
164+
import com.bc.entity.One;
165+
import tk.mybatis.mapper.common.Mapper;
166+
167+
public interface OneMapper extends Mapper<One> {
168+
169+
//继承了通用类,可以实现一些通用的函数,这里可以编写自定义查询函数...
170+
171+
}
172+
{% endhighlight %}
173+
174+
175+
### One.java
176+
177+
{% highlight java %}
178+
package com.bc.entity;
179+
180+
import javax.persistence.*;
181+
182+
@Table(name = "`bc_one`")
183+
public class One {
184+
@Id
185+
@Column(name = "`id`")
186+
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "SELECT LAST_INSERT_ID()")
187+
private Integer id;
188+
189+
@Column(name = "`name`")
190+
private String name;
191+
192+
@Column(name = "`age`")
193+
private Integer age;
194+
195+
/**
196+
* @return id
197+
*/
198+
public Integer getId() {
199+
return id;
200+
}
201+
202+
/**
203+
* @param id
204+
*/
205+
public void setId(Integer id) {
206+
this.id = id;
207+
}
208+
209+
/**
210+
* @return name
211+
*/
212+
public String getName() {
213+
return name;
214+
}
215+
216+
/**
217+
* @param name
218+
*/
219+
public void setName(String name) {
220+
this.name = name == null ? null : name.trim();
221+
}
222+
223+
/**
224+
* @return age
225+
*/
226+
public Integer getAge() {
227+
return age;
228+
}
229+
230+
/**
231+
* @param age
232+
*/
233+
public void setAge(Integer age) {
234+
this.age = age;
235+
}
236+
}
237+
{% endhighlight %}
238+
239+
### OneMapper.xml
240+
241+
{% highlight xml %}
242+
<?xml version="1.0" encoding="UTF-8"?>
243+
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
244+
<mapper namespace="com.bc.dao.OneMapper">
245+
<resultMap id="BaseResultMap" type="com.bc.entity.One">
246+
<!--
247+
WARNING - @mbg.generated
248+
-->
249+
<id column="id" jdbcType="INTEGER" property="id" />
250+
<result column="name" jdbcType="VARCHAR" property="name" />
251+
<result column="age" jdbcType="INTEGER" property="age" />
252+
</resultMap>
253+
<sql id="Base_Column_List">
254+
<!--
255+
WARNING - @mbg.generated
256+
-->
257+
id, `name`, age
258+
</sql>
259+
</mapper>
260+
{% endhighlight %}
261+
262+
至此mybatis-generator的配置和用法就记录完成了,感觉上确实方便了很多,对于Dao类生成来说,不需要重复的去建立文件了,直接通过命令行直接生成就好了
263+
![](/images/2020-11-08-4.jpg)
264+
265+
下一篇文章介绍mybatis与springmvc的整合和使用

images/2020-11-08-1.jpg

93.4 KB
Loading

images/2020-11-08-2.png

59.2 KB
Loading

images/2020-11-08-3.jpg

23.8 KB
Loading

images/2020-11-08-4.jpg

21.2 KB
Loading

tag/docker/index.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ <h2 class="post_title">标签分类 "docker&rdquo;</h2>
138138

139139

140140

141+
142+
143+
144+
141145

142146

143147

@@ -288,6 +292,8 @@ <h2 class="post-title">
288292
<h2>近期文章</h2>
289293
<ul>
290294

295+
<li><a href="/2020/11/springmvc%E6%95%B4%E5%90%88mybatis%E4%B9%8Bmybatis-generator%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90">springmvc整合mybatis之mybatis generator自动生成</a></li>
296+
291297
<li><a href="/2020/11/springmvc%E6%95%B4%E5%90%88thymeleaf">springMVC整合thymeleaf</a></li>
292298

293299
<li><a href="/2020/11/idea%E6%90%AD%E5%BB%BAspringmvc">idea搭建springMVC</a></li>
@@ -296,8 +302,6 @@ <h2>近期文章</h2>
296302

297303
<li><a href="/2020/10/springmvci%E9%9B%86%E6%88%90shiro-%E5%A4%9A%E8%BA%AB%E4%BB%BD">springMVCi集成shiro(多身份)</a></li>
298304

299-
<li><a href="/2020/05/git-%E7%94%9F%E6%88%90%E8%A1%A5%E4%B8%81%E5%92%8C%E6%89%93%E8%A1%A5%E4%B8%81">git 生成补丁和打补丁</a></li>
300-
301305
</ul>
302306
</div>
303307

@@ -315,7 +319,7 @@ <h2>文章分类</h2>
315319
<span>github</span></a></li>
316320

317321
<li>
318-
<a href="/tag/java" data-toggle="tooltip" data-placement="right" title="4">
322+
<a href="/tag/java" data-toggle="tooltip" data-placement="right" title="5">
319323
<span>java</span></a></li>
320324

321325
<li>

tag/github/index.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ <h2 class="post_title">标签分类 "github&rdquo;</h2>
6868

6969

7070

71+
72+
73+
74+
7175

7276

7377

@@ -342,6 +346,8 @@ <h2 class="post-title">
342346
<h2>近期文章</h2>
343347
<ul>
344348

349+
<li><a href="/2020/11/springmvc%E6%95%B4%E5%90%88mybatis%E4%B9%8Bmybatis-generator%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90">springmvc整合mybatis之mybatis generator自动生成</a></li>
350+
345351
<li><a href="/2020/11/springmvc%E6%95%B4%E5%90%88thymeleaf">springMVC整合thymeleaf</a></li>
346352

347353
<li><a href="/2020/11/idea%E6%90%AD%E5%BB%BAspringmvc">idea搭建springMVC</a></li>
@@ -350,8 +356,6 @@ <h2>近期文章</h2>
350356

351357
<li><a href="/2020/10/springmvci%E9%9B%86%E6%88%90shiro-%E5%A4%9A%E8%BA%AB%E4%BB%BD">springMVCi集成shiro(多身份)</a></li>
352358

353-
<li><a href="/2020/05/git-%E7%94%9F%E6%88%90%E8%A1%A5%E4%B8%81%E5%92%8C%E6%89%93%E8%A1%A5%E4%B8%81">git 生成补丁和打补丁</a></li>
354-
355359
</ul>
356360
</div>
357361

@@ -369,7 +373,7 @@ <h2>文章分类</h2>
369373
<span>github</span></a></li>
370374

371375
<li>
372-
<a href="/tag/java" data-toggle="tooltip" data-placement="right" title="4">
376+
<a href="/tag/java" data-toggle="tooltip" data-placement="right" title="5">
373377
<span>java</span></a></li>
374378

375379
<li>

tag/java/index.html

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ <h2 class="post_title">标签分类 "java&rdquo;</h2>
5555

5656

5757

58+
<h2 class="post-title">
59+
<a href="https://GuoSongyu.github.io/2020/11/springmvc%E6%95%B4%E5%90%88mybatis%E4%B9%8Bmybatis-generator%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90">
60+
springmvc整合mybatis之mybatis generator自动生成
61+
</a>
62+
</h2>
63+
64+
<div class="post-meta">
65+
<div class="post-time">
66+
<i class="fa fa-calendar"></i>
67+
<time datetime='2020-11-08'>08 Nov 2020</time>
68+
</div>
69+
</div>
70+
<div class="post-descr">
71+
<p>
72+
73+
</p>
74+
</div>
75+
76+
77+
78+
79+
5880
<h2 class="post-title">
5981
<a href="https://GuoSongyu.github.io/2020/11/springmvc%E6%95%B4%E5%90%88thymeleaf">
6082
springMVC整合thymeleaf
@@ -324,6 +346,8 @@ <h2 class="post-title">
324346
<h2>近期文章</h2>
325347
<ul>
326348

349+
<li><a href="/2020/11/springmvc%E6%95%B4%E5%90%88mybatis%E4%B9%8Bmybatis-generator%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90">springmvc整合mybatis之mybatis generator自动生成</a></li>
350+
327351
<li><a href="/2020/11/springmvc%E6%95%B4%E5%90%88thymeleaf">springMVC整合thymeleaf</a></li>
328352

329353
<li><a href="/2020/11/idea%E6%90%AD%E5%BB%BAspringmvc">idea搭建springMVC</a></li>
@@ -332,8 +356,6 @@ <h2>近期文章</h2>
332356

333357
<li><a href="/2020/10/springmvci%E9%9B%86%E6%88%90shiro-%E5%A4%9A%E8%BA%AB%E4%BB%BD">springMVCi集成shiro(多身份)</a></li>
334358

335-
<li><a href="/2020/05/git-%E7%94%9F%E6%88%90%E8%A1%A5%E4%B8%81%E5%92%8C%E6%89%93%E8%A1%A5%E4%B8%81">git 生成补丁和打补丁</a></li>
336-
337359
</ul>
338360
</div>
339361

@@ -351,7 +373,7 @@ <h2>文章分类</h2>
351373
<span>github</span></a></li>
352374

353375
<li>
354-
<a href="/tag/java" data-toggle="tooltip" data-placement="right" title="4">
376+
<a href="/tag/java" data-toggle="tooltip" data-placement="right" title="5">
355377
<span>java</span></a></li>
356378

357379
<li>

0 commit comments

Comments
 (0)