激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring mvc整合mybatis(crud+分頁插件)操作mysql

Spring mvc整合mybatis(crud+分頁插件)操作mysql

2020-09-18 14:52Qbian Java教程

這篇文章主要介紹了Spring mvc整合mybatis(crud+分頁插件)操作mysql的步驟詳解,需要的朋友可以參考下

一、web.xml配置

我們都知道java ee的項目啟動的第一件事就是讀取web.xml,spring mvc 的web.xml我在上一篇文章中也做了詳細講解,不懂的可以回頭看看,講解的這個項目源碼我也會放到github上,也可以去那里看看,這里就不做介紹了。

web.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
<context-param>
 <param-name>contextconfiglocation</param-name>
 <param-value>classpath:/context.xml</param-value>
</context-param>
<!-- 監聽器:啟動服務器時,啟動 spring -->
<listener>
 <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
</listener>
<!-- spring 核心控制器 -->
<servlet>
 <servlet-name>dispatcherservlet</servlet-name>
 <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
 <load-on-startup>1</load-on-startup>
<init-param>
 <param-name>contextconfiglocation</param-name>
 <param-value>classpath:external-servlet.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
 <servlet-name>dispatcherservlet</servlet-name>
 <url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 編碼過濾器 -->
<filter>
 <filter-name>encodingfilter</filter-name>
 <filter-class>org.springframework.web.filter.characterencodingfilter</filter-class>
<init-param>
 <param-name>encoding</param-name>
 <param-value>utf-8</param-value>
</init-param>
<init-param>
 <param-name>forceencoding</param-name>
 <param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
 <filter-name>encodingfilter</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>

二、spring(context.xml) 上下文配置

這個配置文件可以說是服務器容器第二個要讀取的了,這里配置了spring啟動時掃描的基礎包路徑、外部配置的屬性文件的導入、需要連接的數據庫的配置、mybatis 和 spring 的整合、開頭我們說到的 mybatis 日期插件和分頁插件也是在這里配置、還有就是mybatis掃描的實體包及其 mapper 文件位置了。

context.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
<!-- spring 掃描的基礎包路徑 -->
<context:component-scan base-package="com.qbian" />
<!-- jdbc properties -->
<bean id="propertyconfigurer"
 class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"
 p:location="classpath:jdbc.properties" />
<!-- define the datasource (這里用的是c3p0的數據看連接池,性能不是很好,可以喚其它更好的連接池[jdbc pool等])-->
<bean id="datasource" class="com.mchange.v2.c3p0.combopooleddatasource"
 destroy-method="close">
 <property name="driverclass" value="${jdbc.driverclassname}" />
 <property name="jdbcurl" value="${jdbc.url}" />
 <property name="user" value="${jdbc.username}" />
 <property name="password" value="${jdbc.password}" />
</bean>
<!-- define the sqlsessionfactory -->
<bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean">
 <property name="datasource" ref="datasource" />
 <property name="typealiasespackage" value="com.qbian.**.dto" />
 <property name="plugins">
 <list>
  <!-- 配置自己實現的日期插件 -->
  <bean class="com.qbian.common.plugin.dateplugin" />
  <!-- 分頁插件 -->
  <bean class="com.qbian.common.plugin.pageplugin" />
 </list>
 </property>
</bean>
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.mapperscannerconfigurer">
 <property name="basepackage" value="com.qbian.**.dao" />
 <property name="sqlsessionfactorybeanname" value="sqlsessionfactory" />
</bean>
<!-- 將多個配置文件讀取到容器中,交給spring管理 -->
<bean id="configproperties" class="com.qbian.common.plugin.propertiesconfigurer">
 <property name="locations">
 <list>
  <!--<value>classpath:redis.properties</value>-->
 </list>
 </property>
</bean>

三、spring 控制器配置

這里配置的是控制器所在的位置,及其支持的請求類型和編碼。

external-servlet.xml 配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- 控制器掃描 -->
<context:component-scan base-package="com.qbian.common.controller" />
<mvc:annotation-driven>
 <mvc:message-converters>
 <bean class="org.springframework.http.converter.stringhttpmessageconverter">
  <property name="supportedmediatypes">
  <list>
   <value>text/html;charset=utf-8</value>
  </list>
  </property>
  <property name="writeacceptcharset" value="false" />
 </bean>
 </mvc:message-converters>
</mvc:annotation-driven>

配置信息就是以上三個了,接下來我們來看看具體的代碼,

四、代碼講解

1、java代碼講解,以下不做排序,只是按編輯器顯示順序排列講解。(以下內容均在java.com.qbian包下)

?
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
common |
 annotation |
 @interface now : 插入|更新數據的日期注解。
 @interface uuid :插入數據的uuid注解。
 controller |
 externalcontroller.class :核心控制器,攔截所有請求,異常處理,跨域設置等功能。
 dao |
 interface studentdao :使用例子,crud 共通方法。
 dto |
 pageinfodto.class :分頁使用的基礎dto對象。
 responsedto.class :響應數據的基本模型。
 entity |
 student.class :使用例子,自定義注解的使用。
 enums |
 enum messageenum :統一的返回狀態碼及描述信息。
 exception |
 externalserviceexception.class :自定義異常,業務相關都拋出該異常對象。
 factory |
 beanfactoryutil.class :根據bean name獲取spring管理的bean實例。
 hadle |
 exceptionhandle.class :spring自帶的統一異常捕獲處理。
 plugin |
 dateplugin.class :自定義mybatis日期插件。
 pageplugin.class :自定義mybatis分頁插件。
 propertiesconfigurer.class :將外部配置的屬性文件讀取到 spring 容器中統一管理。
 service |
 interface ibaseservie :基礎的service接口。
 baseservice.class :基礎的service抽象類。
 tokenservice.class :鑒權token服務類。
 util |
 checkutil.class :請求信息校驗相關工具類。
 dateutil.class :日期相關工具類。
 responseutil.class :響應信息工具類。
 secondsformatserializer.class :java.util.date類型轉時間戳工具類。
 timestampsecondsformatserializer.class :java.sql.timestamp類型轉時間戳工具類。
 stringutil.class :字符串相關工具類。
other |
 dao |
 interface studentextdao :使用例子,業務相關crud操作。
 dto |
 querystudentsexpagedto.class :根據學生性別分頁查詢返回對象dto。
 studentpagedto.class :根據學生性別分頁查詢封裝的對象。
 service |
 addstudentservice.class :插入學生數據接口。
 deletestudentservice.class :刪除學生數據接口。
 findstudentservice.class :查詢學生數據接口。
 updatestudentservice.class :更新學生數據接口。
 querystudentbysexservice.class :根據學生性別分頁查詢接口。

2、mybatis的 mapper.xml講解(以下內容均在resources/com/qbian文件夾下)

?
1
2
3
4
5
6
common |
 dao |
 studentdao.xml :對應common.dao.studentdao接口。
other |
 dao |
 studentextdao.xml :對應other.dao.studentextdao接口。

五、功能演示

1、token校驗

這里的token我寫死在代碼里了,123456表示校驗成功。我們先用插入數據接口測試一下,傳個錯誤的token,如下圖:

Spring mvc整合mybatis(crud+分頁插件)操作mysql

授權token校驗

2、請求參數校驗

我們來看看插入數據接口還需要校驗哪些值。

?
1
2
3
4
5
6
7
// 校驗請求參數
checkutil.checkempty(params, "token", "sex", "age");
// 校驗 token
tokenservice.checkuserlogin(params.getstring("token"));
student student = jsonobject.parseobject(params.tojsonstring(), student.class);
studentdao.insert(student);
return responseutil.success();

然后我們少傳age字段試一下:

Spring mvc整合mybatis(crud+分頁插件)操作mysql

請求字段校驗

3、插入數據

在插入數據之前我們先看看數據庫里都有哪些數據:

Spring mvc整合mybatis(crud+分頁插件)操作mysql

初始化數據庫中的值

從上圖可以看出,數據庫中沒有任何數據。我們來執行下插入接口。

Spring mvc整合mybatis(crud+分頁插件)操作mysql

測試插入接口

我們再來看下數據庫:

Spring mvc整合mybatis(crud+分頁插件)操作mysql

調用插入接口后

數據庫已經有數據了。

4、查詢數據

根據上一條數據的id查詢

Spring mvc整合mybatis(crud+分頁插件)操作mysql

調用查詢接口

剛插入的數據我們也查詢出來了。

5、更新數據

更新一下查詢出來的數據:

Spring mvc整合mybatis(crud+分頁插件)操作mysql

調用更新接口

然后我們再查詢一次該條數據

Spring mvc整合mybatis(crud+分頁插件)操作mysql

更新后再次查詢

可以看到性別和年齡都更新了,并且更新日期也是最新的了。

6、分頁查詢

先來看一下代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
// 校驗請求參數
checkutil.checkempty(params, "token", "sex", "pageno", "pagesize");
// 校驗 token
 tokenservice.checkuserlogin(params.getstring("token"));
// 根據性別分頁查詢 student,查詢總數會自動封裝到pagedto對象上
querystudentsexpagedto pagedto = jsonobject.parseobject(params.tojsonstring(), querystudentsexpagedto.class);
list<student> students = studentextdao.querybysexwithpage(pagedto);
studentpagedto studentpagedto = new studentpagedto();
// 查詢總數會自動封裝到pagedto對象上
studentpagedto.settotalsize(pagedto.gettotalsize());
studentpagedto.setstudents(students);
 return responseutil.success(studentpagedto);

分頁查詢之前我們想要導入多一點測試數據。

Spring mvc整合mybatis(crud+分頁插件)操作mysql

分頁前測試數據

可以看到數據庫目前有十條測試數據,男生有六條,年齡分別為19~24。好了,我們開始調用分頁查詢接口:

Spring mvc整合mybatis(crud+分頁插件)操作mysql

調用分頁查詢接口返回結果

格式化一下返回數據:

Spring mvc整合mybatis(crud+分頁插件)操作mysql

分頁查詢返回結果整理

這和我們直接查詢數據庫看到的一樣。

7、刪除數據

最后就是刪除數據接口了,我們將第一條測試數據刪除掉。

Spring mvc整合mybatis(crud+分頁插件)操作mysql

調用刪除接口返回結果

然后我們在查詢一下是否真的刪除了。

Spring mvc整合mybatis(crud+分頁插件)操作mysql

刪除后查詢

數據已經被刪除了。

最后附上項目源碼:https://github.com/qbian61/spring-mvc-mybatis

以上所述是小編給大家介紹的spring mvc整合mybatis(crud+分頁插件)操作mysql,,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:http://www.jianshu.com/p/f32c58b683fe

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲天堂ww | 久久久经典视频 | 欧美精品成人一区二区三区四区 | 国产va在线观看 | gogo全球大胆高清人露出91 | 污片视频网站 | 精品国产精品久久 | av亚洲在线观看 | 免费a级观看 | 亚洲综合一区二区三区 | 亚洲国产成人一区二区 | 精品国产一区三区 | 欧美伦理一区二区 | 国产精品久久久久久久久久久天堂 | 欧美日韩国产成人在线观看 | 一本色道久久综合狠狠躁篇适合什么人看 | 欧美淫视频| 噜噜噜躁狠狠躁狠狠精品视频 | 成年人免费黄色片 | 九九热精品在线视频 | 国产亚洲精品久久久久久久软件 | 久夜tv| 国产一区二区三区在线免费 | h视频免费在线观看 | 99欧美精品 | 欧美在线日韩 | 国产亚洲小视频 | 欧美精品久久久久久久久久 | av电影在线观看网址 | 91一区二区三区久久久久国产乱 | 亚洲成在人 | jizzjizz中国人少妇中文 | mmmwww| 亚洲婷婷日日综合婷婷噜噜噜 | 一级黄色影院 | 日日摸夜夜添夜夜添牛牛 | 一区二区三区手机在线观看 | 成人免费观看在线视频 | 福利一区二区三区视频在线观看 | 久久久久久久久成人 | 日本精品免费观看 |