applicationContext-dao.xmlhtml
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 加載db.properties文件中的內容 中的key要有必定的規則 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="maxActive" value="30"/> <property name="maxIdle" value="5"/> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加載數據庫鏈接池 --> <property name="dataSource" ref="dataSource"></property> <!-- 加載mybatis全局配置文件 --> <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property> </bean> <!-- 配置mapper掃描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 掃描包路徑,若是須要掃描多個,中間使用半角的逗號隔開 --> <property name="basePackage" value="com.mybatis.mapper"></property> <property name="SqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
applicationContext-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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 商品管理的service --> <bean id="itemsService" class="com.mybatis.service.impl.ItemsServiceImpl"></bean> </beans>
applicationContext-transaction.xmljava
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 事務管理器 --> <!-- 對mybatis操做數據庫事務控制 spring使用jdbc的事務控制類 --> <bean id="transactionManager" class="DataSourceTransactionManager"> <!-- dataSource在 applicationContext-dao.xml中配置了--> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 通知 --> <!-- transaction-manager 表示把這通知給誰 transactionManager就是上面的事務管理器的id--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 傳播行爲 --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> <tx:method name="select*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- aop 通知是有aop來調用 --> <aop:config> <!-- pointcut 表示aop的切點 --> <!-- execution(* com.mybatis.service.impl.*.*(..)) 表示要切com.mybatis.service.impl這個包下面的全部類的全部方法 --> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.mybatis.service.impl.*.*(..))"/> </aop:config> </beans>
SpringMvc.xmlweb
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!--Spring組件掃描器 註解的處理器能夠單個配置 可是咱們能夠使用spring框架的組件掃描的方式來配置 base-package:須要掃描哪一個包下面的處理器 --> <context:component-scan base-package="com.mybatis.controller"></context:component-scan> <!-- 可代替上面註解的處理器和處理器適配器的配置 建議使用 --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 視圖解析器 --> <!-- 解析jsp 默認使用jstl標籤 classpath下得有jstl包 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 配置jsp路徑的前綴 --> <property name="prefix" value="/WEB-INF/jsp/"></property> <property name="suffix" value=".jsp"></property> </bean> </beans>
web.xmlspring
<?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>mybatis</display-name> <!-- springmvc前端控制器 --> <servlet> <servlet-name>mybatis</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 經過contextConfigLocation來配置springmvc加載的配置文件(處理器映射器、處理器適配器等等) --> <!-- 若是不配置 contextConfigLocation 默認加載的是/WEB-INF/servler名稱-servlet.xml(springmvc-servlet.xml)--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:SpringMvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>mybatis</servlet-name> <!-- 第一種 *.action:訪問以.action結尾的由DispatcherServlet進行解析 --> <!-- 第二種/ :全部由DispatcherServlet進行解析 須要配置靜態文件不被解析 使用此種能夠實現RESTFul分割的url --> <!-- 第三種/* :這樣配置不對,最終要轉發到一個jsp頁面時,仍然會由DispatcherServlet解析jsp地址 ,不能根據jsp頁面找到handler 會報錯--> <url-pattern>*.action</url-pattern> </servlet-mapping> <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> </web-app>
所有配置好以後 還須要在web.xml中加載applicationContext-*.xml(spring的配置文件)文件 包括(applicationContext-dao.xml,applicationContext-transaction.xml事務管理,applicationContext-service.xml)sql
<?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>mybatis</display-name> <!-- 加載spring的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- springmvc前端控制器 --> <servlet> <servlet-name>mybatis</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 經過contextConfigLocation來配置springmvc加載的配置文件(處理器映射器、處理器適配器等等) --> <!-- 若是不配置 contextConfigLocation 默認加載的是/WEB-INF/servler名稱-servlet.xml(springmvc-servlet.xml)--> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc-cfg.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>mybatis</servlet-name> <!-- 第一種 *.action:訪問以.action結尾的由DispatcherServlet進行解析 --> <!-- 第二種/ :全部由DispatcherServlet進行解析 須要配置靜態文件不被解析 使用此種能夠實現RESTFul分割的url --> <!-- 第三種/* :這樣配置不對,最終要轉發到一個jsp頁面時,仍然會由DispatcherServlet解析jsp地址 ,不能根據jsp頁面找到handler 會報錯--> <url-pattern>*.action</url-pattern> </servlet-mapping> <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> </web-app>