spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)
**這個(gè)整合。僅僅是最主要的整合,而且是xml配置文件的方式之中的一個(gè),即當(dāng)中的mybatis是採(cǎi)用非mapper接口的方式。(第二遍採(cǎi)用mapper接口方式。第三遍採(cǎi)用注解的方式;第四篇採(cǎi)用注解基于maven的方式)。記錄在這里,以免下次忘記時(shí)留作備用。
===================================================================================================**
一。總體結(jié)構(gòu)
二,所需jar包:
實(shí)質(zhì)上并不須要所有導(dǎo)入,這里為了方便就所有導(dǎo)入啦。
(1)spring4所有jar包
(2)mybatis所有jar包
(3)mybatis-spring.jar整合須要這個(gè)包,如今spring官方已經(jīng)不提供整合的jar,所以須要另外下載mybatis官方提供的整合jar。
(4)這里使用數(shù)據(jù)源dbcp.jar,依賴pool.jar,因此也須要導(dǎo)進(jìn)來。
以及數(shù)據(jù)庫(kù)連接驅(qū)動(dòng)jar也須要。
三,整合spring。springmvc和mybatis配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"><display-name>zh2demo</display-name><welcome-file-list><welcome-file>index.html</welcome-file><welcome-file>index.htm</welcome-file><welcome-file>index.jsp</welcome-file><welcome-file>default.html</welcome-file><welcome-file>default.htm</welcome-file><welcome-file>default.jsp</welcome-file></welcome-file-list><!-- 上下文參數(shù) --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:com/mapc/conf/application-dao.xml,classpath:com/mapc/conf/application-service.xml</param-value></context-param><context-param><param-name>log4jConfigLocation</param-name><param-value>classpath:com/mapc/conf/log4j.properties</param-value></context-param><!-- 配置log4j日志監(jiān)聽器,開啟日志記錄,容器須要 --><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener><!-- 配置context上下文監(jiān)聽器,創(chuàng)建root容器,和web容器集成在一起 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置springmvc控制器,會(huì)創(chuàng)建web上下文容器。而且會(huì)設(shè)置root上下文容器為此容器的父容器 --><servlet><servlet-name>controller</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:com/mapc/conf/application-controller.xml</param-value></init-param><load-on-startup>1</load-on-startup><!-- web容器啟動(dòng)時(shí)就創(chuàng)建該context容器 --></servlet><servlet-mapping><servlet-name>controller</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- 利用spring提供的編碼控制過濾器 --><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></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping> </web-app>root父容器所需的配置文件:
1。application-dao.xml
特別注意:須要引入mybatis-spring.jar-->
<bean id="sqlSessionFactory" class="</span><span style="color:#000099;">org.mybatis.spring.SqlSessionFactoryBean</span><span style="color:#ff0000;">"> </span><span style="color:#000099;"><property name="configLocation" value="classpath:com/mapc/conf/mybatisconf/Configuration.xml"/> </span><span style="color:#ff0000;"> </span><span style="color: rgb(255, 0, 0); white-space: pre;"> </span><span style="color:#ff0000;"><!--這里提供了集成持久層框架mybatis的集成點(diǎn)--> <property name="dataSource" ref="dataSource"></property> </bean> </span><!-- 配置Session模板類 --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> <!-- 配置事務(wù)管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <!-- 配置增強(qiáng)通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="query*" propagation="REQUIRED"/> <tx:method name="get*" propagation="REQUIRED"/> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="*" read-only="true"/> </tx:attributes> </tx:advice> <!-- 配置共享事務(wù) --> <aop:config expose-proxy="true"> <!-- 配置切點(diǎn) --> <aop:pointcut expression="execution(* com.mapc.service..*.*(..))" id="txPointcut"/> <!-- 配置切面 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> <!-- 裝配StudentDao --> <bean id="studentDao" class="com.mapc.dao.impl.StudentDaoImpl" p:sqlSession-ref="sqlSessionTemplate"/> </beans>屬性文件:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/test?useUnicode\=true&characterEncoding\=UTF8 jdbc.username=root jdbc.password=root2,application-service.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"><!-- 裝配StudentService --><bean id="studentService" class="com.mapc.service.impl.StudentServiceImpl"p:studentDao-ref="studentDao"/></beans>3,其它,這里視詳細(xì)需求。
web層子容器的配置文件:
>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 處理器映射器:這樣的方式,從url映射到詳細(xì)的控制器 --> <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <!-- 處理器適配器 --> <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> <!-- 視圖解析器,配置ContentNegotiatingViewResolver更通用 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/pages/" p:suffix=".jsp"/> <!-- 靜態(tài)資源的訪問控制 --> <mvc:resources location="/resources/" mapping="/resources/**"/> <!-- 處理器 --> <bean id="/queryStudentList.do" class="com.mapc.controller.StudentController" p:studentService-ref="studentService"/> </beans>mybatis配置文件Configuration.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><mappers><mapper resource="com/mapc/conf/mybatisconf/Student.xml"/></mappers></configuration>mybatis Mapper的配置文件:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="Student"><resultMap type="com.mapc.bean.Student" id="studentResultMapper"><id column="ID" jdbcType="VARCHAR" property="id"/><result column="NAME" jdbcType="VARCHAR" property="name"/><result column="AGE" jdbcType="INTEGER" property="age"/></resultMap><select id="queryStudentList" resultMap="studentResultMapper">select ID,NAME,AGE from STUDENT </select> </mapper>四,java源代碼
———-bean
———-dao以及dao.impl
package com.mapc.dao;import java.util.List;import com.mapc.bean.Student;/*** 持久層接口* @author DC**/ public interface StudentDao {/*** 查詢所有學(xué)生*/List<Student> queryStudentList(); } package com.mapc.dao.impl;import java.util.List;import org.apache.ibatis.session.SqlSession; import org.mybatis.spring.SqlSessionTemplate;import com.mapc.bean.Student; import com.mapc.dao.StudentDao; /*** 持久層詳細(xì)實(shí)現(xiàn)類之中的一個(gè)* @author DC**/ public class StudentDaoImpl implements StudentDao {private SqlSessionTemplate sqlSessionTemplate;public void setSqlSession(SqlSessionTemplate sqlSessionTemplate) {this.sqlSessionTemplate = sqlSessionTemplate;}@Overridepublic List<Student> queryStudentList() {return sqlSessionTemplate.selectList("Student.queryStudentList");}}————service和service.impl
package com.mapc.service;import java.util.List;import com.mapc.bean.Student; import com.mapc.dao.StudentDao;/*** 學(xué)生相關(guān)的業(yè)務(wù)接口* @author DC**/ public interface StudentService {/*** 查詢學(xué)生信息的業(yè)務(wù)接口方法*/public List<Student> queryStudentList();} package com.mapc.service.impl;import java.util.List;import com.mapc.bean.Student; import com.mapc.dao.StudentDao; import com.mapc.service.StudentService; /*** 學(xué)生業(yè)務(wù)的詳細(xì)實(shí)現(xiàn)類* @author DC**/ public class StudentServiceImpl implements StudentService {/*** 注入StudentDao*/private StudentDao studentDao;public void setStudentDao(StudentDao studentDao) {this.studentDao = studentDao;}@Overridepublic List<Student> queryStudentList() {return studentDao.queryStudentList();}}——–controller頁(yè)面處理器
package com.mapc.controller;import java.util.List;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller;import com.mapc.bean.Student; import com.mapc.service.StudentService; /*** 學(xué)生控制類* @author DC**/ public class StudentController implements Controller {/*** 注入學(xué)生業(yè)務(wù)類*/private StudentService studentService;public void setStudentService(StudentService studentService) {this.studentService = studentService;}@Overridepublic ModelAndView handleRequest(HttpServletRequest req,HttpServletResponse resp) throws Exception {ModelAndView mv=new ModelAndView();List<Student> list=studentService.queryStudentList();mv.addObject("studentList",list);System.out.println("----------------:"+list.toString());mv.setViewName("showStudentList");return mv;}}五,數(shù)據(jù)表
六。執(zhí)行測(cè)試(利用別的測(cè)試手段也得)
菜鳥首次整合,盡管非常easy,在第一次整合的時(shí)候還是出現(xiàn)各種難以預(yù)料的錯(cuò)誤的。
為了更加清晰這里面的情況,整合了第二次,還是實(shí)踐可以驗(yàn)證整理。
多實(shí)踐,多思考,謹(jǐn)記!
轉(zhuǎn)載于:https://www.cnblogs.com/zsychanpin/p/7278572.html
總結(jié)
以上是生活随笔為你收集整理的spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 9.11排序与查找(三)——给定一个排序
- 下一篇: SQLSERVER 2008 R2 事务