Spring mvc整合mybatis(crud+分頁(yè)插件)操作mysql
一、web.xml配置
我們都知道java ee的項(xiàng)目啟動(dòng)的第一件事就是讀取web.xml,spring mvc 的web.xml我在上一篇文章中也做了詳細(xì)講解,不懂的可以回頭看看,講解的這個(gè)項(xiàng)目源碼我也會(huì)放到github上,也可以去那里看看,這里就不做介紹了。
web.xml 配置
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:/context.xml</param-value> </context-param> <!-- 監(jiān)聽(tīng)器:?jiǎn)?dòng)服務(wù)器時(shí),啟動(dòng) 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> <!-- 編碼過(guò)濾器 --> <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) 上下文配置
這個(gè)配置文件可以說(shuō)是服務(wù)器容器第二個(gè)要讀取的了,這里配置了spring啟動(dòng)時(shí)掃描的基礎(chǔ)包路徑、外部配置的屬性文件的導(dǎo)入、需要連接的數(shù)據(jù)庫(kù)的配置、mybatis 和 spring 的整合、開(kāi)頭我們說(shuō)到的 mybatis 日期插件和分頁(yè)插件也是在這里配置、還有就是mybatis掃描的實(shí)體包及其 mapper 文件位置了。
context.xml 配置
<!-- spring 掃描的基礎(chǔ)包路徑 --> <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的數(shù)據(jù)看連接池,性能不是很好,可以喚其它更好的連接池[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> <!-- 配置自己實(shí)現(xiàn)的日期插件 --> <bean class="com.qbian.common.plugin.DatePlugin" /> <!-- 分頁(yè)插件 --> <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> <!-- 將多個(gè)配置文件讀取到容器中,交給Spring管理 --> <bean id="configProperties" class="com.qbian.common.plugin.PropertiesConfigurer"> <property name="locations"> <list> <!--<value>classpath:redis.properties</value>--> </list> </property> </bean>
三、spring 控制器配置
這里配置的是控制器所在的位置,及其支持的請(qǐng)求類型和編碼。
external-servlet.xml 配置
<!-- 控制器掃描 --> <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>
配置信息就是以上三個(gè)了,接下來(lái)我們來(lái)看看具體的代碼,
四、代碼講解
1、java代碼講解,以下不做排序,只是按編輯器顯示順序排列講解。(以下內(nèi)容均在java.com.qbian包下)
common | annotation | @interface Now : 插入|更新數(shù)據(jù)的日期注解。 @interface UUID :插入數(shù)據(jù)的uuid注解。 controller | ExternalController.class :核心控制器,攔截所有請(qǐng)求,異常處理,跨域設(shè)置等功能。 dao | interface StudentDao :使用例子,crud 共通方法。 dto | PageInfoDto.class :分頁(yè)使用的基礎(chǔ)dto對(duì)象。 ResponseDto.class :響應(yīng)數(shù)據(jù)的基本模型。 entity | Student.class :使用例子,自定義注解的使用。 enums | enum MessageEnum :統(tǒng)一的返回狀態(tài)碼及描述信息。 exception | ExternalServiceException.class :自定義異常,業(yè)務(wù)相關(guān)都拋出該異常對(duì)象。 factory | BeanFactoryUtil.class :根據(jù)bean name獲取spring管理的bean實(shí)例。 hadle | ExceptionHandle.class :spring自帶的統(tǒng)一異常捕獲處理。 plugin | DatePlugin.class :自定義mybatis日期插件。 PagePlugin.class :自定義mybatis分頁(yè)插件。 PropertiesConfigurer.class :將外部配置的屬性文件讀取到 spring 容器中統(tǒng)一管理。 service | interface IbaseServie :基礎(chǔ)的service接口。 BaseService.class :基礎(chǔ)的service抽象類。 TokenService.class :鑒權(quán)token服務(wù)類。 util | CheckUtil.class :請(qǐng)求信息校驗(yàn)相關(guān)工具類。 DateUtil.class :日期相關(guān)工具類。 ResponseUtil.class :響應(yīng)信息工具類。 SecondsFormatSerializer.class :java.util.Date類型轉(zhuǎn)時(shí)間戳工具類。 TimestampSecondsFormatSerializer.class :java.sql.Timestamp類型轉(zhuǎn)時(shí)間戳工具類。 StringUtil.class :字符串相關(guān)工具類。 other | dao | interface StudentExtDao :使用例子,業(yè)務(wù)相關(guān)crud操作。 dto | QueryStudentSexPageDto.class :根據(jù)學(xué)生性別分頁(yè)查詢返回對(duì)象dto。 StudentPageDto.class :根據(jù)學(xué)生性別分頁(yè)查詢封裝的對(duì)象。 service | AddStudentService.class :插入學(xué)生數(shù)據(jù)接口。 DeleteStudentService.class :刪除學(xué)生數(shù)據(jù)接口。 FindStudentService.class :查詢學(xué)生數(shù)據(jù)接口。 UpdateStudentService.class :更新學(xué)生數(shù)據(jù)接口。 QueryStudentBySexService.class :根據(jù)學(xué)生性別分頁(yè)查詢接口。
2、mybatis的 mapper.xml講解(以下內(nèi)容均在resources/com/qbian文件夾下)
common | dao | StudentDao.xml :對(duì)應(yīng)common.dao.StudentDao接口。 other | dao | StudentExtDao.xml :對(duì)應(yīng)other.dao.StudentExtDao接口。
五、功能演示
1、token校驗(yàn)
這里的token我寫死在代碼里了,123456表示校驗(yàn)成功。我們先用插入數(shù)據(jù)接口測(cè)試一下,傳個(gè)錯(cuò)誤的token,如下圖:
授權(quán)token校驗(yàn)
2、請(qǐng)求參數(shù)校驗(yàn)
我們來(lái)看看插入數(shù)據(jù)接口還需要校驗(yàn)?zāi)男┲怠?/p>
// 校驗(yàn)請(qǐng)求參數(shù) CheckUtil.checkEmpty(params, "token", "sex", "age"); // 校驗(yàn) token tokenService.checkUserLogin(params.getString("token")); Student student = JSONObject.parseObject(params.toJSONString(), Student.class); studentDao.insert(student); return ResponseUtil.success();
然后我們少傳age字段試一下:
請(qǐng)求字段校驗(yàn)
3、插入數(shù)據(jù)
在插入數(shù)據(jù)之前我們先看看數(shù)據(jù)庫(kù)里都有哪些數(shù)據(jù):
初始化數(shù)據(jù)庫(kù)中的值
從上圖可以看出,數(shù)據(jù)庫(kù)中沒(méi)有任何數(shù)據(jù)。我們來(lái)執(zhí)行下插入接口。
測(cè)試插入接口
我們?cè)賮?lái)看下數(shù)據(jù)庫(kù):
調(diào)用插入接口后
數(shù)據(jù)庫(kù)已經(jīng)有數(shù)據(jù)了。
4、查詢數(shù)據(jù)
根據(jù)上一條數(shù)據(jù)的ID查詢
調(diào)用查詢接口
剛插入的數(shù)據(jù)我們也查詢出來(lái)了。
5、更新數(shù)據(jù)
更新一下查詢出來(lái)的數(shù)據(jù):
調(diào)用更新接口
然后我們?cè)俨樵円淮卧摋l數(shù)據(jù)
更新后再次查詢
可以看到性別和年齡都更新了,并且更新日期也是最新的了。
6、分頁(yè)查詢
先來(lái)看一下代碼:
// 校驗(yàn)請(qǐng)求參數(shù) CheckUtil.checkEmpty(params, "token", "sex", "pageNo", "pageSize"); // 校驗(yàn) token tokenService.checkUserLogin(params.getString("token")); // 根據(jù)性別分頁(yè)查詢 Student,查詢總數(shù)會(huì)自動(dòng)封裝到pageDto對(duì)象上 QueryStudentSexPageDto pageDto = JSONObject.parseObject(params.toJSONString(), QueryStudentSexPageDto.class); List<Student> students = studentExtDao.queryBySexWithPage(pageDto); StudentPageDto studentPageDto = new StudentPageDto(); // 查詢總數(shù)會(huì)自動(dòng)封裝到pageDto對(duì)象上 studentPageDto.setTotalSize(pageDto.getTotalSize()); studentPageDto.setStudents(students); return ResponseUtil.success(studentPageDto);
分頁(yè)查詢之前我們想要導(dǎo)入多一點(diǎn)測(cè)試數(shù)據(jù)。
分頁(yè)前測(cè)試數(shù)據(jù)
可以看到數(shù)據(jù)庫(kù)目前有十條測(cè)試數(shù)據(jù),男生有六條,年齡分別為19~24。好了,我們開(kāi)始調(diào)用分頁(yè)查詢接口:
調(diào)用分頁(yè)查詢接口返回結(jié)果
格式化一下返回?cái)?shù)據(jù):
分頁(yè)查詢返回結(jié)果整理
這和我們直接查詢數(shù)據(jù)庫(kù)看到的一樣。
7、刪除數(shù)據(jù)
最后就是刪除數(shù)據(jù)接口了,我們將第一條測(cè)試數(shù)據(jù)刪除掉。
調(diào)用刪除接口返回結(jié)果
然后我們?cè)诓樵円幌率欠裾娴膭h除了。
刪除后查詢
數(shù)據(jù)已經(jīng)被刪除了。
最后附上項(xiàng)目源碼:https://github.com/Qbian61/spring-mvc-mybatis
以上所述是小編給大家介紹的Spring mvc整合mybatis(crud+分頁(yè)插件)操作mysql,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Java如何將任意類型的Object對(duì)象轉(zhuǎn)換為相應(yīng)的實(shí)體對(duì)象
這篇文章主要介紹了Java如何將任意類型的Object對(duì)象轉(zhuǎn)換為相應(yīng)的實(shí)體對(duì)象問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Java注解中@Component和@Bean的區(qū)別
這篇文章主要介紹了@Component和@Bean的區(qū)別,在這給大家簡(jiǎn)單介紹下作用對(duì)象不同:@Component 注解作用于類,而 @Bean 注解作用于方法,具體實(shí)例代碼參考下本文2024-03-03Spring?Boot?中的?Native?SQL基本概念及使用方法
在本文中,我們介紹了 Spring Boot 中的 Native SQL,以及如何使用 JdbcTemplate 和 NamedParameterJdbcTemplate 來(lái)執(zhí)行自定義的 SQL 查詢或更新語(yǔ)句,需要的朋友跟隨小編一起看看吧2023-07-07Idea?中控制啟動(dòng)命令的詳細(xì)過(guò)程?區(qū)分環(huán)境案例詳解
這篇文章主要介紹了Idea?中控制啟動(dòng)命令的詳細(xì)過(guò)程?區(qū)分環(huán)境案例詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08Java實(shí)現(xiàn)克隆的三種方式實(shí)例總結(jié)
這篇文章主要介紹了Java實(shí)現(xiàn)克隆的三種方式,結(jié)合實(shí)例形式總結(jié)分析了java淺復(fù)制、深復(fù)制以及使用serializable實(shí)現(xiàn)深復(fù)制的相關(guān)操作技巧,需要的朋友可以參考下2018-08-08Spring實(shí)戰(zhàn)之屬性覆蓋占位符配置器用法示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之屬性覆蓋占位符配置器用法,結(jié)合實(shí)例形式分析了Spring屬性覆蓋占位符配置器相關(guān)原理、配置與使用技巧,需要的朋友可以參考下2019-12-12詳解Spring Boot使用redis實(shí)現(xiàn)數(shù)據(jù)緩存
本篇文章主要介紹了詳解Spring Boot使用redis實(shí)現(xiàn)數(shù)據(jù)緩存,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04