spring+springMvc+Mybatis的简单整合
1、导包
spring核心包、mybatis、mybatis-spring、connection-java、\spring-mvc\spring-webmvc\servlet-api
2、jdbc文件
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=root
3、spring.xml的配置
applicationContext.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 扫描jdbc -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置SqlSessionFactory:给spring管理 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 扫描mapper.xml的配置文件(统一扫描) -->
<property name="mapperLocations" value="classpath:cn/itsource/mapper/*Mapper.xml" />
<!-- 配置别名 -->
<property name="typeAliasesPackage" value="cn.itsource.domain,cn.itsource.query" ></property>
</bean>
<!-- 扫描mapper层:创建代理对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 这句可以省略,自动注册 -->
<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> -->
<property name="basePackage" value="cn.itsource.mapper"></property>
</bean>
<!-- 扫描service -->
<context:component-scan base-package="cn.itsource.service"></context:component-scan>
</beans>
4、SpringMvc的配置
4.1 web.xml的配置(前台请求根据前置控制器找到对应的controller)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<!-- 方案1:独立容器方案,Spring和SpringMVC都有自己配置文件,实现责任分离 -->
<!-- 1.1 Spring容器初始化 :监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 加载spring的核心配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 1.2 配置SpringMVC前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- /*会拦截所有包括jsp -->
<!-- /不会拦截jsp,但是会拦截静态资源html、css、js、图片等 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 1.3 解决post请求参数中文乱码问题 -->
<!--
post请求乱码解决
get请求实在tomcat的server.xml中配置URIEncoding="UTF-8"
-->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--指定字符编码-->
<init-param>
<!--因为当前成员变量为encoding,所以param-name必须为encoding-->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!--强制指定字符编码,即使request或response设置了字符编码,也会强制使用当前设置的,任何情况下强制使用此编码-->
<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>
</web-app>
4.2 mvc.xml的配置(包含支持mvc注解,视图解析器等)
<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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1. 配置扫描包路径子容器只扫描controller包 -->
<context:component-scan base-package="cn.itsource.controller"></context:component-scan>
<!-- 2. 配置静态资源放行 -->
<mvc:default-servlet-handler/>
<!-- 3. 配置开启Spring对mvc的支持,支持@RequestMapping注解 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 4.配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>