Spring + SpringMVC + Mybatis整合
依賴(lài)
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<!-- Spring-JDBC,要和spring-webmvc的版本一致 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!-- MyBatis-Spring 整合jar包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.2</version>
</dependency>
<!-- MySQL驅(qū)動(dòng)jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
</dependency>
<!-- DBCP連接池 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- 添加jackson廊散,自動(dòng)轉(zhuǎn)換為JSON數(shù)據(jù) -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
<!-- 添加jstl標(biāo)簽庫(kù) -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- 導(dǎo)入aspectj依賴(lài) -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<!-- 導(dǎo)入spring的aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<!-- 添加文件上傳的依賴(lài) -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
配置數(shù)據(jù)庫(kù)連接信息 --- db.properties文件
url=jdbc:mysql://localhost:3306/db_blog3?useUnicode=true&characterEncoding=utf8
driver=com.mysql.jdbc.Driver
user=root
password=root
initSize=2
maxSize=10
Mybatis和Spring整合 --- spring-dao.xml
- 讀取
db.properties
文件配置DBCP
連接池創(chuàng)建數(shù)據(jù)源
- 使用上面的數(shù)據(jù)源配置
SqlSessionFactoryBean
- 配置組件掃描Mybatis的接口
mapper
的包市咆,用于創(chuàng)建mapper接口對(duì)象
- 配置批量掃描Mybatis的
XXMapper.xml
文件的MapperScannerConfigurer
- 配置事務(wù)管理器
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 組件掃描 用于自動(dòng)創(chuàng)建mapper包下的所有接口的對(duì)象,否則將不能使用@Resource注解的方式注入接口對(duì)象 -->
<context:component-scan base-package="cn.tedu.blog.mapper" />
<!-- 配置MapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 用于配置持久層接口在哪里这敬,指定mapper的包 -->
<property name="basePackage" value="cn.tedu.blog.mapper" />
</bean>
<!-- 加載db.properties,其中定義了數(shù)據(jù)庫(kù)的配置信息 -->
<util:properties id="dbConfig" location="classpath:db.properties" />
<!-- 數(shù)據(jù)源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="#{dbConfig.url}" />
<property name="driverClassName" value="#{dbConfig.driver}" />
<property name="username" value="#{dbConfig.user}" />
<property name="password" value="#{dbConfig.password}" />
<property name="initialSize" value="#{dbConfig.initSize}" />
<property name="maxActive" value="#{dbConfig.maxSize}" />
</bean>
<!-- 配置SqlSessionFactoryBean -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 用于配置數(shù)據(jù)庫(kù)連接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 用于配置持久層映射文件在哪里,所有的xml文件搜锰,使用通配符 -->
<property name="mapperLocations" value="classpath:mappers/*.xml" />
</bean>
<!-- 配置事務(wù)管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入數(shù)據(jù)源维贺,這里使用的是上面配置好的DataSource -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 開(kāi)啟事務(wù)注解 ,transaction-manager指定的是上面配置的事務(wù)管理器的id-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
Spring與SpringMVC不需要整合
- 我們只需要?jiǎng)?chuàng)建一個(gè)
springMVC.xml
文件即可
- 掃描
controller
包下告唆,組件掃描域携,自動(dòng)創(chuàng)建對(duì)象
- 配置視圖解析器
- 配置注解驅(qū)動(dòng)
- 配置攔截器
- 配置上傳文件的解析器
- .....................
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 組件掃描 自動(dòng)創(chuàng)建對(duì)象 -->
<context:component-scan base-package="cn.tedu.blog.controller" />
<!-- 配置ViewResolver視圖解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置驅(qū)動(dòng)簇秒,用于@ResponseBody的使用 -->
<mvc:annotation-driven></mvc:annotation-driven>
<mvc:interceptors>
<mvc:interceptor>
<!-- 配置攔截器的路徑 -->
<mvc:mapping path="/blogger/*" />
<mvc:mapping path="/blogType/*" />
<!-- 配置不攔截的路徑 -->
<mvc:exclude-mapping path="/blogger/showLogin.do"></mvc:exclude-mapping>
<mvc:exclude-mapping path="/blogger/login.do"></mvc:exclude-mapping>
<mvc:exclude-mapping path="/blogger/showInfo.do"></mvc:exclude-mapping>
<bean class="cn.tedu.blog.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<!-- 上傳組件的解析器 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上傳文件大小 -->
<property name="maxUploadSize" value="10000000"></property>
<!-- 請(qǐng)求的編碼格式,必須和jSP的pageEncoding屬性一致秀鞭,以便正確讀取表單的內(nèi)容趋观,默認(rèn)為ISO-8859-1 -->
<property name="defaultEncoding" value="utf-8"></property>
</bean>
</beans>
業(yè)務(wù)層的配置文件 -- spring-service.xml
- 配置組件自動(dòng)掃描業(yè)務(wù)層的類(lèi),自動(dòng)創(chuàng)建對(duì)象
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
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/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 組件掃描 -->
<context:component-scan base-package="cn.tedu.blog.service" />
</beans>
配置 web.xml 文件
- 配置
POST
中文亂碼過(guò)濾器
- 配置
Spring
的監(jiān)聽(tīng)器ContextLoaderListener
锋边,用于在容器啟動(dòng)的時(shí)候就加載spring的配置文件
- 配置
SpringMVC
的前端控制器DispatcherServlet
皱坛,其中指定的是springMVC的配置文件springMVC.xml
<!-- 解決POST提交方式的中文亂碼的過(guò)濾器 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置SpringMVC的前端控制器 -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- springmvc的配置文件 -->
<param-value>classpath:springMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 使用ContextLoaderListener配置spring的監(jiān)聽(tīng)器,主要是在啟動(dòng)的時(shí)候加載spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 指定所有的spring配置文件豆巨,這里使用 * 通配符 -->
<param-value>classpath:spring-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
包的結(jié)構(gòu)