Skip to content

Commit 01790d3

Browse files
author
Songyu
committed
新增两篇文章,及图片资源
springmvc整合mybatis之mybatis整合 springMVC页面中文乱码及对象自动转json格式
1 parent 8625e3e commit 01790d3

17 files changed

+348
-50
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
---
2+
layout: post
3+
title: "springmvc整合mybatis之mybatis整合"
4+
date: 2020-11-12 20:49:53 +0800
5+
tags: java
6+
description:
7+
---
8+
9+
上一篇文章中,已经说明了需要引入的jar包
10+
11+
{% highlight xml %}
12+
<!-- mybatis通用Mapper类 -->
13+
<dependency>
14+
<groupId>tk.mybatis</groupId>
15+
<artifactId>mapper</artifactId>
16+
<version>4.1.5</version>
17+
</dependency>
18+
<!-- mybatis类 -->
19+
<dependency>
20+
<groupId>org.mybatis</groupId>
21+
<artifactId>mybatis</artifactId>
22+
<version>3.5.3</version>
23+
</dependency>
24+
<!-- mybatis+spring整合包 -->
25+
<dependency>
26+
<groupId>org.mybatis</groupId>
27+
<artifactId>mybatis-spring</artifactId>
28+
<version>2.0.5</version>
29+
</dependency>
30+
{% endhighlight %}
31+
32+
主要是下面两个,第一个通用mapper类只是为了更方便的去使用,里面已经集成好了一些常用的函数。我们不需要重复封装,直接调用就可以了
33+
34+
接下来需要对mapper的一些参数进行配置。我们在resources目录下,创建springmapper.xml文件,配置如下:
35+
36+
{% highlight xml %}
37+
<?xml version="1.0" encoding="UTF-8"?>
38+
<beans xmlns="http://www.springframework.org/schema/beans"
39+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
40+
xmlns:context="http://www.springframework.org/schema/context"
41+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
42+
<!-- 加载properties文件中的内容 -->
43+
<context:property-placeholder location="classpath:conf/jdbc.properties"/>
44+
<!-- 扫描的包路径 -->
45+
<context:component-scan base-package="com.bc.*" />
46+
<!-- 配置连接池 -->
47+
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
48+
<property name="user" value="${jdbc.username}"></property>
49+
<property name="password" value="${jdbc.password}"></property>
50+
<property name="driverClass" value="${jdbc.driver}"></property>
51+
<property name="jdbcUrl" value="${jdbc.url}"></property>
52+
<property name="initialPoolSize" value="${jdbc.initialSize}"></property>
53+
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
54+
<property name="minPoolSize" value="${jdbc.minPoolSize}"></property>
55+
</bean>
56+
<!-- mybatis整合 -->
57+
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
58+
<property name="dataSource" ref="dataSource"></property>
59+
<!-- 映射路径 -->
60+
<property name="mapperLocations" value="classpath:mapper/*"></property>
61+
</bean>
62+
<!-- 配置扫描器,这里使用了通用mapper中的类 -->
63+
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
64+
<!-- 扫描包路径,如果需要扫描多个包中间用半角逗号隔开 -->
65+
<property name="basePackage" value="com.bc.dao"/>
66+
<!-- 这边不能使用ref="sqlSessionFactory"原因是因为上面加载配置文件导致这边引用会报错 -->
67+
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
68+
</bean>
69+
<!-- 事务管理器 对mybatis操作数据库事务控制,spring使用jdbc的事务控制类 -->
70+
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
71+
<!-- 数据源 -->
72+
<property name="dataSource" ref="dataSource" />
73+
</bean>
74+
</beans>
75+
{% endhighlight %}
76+
77+
配置完成后,在web.xml引入刚才的配置文件
78+
79+
{% highlight xml %}
80+
<context-param>
81+
<param-name>contextConfigLocation</param-name>
82+
<param-value>
83+
classpath:applicationContext.xml,
84+
<!-- 引入mybatis配置 -->
85+
classpath:springmapper.xml
86+
</param-value>
87+
</context-param>
88+
{% endhighlight %}
89+
90+
完成后,配合之前的mybatis-generator,进行测试,先随便创建一个表
91+
![](/images/2020-11-12-1.png)
92+
93+
进行自动生成,生成如下几个文件,分别是Mapper查询接口(继承tk通用mapper)、实体类、xml映射文件
94+
![](/images/2020-11-12-2.png)
95+
96+
创建控制器,并调用Mapper接口类,进行查询(省略了Serivce类)
97+
98+
{% highlight java %}
99+
@RestController
100+
@RequestMapping(value = "data")
101+
public class DataController {
102+
103+
@Autowired
104+
private OneMapper oneMapper;
105+
106+
@GetMapping(value = "index")
107+
public One index(){
108+
One one = oneMapper.selectByPrimaryKey(1);
109+
110+
return one;
111+
}
112+
113+
}
114+
{% endhighlight %}
115+
116+
我们运用的selectByPrimaryKey函数就是通用类其中的一个,通用类中包含了很多常用的函数,大大加快的开发进度,节约了很多时间
117+
118+
![](/images/2020-11-12-3.png)
119+
120+
完成后,访问链接,读取数据库信息成功,mybatis配置成功!
121+
122+
![](/images/2020-11-12-4.png)
123+
124+
springMVC整合mybatis,至此就结束了,后续还有继续记录mybatis的一些用法(用到哪里记录哪里)
125+
126+
![](/images/2020-11-12-5.jpg)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
layout: post
3+
title: "springMVC页面中文乱码及对象自动转json格式"
4+
date: 2020-11-12 21:34:39 +0800
5+
tags: java
6+
description:
7+
---
8+
9+
对于页面中文乱码的问题,需要在springmvc.xml增加如下配置
10+
11+
{% highlight xml %}
12+
<mvc:annotation-driven>
13+
<mvc:message-converters>
14+
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
15+
<property name="supportedMediaTypes">
16+
<list>
17+
<value>application/json;charset=UTF-8</value>
18+
<value>text/html;charset=UTF-8</value>
19+
</list>
20+
</property>
21+
</bean>
22+
</mvc:message-converters>
23+
</mvc:annotation-driven>
24+
{% endhighlight %}
25+
26+
控制器直接返回对象时,用到的情况其实很多,比如接口返回值之类的。开始的时候,虽然我在控制器中增加了@RestController但是返回的时候,仍然会报错。还需要要引入第三方json工具类,并在配置文件中进行配置。
27+
28+
对于json工具类,我选择了谷歌的gson,springmvc原本就支持gson类,所以只需要增加一句配置就可以了
29+
30+
{% highlight xml %}
31+
<dependency>
32+
<groupId>com.google.code.gson</groupId>
33+
<artifactId>gson</artifactId>
34+
<version>2.8.5</version>
35+
</dependency>
36+
{% endhighlight %}
37+
38+
然后在springmvc.xml继续增加
39+
40+
{% highlight xml %}
41+
<mvc:annotation-driven>
42+
<mvc:message-converters>
43+
<!-- 防止中文乱码 -->
44+
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
45+
<property name="supportedMediaTypes">
46+
<list>
47+
<value>application/json;charset=UTF-8</value>
48+
<value>text/html;charset=UTF-8</value>
49+
</list>
50+
</property>
51+
</bean>
52+
<!-- 对象自动转json -->
53+
<bean class="org.springframework.http.converter.json.GsonHttpMessageConverter"/>
54+
</mvc:message-converters>
55+
</mvc:annotation-driven>
56+
{% endhighlight %}

images/2020-11-12-1.png

17.2 KB
Loading

images/2020-11-12-2.png

51.6 KB
Loading

images/2020-11-12-3.png

48.4 KB
Loading

images/2020-11-12-4.png

2.02 KB
Loading

images/2020-11-12-5.jpg

9.9 KB
Loading

tag/docker/index.html

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

139139

140140

141+
142+
143+
144+
145+
146+
147+
148+
141149

142150

143151

@@ -292,16 +300,16 @@ <h2 class="post-title">
292300
<h2>近期文章</h2>
293301
<ul>
294302

303+
<li><a href="/2020/11/springmvc%E9%A1%B5%E9%9D%A2%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81%E5%8F%8A%E5%AF%B9%E8%B1%A1%E8%87%AA%E5%8A%A8%E8%BD%ACjson%E6%A0%BC%E5%BC%8F">springMVC页面中文乱码及对象自动转json格式</a></li>
304+
305+
<li><a href="/2020/11/springmvc%E6%95%B4%E5%90%88mybatis%E4%B9%8Bmybatis%E6%95%B4%E5%90%88">springmvc整合mybatis之mybatis整合</a></li>
306+
295307
<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>
296308

297309
<li><a href="/2020/11/springmvc%E6%95%B4%E5%90%88thymeleaf">springMVC整合thymeleaf</a></li>
298310

299311
<li><a href="/2020/11/idea%E6%90%AD%E5%BB%BAspringmvc">idea搭建springMVC</a></li>
300312

301-
<li><a href="/2020/10/shiro%E6%B5%8F%E8%A7%88%E5%99%A8%E5%90%8C%E6%97%B6%E7%99%BB%E5%BD%95%E4%B8%A4%E4%B8%AA%E4%B8%8D%E5%90%8C%E7%9A%84%E7%94%A8%E6%88%B7">shiro浏览器同时登录两个不同的用户</a></li>
302-
303-
<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>
304-
305313
</ul>
306314
</div>
307315

@@ -319,7 +327,7 @@ <h2>文章分类</h2>
319327
<span>github</span></a></li>
320328

321329
<li>
322-
<a href="/tag/java" data-toggle="tooltip" data-placement="right" title="5">
330+
<a href="/tag/java" data-toggle="tooltip" data-placement="right" title="7">
323331
<span>java</span></a></li>
324332

325333
<li>

tag/github/index.html

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ <h2 class="post_title">标签分类 "github&rdquo;</h2>
6666

6767

6868

69+
70+
71+
72+
73+
74+
75+
76+
6977

7078

7179

@@ -346,16 +354,16 @@ <h2 class="post-title">
346354
<h2>近期文章</h2>
347355
<ul>
348356

357+
<li><a href="/2020/11/springmvc%E9%A1%B5%E9%9D%A2%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81%E5%8F%8A%E5%AF%B9%E8%B1%A1%E8%87%AA%E5%8A%A8%E8%BD%ACjson%E6%A0%BC%E5%BC%8F">springMVC页面中文乱码及对象自动转json格式</a></li>
358+
359+
<li><a href="/2020/11/springmvc%E6%95%B4%E5%90%88mybatis%E4%B9%8Bmybatis%E6%95%B4%E5%90%88">springmvc整合mybatis之mybatis整合</a></li>
360+
349361
<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>
350362

351363
<li><a href="/2020/11/springmvc%E6%95%B4%E5%90%88thymeleaf">springMVC整合thymeleaf</a></li>
352364

353365
<li><a href="/2020/11/idea%E6%90%AD%E5%BB%BAspringmvc">idea搭建springMVC</a></li>
354366

355-
<li><a href="/2020/10/shiro%E6%B5%8F%E8%A7%88%E5%99%A8%E5%90%8C%E6%97%B6%E7%99%BB%E5%BD%95%E4%B8%A4%E4%B8%AA%E4%B8%8D%E5%90%8C%E7%9A%84%E7%94%A8%E6%88%B7">shiro浏览器同时登录两个不同的用户</a></li>
356-
357-
<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>
358-
359367
</ul>
360368
</div>
361369

@@ -373,7 +381,7 @@ <h2>文章分类</h2>
373381
<span>github</span></a></li>
374382

375383
<li>
376-
<a href="/tag/java" data-toggle="tooltip" data-placement="right" title="5">
384+
<a href="/tag/java" data-toggle="tooltip" data-placement="right" title="7">
377385
<span>java</span></a></li>
378386

379387
<li>

tag/java/index.html

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,50 @@ <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%E9%A1%B5%E9%9D%A2%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81%E5%8F%8A%E5%AF%B9%E8%B1%A1%E8%87%AA%E5%8A%A8%E8%BD%ACjson%E6%A0%BC%E5%BC%8F">
60+
springMVC页面中文乱码及对象自动转json格式
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-12'>12 Nov 2020</time>
68+
</div>
69+
</div>
70+
<div class="post-descr">
71+
<p>
72+
73+
</p>
74+
</div>
75+
76+
77+
78+
79+
80+
<h2 class="post-title">
81+
<a href="https://GuoSongyu.github.io/2020/11/springmvc%E6%95%B4%E5%90%88mybatis%E4%B9%8Bmybatis%E6%95%B4%E5%90%88">
82+
springmvc整合mybatis之mybatis整合
83+
</a>
84+
</h2>
85+
86+
<div class="post-meta">
87+
<div class="post-time">
88+
<i class="fa fa-calendar"></i>
89+
<time datetime='2020-11-12'>12 Nov 2020</time>
90+
</div>
91+
</div>
92+
<div class="post-descr">
93+
<p>
94+
95+
</p>
96+
</div>
97+
98+
99+
100+
101+
58102
<h2 class="post-title">
59103
<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">
60104
springmvc整合mybatis之mybatis generator自动生成
@@ -346,16 +390,16 @@ <h2 class="post-title">
346390
<h2>近期文章</h2>
347391
<ul>
348392

393+
<li><a href="/2020/11/springmvc%E9%A1%B5%E9%9D%A2%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81%E5%8F%8A%E5%AF%B9%E8%B1%A1%E8%87%AA%E5%8A%A8%E8%BD%ACjson%E6%A0%BC%E5%BC%8F">springMVC页面中文乱码及对象自动转json格式</a></li>
394+
395+
<li><a href="/2020/11/springmvc%E6%95%B4%E5%90%88mybatis%E4%B9%8Bmybatis%E6%95%B4%E5%90%88">springmvc整合mybatis之mybatis整合</a></li>
396+
349397
<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>
350398

351399
<li><a href="/2020/11/springmvc%E6%95%B4%E5%90%88thymeleaf">springMVC整合thymeleaf</a></li>
352400

353401
<li><a href="/2020/11/idea%E6%90%AD%E5%BB%BAspringmvc">idea搭建springMVC</a></li>
354402

355-
<li><a href="/2020/10/shiro%E6%B5%8F%E8%A7%88%E5%99%A8%E5%90%8C%E6%97%B6%E7%99%BB%E5%BD%95%E4%B8%A4%E4%B8%AA%E4%B8%8D%E5%90%8C%E7%9A%84%E7%94%A8%E6%88%B7">shiro浏览器同时登录两个不同的用户</a></li>
356-
357-
<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>
358-
359403
</ul>
360404
</div>
361405

@@ -373,7 +417,7 @@ <h2>文章分类</h2>
373417
<span>github</span></a></li>
374418

375419
<li>
376-
<a href="/tag/java" data-toggle="tooltip" data-placement="right" title="5">
420+
<a href="/tag/java" data-toggle="tooltip" data-placement="right" title="7">
377421
<span>java</span></a></li>
378422

379423
<li>

0 commit comments

Comments
 (0)