SpringMVC


1 SpringMVC 介绍

​解析 :Spring mvc 是Spring框架的一部分,基于mvc 的表现层框架 , 用于web项目开发

SpringMvc.md-LMLPHP

1.1 mvc 设计模式

​简单来说就是 m(model) 模型- v(view) 视图 - c(controller) 控制器 , 三次架构设计模式 。 主要用于前端页面的展示和后端业务数据处理逻辑分离

​模式优点:分层设计 , 实现我们业务系统各个组件之间的解耦 , 有利于业务系统的可拓展性 , 维护性 , 有利于并行开发,提升开发效率

SpringMvc.md-LMLPHP

1.2 入门案例
1.2.1 准备主配置文件
  • 创建一个web 项目

  • 加入pom.xml的依赖

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    
    	<groupId>com.dsdd.weyou</groupId>
    	<artifactId>springmvc-first</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    
    	<packaging>war</packaging>
    
    	<name>springmvc-first Maven Webapp</name>
    	<url>http://maven.apache.org</url>
    
    	<properties>
    		<!-- spring版本号 -->
    		<spring.version>4.3.8.RELEASE</spring.version>
    		<!-- jstl标签版本 -->
    		<jstl.version>1.2</jstl.version>
    	</properties>
    
    	<dependencies>
    		<!-- springmvc依赖包 -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-core</artifactId>
    			<version>${spring.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-web</artifactId>
    			<version>${spring.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-tx</artifactId>
    			<version>${spring.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-jdbc</artifactId>
    			<version>${spring.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-webmvc</artifactId>
    			<version>${spring.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-aop</artifactId>
    			<version>${spring.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context-support</artifactId>
    			<version>${spring.version}</version>
    		</dependency>
    		<!-- JSTL标签类 -->
    		<dependency>
    			<groupId>jstl</groupId>
    			<artifactId>jstl</artifactId>
    			<version>${jstl.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.12</version>
    			<scope>test</scope>
    		</dependency>
    	</dependencies>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.apache.tomcat.maven</groupId>
    				<artifactId>tomcat7-maven-plugin</artifactId>
    				<version>2.1</version>
    				<configuration>
    					<!-- tomcat 的端口号 -->
    					<port>8080</port>
    					<!-- 访问应用的路径 -->
    					<path>/springmvc-first</path>
    					<!-- URL按UTF-8进行编码,解决中文参数乱码 -->
    					<uriEncoding>UTF-8</uriEncoding>
    					<!-- tomcat名称 -->
    					<server>tomcat7</server>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>
    </project>
    
  • 加入SpringMvc.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"
    	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-4.0.xsd
            http://www.springframework.org/schema/mvc 
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
    </beans> 
    
1.2.2 web.xml配置前端控制器
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>springmvc-first</display-name>
  
  <!-- 配置前端控制器:DispatcherServlet -->
  <servlet>
  	<servlet-name>springmvc-first</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	
  	<!-- 加载主配置文件 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springmvc.xml</param-value>
  	</init-param>
  	
  	<!-- 配置什么时候加载前端控制器,说明:
  	1.大于等于0的整数,表示在web容器启动的时候加载
  	2.小于0的整数,表示在第一次请求到达的时候加载 -->
  	<load-on-startup>1</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springmvc-first</servlet-name>
  	<!-- 配置什么样的请求进入前端控制器,说明:
  	1.*.do,配置以.do的请求,进入前端控制器
  	2./,配置所有请求都进入前端控制器 -->
  	<url-pattern>*.do</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>
1.2.3 准备jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>springmvc入门程序</title>
</head>
<body>
	你好,世界。${hello}
</body>
</html>
1.2.4 准备控制器controller
// @Controller注解:用于标记普通的java类,作为控制器
@Controller
public class HelloController {
	
	// ModelAndView:模型和视图,用于设置响应的模型数据,用于设置响应的视图
	// @RequestMapping注解:配置请求的url
	@RequestMapping("/hello.do")
	public ModelAndView hello(){
		
		// 1.创建ModelAndView对象
		ModelAndView mav = new ModelAndView();
		
		// 2.设置响应的模型数据
		// addObject方法:设置响应的模型数据
		// attributeName参数:模型的名称(hello)
		// attributeValue参数:模型的数据
		mav.addObject("hello", "springmvc!!!");
		
		// 3.设置响应的视图
		// setViewName方法:设置视图名称
		// viewName参数:视图名称(jsp页面的物理路径)
		mav.setViewName("/WEB-INF/jsp/hello.jsp");
		
		return mav;
		
	}

}
1.2.5 配置扫描controller
<?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"
	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-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        
        <!-- 配置组件扫描controller -->
        <context:component-scan base-package="com.dsdd.weyou.controller"></context:component-scan>

</beans> 

2 SpringMVC 三大组件

2.1 处理器映射器

​解析 : 根据请求的url , 查找处理器

2.1.1 默认处理器映射器(已过时)
org.springframework.web.servlet.HandlerMapping=org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
2.1.2 配置处理器映射器

​在springmvc.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"
	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-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        
        <!-- 配置组件扫描controller -->
        <context:component-scan base-package="com.dsdd.weyou.controller"></context:component-scan>
        
        <!-- 配置处理器映射器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
</beans> 
2.2 处理器适配器

​解析:执行处理器方法

2.2.1 默认处理器适配器(已过时)
org.springframework.web.servlet.HandlerAdapter=org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,
	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,
	org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
2.2.2 配置处理器适配器
<?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"
	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-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        
        <!-- 配置组件扫描controller -->
        <context:component-scan base-package="com.dsdd.weyou.controller"></context:component-scan>
        
        <!-- 配置处理器映射器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
        
        <!-- 配置处理器适配器 -->
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>

</beans> 
2.3 实际开发中配置映射器和适配器
<?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"
	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-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        
        <!-- 配置组件扫描controller -->
        <context:component-scan base-package="com.dsdd.weyou.controller"></context:component-scan>
        
        <!-- 配置处理器映射器 -->
        <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->
        
        <!-- 配置处理器适配器 -->
       <!--  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->
       
       <!-- 处理器映射器,处理器适配器配置方式二 ,说明;
       1.等于同时配置RequestMappingHandlerMapping/RequestMappingHandlerAdapter
       2.企业项目中,推荐使用这种方式-->
       <mvc:annotation-driven></mvc:annotation-driven>

</beans> 
2.3 视图解析器

​解析 :把逻辑视图(在controller设置的视图名称)解析成物理视图(在浏览器看到的页面)。

2.3.1 默认视图解析器
org.springframework.web.servlet.ViewResolver=org.springframework.web.servlet.view.InternalResourceViewResolver
2.3.2 配置视图解析器
<?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"
	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-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        
        <!-- 配置组件扫描controller -->
        <context:component-scan base-package="com.dsdd.weyou.controller"></context:component-scan>
        
        <!-- 配置处理器映射器 -->
        <!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> -->
        
        <!-- 配置处理器适配器 -->
       <!--  <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> -->
       
       <!-- 处理器映射器,处理器适配器配置方式二 ,说明;
       1.等于同时配置RequestMappingHandlerMapping/RequestMappingHandlerAdapter
       2.企业项目中,推荐使用这种方式-->
       <mvc:annotation-driven></mvc:annotation-driven>
       
       <!-- 配置视图解析器 -->
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       		<!-- 配置视图的公共目录路径 -->
       		<property name="prefix" value="/WEB-INF/jsp/"></property>
       		
       		<!-- 配置视图的扩展名称 -->
       		<property name="suffix" value=".jsp"></property>
       </bean>

</beans> 

3 框架原理

3.1 spring mvc 流程

​解析 :

  • 1 用户页面请求 , 前端控制器接受用户请求***(DispatcherServlet 为springMvc的核心类)***

  • 2 请求处理器映射器 , 查找处理器(处理器映射器:HandlerMapping查找处理器)

  • 3 根据请求的url , 查找处理器(处理器Controller)

  • 4 返回处理器到前端控制器

  • 5 请求处理器适配器 , 执行处理器(处理器适配器:HandlerAdapter执行处理器)

  • 6 执行处理器 ,返回模型和视图

  • 7 请求解析视图 , 视图解析器(ViewResolve解析视图)

  • 8 解析视图:jsp

  • 9 返回视图 , 响应用户

SpringMvc.md-LMLPHP

3.2 项目分层

SpringMvc.md-LMLPHP

3.3 spring mvc 整合
3.3.1 pom.xml 加入依赖(mybatis框架包,spring框架包(包括springmvc), myabtis-spring整合包,数据库驱动包,数据源连接池包)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.dsdd.weyou</groupId>
	<artifactId>ssm</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<packaging>war</packaging>

	<name>ssm Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<properties>
		<!-- spring版本号 -->
		<spring.version>4.3.8.RELEASE</spring.version>
		<!-- aspectj版本号 -->
		<aspectj.version>1.6.12</aspectj.version>
		<!-- jstl标签版本 -->
		<jstl.version>1.2</jstl.version>
		<!-- mybatis版本号 -->
		<mybatis.version>3.4.5</mybatis.version>
		<!-- mybatis-spring整合包版本 -->
		<mybatis.spring.version>1.3.1</mybatis.spring.version>
		<!-- mysql版本号 -->
		<mysql.version>5.1.30</mysql.version>
		<!-- dbcp数据源连接池jar包 -->
		<dbcp.version>1.2.2</dbcp.version>
		<!-- log4j日志包版本 -->
		<slf4j.version>1.7.7</slf4j.version>
		<log4j.version>1.2.17</log4j.version>
		<!-- commons-lang版本 -->
		<commons.lang.version>2.6</commons.lang.version>
	</properties>

	<dependencies>
		<!-- springmvc依赖包 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>${aspectj.version}</version>
		</dependency>
		<!-- JSTL标签类 -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>${jstl.version}</version>
		</dependency>
		<!-- mybatis核心包 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>${mybatis.version}</version>
		</dependency>
		<!-- mybatis-spring整合包 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>${mybatis.spring.version}</version>
		</dependency>
		<!-- 导入Mysql数据库链接jar包 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.version}</version>
		</dependency>
		<!-- 导入dbcp数据源连接池jar包 -->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>${dbcp.version}</version>
		</dependency>
		<!-- 日志文件管理包 -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>${log4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>${slf4j.version}</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>${commons.lang.version}</version>
		</dependency>

		<!-- jsp依赖包,只在编译时需要 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.0</version>
			<scope>provided</scope>
		</dependency>
	</dependencies>
	<build>
		<finalName>ssm</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.1</version>
				<configuration>
					<!-- tomcat 的端口号 -->
					<port>8080</port>
					<!-- 访问应用的路径 -->
					<path>/ssm</path>
					<!-- URL按UTF-8进行编码,解决中文参数乱码 -->
					<uriEncoding>UTF-8</uriEncoding>
					<!-- tomcat名称 -->
					<server>tomcat7</server>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
3.3.2 准备配置文件(配置别名)
<?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>

	<!-- 配置别名 -->
	<typeAliases>
		<!-- 包扫描方式配置别名 -->
		<package name="com.dsdd.weyou.po"/>
	</typeAliases>
	

</configuration>
3.3.3 springMvc.xml(配置组件扫描controller,处理器映射器,处理器适配器,视图解析器)
<?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"
	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-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd">
        
        <!-- 配置组件扫描controller -->
        <context:component-scan base-package="com.dsdd.weyou.controller"></context:component-scan>
        
        <!-- 注解驱动配置处理器映射器,处理器适配器 -->
        <mvc:annotation-driven></mvc:annotation-driven>
        
        <!-- 配置视图解析器 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        	<!-- 配置视图的公共目录路径 -->
        	<property name="prefix" value="/WEB-INF/jsp/"></property>
        	
        	<!-- 配置视图扩展名称 -->
        	<property name="suffix" value=".jsp"></property>
        </bean>

</beans>  
3.3.4 applicationContext-dao.xml(配置数据源对象,mybatis核心对象SqlSessionFactory,mapper扫描器)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util 
	http://www.springframework.org/schema/util/spring-util-4.0.xsd">
	
	<!-- 加载db.properties文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 配置数据源 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
		destroy-method="close">
		
		<property name="driverClassName" value="${db.driverClassName}"/>
		<property name="url" value="${db.url}" />
		<property name="username" value="${db.username}" />
		<property name="password" value="${db.password}" />
		
		<!-- 最大连接数量 -->
		<property name="maxActive" value="${db.maxActive}"/>
		<!-- 最小空闲连接 -->
		<property name="minIdle" value="${db.minIdle}"/>
		<!-- 最大空闲连接 -->
		<property name="maxIdle" value="${db.maxIdle}"/>
		<!-- 初始化连接数 -->
		<property name="initialSize" value="${db.initialSize}"/>
		<!-- 超时等待时间以毫秒为单位 -->
		<property name="maxWait" value="${db.maxWait}"/>
	</bean>
	
	<!-- 配置sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 设置数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		
		<!-- 加载myabtis核心配置文件 -->
		<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property>
	</bean>
	
	<!-- 配置mapper扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 配置要扫描的包,说明:
		 1.如果有多个包,以半角逗号进行分割:","-->
		 <property name="basePackage" value="com.dsdd.weyou.mapper"></property>
	</bean>
	
</beans>
3.3.5 applicationContext-service.xml(配置组件扫描 service)
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:p="http://www.springframework.org/schema/p"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10-06 14:01