Spring整合Springmvc和Mybatis

1.Spring整合SpringMVC

1.1 配置web.xml

在web.xml中配置两方面内容

  • Spring的ApplicationContext 载入(生成Spring容器)
  • 配置SpringMVC(DispatcherServlet)


	usermanage

	
		contextConfigLocation
		classpath:spring/applicationContext*.xml
	
	
	
		org.springframework.web.context.ContextLoaderListener
	

	
	
		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF8
		
	
	
		encodingFilter
		/*
	
	
	
	
		usermanage
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:spring/usermanage-servlet.xml
		
		1
	

	
		usermanage
		/
	
	
	
		index.jsp
	


1.ContextLoaderListener监听器的作用:
在Spring第二天课程中的有详解介绍

(1)原始加载Spring容器方式的缺点—每次需要使用Spring中管理的bean前,都要初始化容器,销毁资源,性能低
在这里插入图片描述
(2)解决思路:web服务启动后,保证spring容器只有一个
我们可以将Spring容器绑定到web servlet容器上,让web容器来管理spring容器的创建和销毁

(3)解决方案:通过监听器来监听,当servletContext初始化时.创建spring容器,并添加到servletContext属性中保存

ServletContext在Web服务运行过程中是唯一的, 其初始化的时候,会自动执行ServletContextListener 监听器 (用来监听上下文的创建和销毁),具体步骤为:
编写一个ServletContextListener监听器,在监听ServletContext到创建的时候,创建Spring容器,并将其放到ServletContext的属性中保存(setAttribute(Spring容器名字,Spring容器对象) )。

Spring提供了一个叫ContextLoaderListener的监听器

原理:ContextLoaderListener加载过程

(4)通过ServletContext 获取Spring容器对象

方式一:
ApplicationContext applicationContext = 
(ApplicationContext)this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

方式二:(推荐)
WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

注意点:

  • 1.ContextLoaderListener会去加载servletContext内部属性contextConfigLocation所对应的值,此值是一个文件路径,如果没有显式指定,则使用默认位置("/WEB-INF/applicationContext.xml")

  • 2.Spring的配置文件applicationContext.xml通常放在src下(maven项目下是src/main/resource),所以此处我们需要指定contextConfigLocation

    • classpath:代表根路径,即src文件夹下

	contextConfigLocation
	classpath:spring/applicationContext*.xml
 

2.context-param的作用: 全局配置参数
容器会将读取到转化为键值对,并交给ServletContext,在Servlet中通过getServletContext().getInitParameter(“param-name”)获取值

1.在web.xml中配置
  
   hello  
   world  
 

2.在servlet中获取全局配置参数
ServletContext servletContext = getServletContext();
String value = (String) servletContext.getInitParameter("hello");

参考:Web.xml配置详解之context-param


3.DispatcherServlet的作用:

DispatcherServlet也是一个servlet,它读取servlet中名为contextConfigLocation的文件路径,如果没有显示指明则默认读取/WEB-INF/(servletame)-servlet.xml这个配置文件,同样的我们应该修改这个读取位置

4.init-param的作用:

web.xml 中与的区别与作用


1.2 配置Spring核心文件applicationContext.xml



	
	
		
		
		
		
		
		
			
				classpath:jdbc.properties
			
		
	
	
	
	

	
	
		
		
		
		
	


1.开启注解扫描
扫描cn.itcast.usermanage.service包以及其子包下的所有文件

2.数据源
此处采用了阿里的DruidDataSource,需要引入对应jar包

3.PropertyPlaceholderConfigurer
在Spring中,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码。

1.3 配置SpringMVC核心文件{servletName}-servlet.xml



	
	
	
	
	
	
	
	
	
	
		
		
	
	

1.配置注解驱动
加载映射器和适配器,以及json转换器

2.配置注解扫描
扫描cn.itcast.usermanage.controller包下文件

3.视图解析器
InternalResourceViewResolver是内部资源视图解析器,可以跳转到/WEB-INF下文件


2.Spring整合Mybatis

2.1 applicationContext-mybatis.xml

原始使用mybatis的步骤为

  • 编写mybatis-config.xml文件
  • 读取配置文件,构建sqlSessionFactory

Spring整合Springmvc和Mybatis_第1张图片

spring整合mybatis后,sqlSessionFactory就交给spring容器来管理了,



	
	
		
		
		
		
		
		
		
		
	

	
	
		
	


1.SqlSessionFactoryBean
用来生产sqlSessionFactory的工厂bean

2.dataSource
数据源,此处引入了spring容器中配置的数据源

3.configLocation
mybatis的全局配置文件

4.mapperLocations
我们可以在全局配置文件中,引入映射文件,当然也可以通过mapperLocations属性来引入

5.typeAliasesPackage
扫描包,包下的类名就是类的别名

6.MapperScannerConfigurer
MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring,之后可以在spring中通过类型来获取

传统:
1.在dao层中注入sqlSession,直接通过sqlSession来操作数据库
Spring整合Springmvc和Mybatis_第2张图片
Spring整合Springmvc和Mybatis_第3张图片
2.动态代理Mapper类
得先保证几点
(1).映射文件中命名空间与Mapper接口路径一致
(2).映射文件中statementId与Mapper接口的方法名保持一致
(3).映射文件中的statement的ResultType必须和mapper接口方法的返回类型一致
(4).映射文件中的statement的parameterType必须和mapper接口方法的参数类型一致
Spring整合Springmvc和Mybatis_第4张图片
传统方式需要创建,每次都要sqlSession.getMapper(xxxMapper.class)过于繁琐,既然sqlSessionFactory都交给Spring了,所以我们就使用MapperScannerConfigurer对接口进行扫描,将Mapper接口生成代理注入到Spring,之后通过类型来获取
在这里插入图片描述
可参考:Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring

2.2 applicationContext-mybatis.xml





	
		
		
	



3.Service层整合事务管理

1.之前我们是怎么进行事务管理的?
在之前的课程中,我们使用了spring自动的DriverManagerDataSource数据源,而此处我们使用阿里的DruidDataSource数据源

我们现在已经接触过4中数据源了

  • 1.Spring内置的数据源:DriverManagerDataSource
  • 2.Apache DBCP:org.apache.commons.dbcp.BasicDataSource
  • 3.C3P0 连接池:com.mchange.v2.c3p0.ComboPooledDataSource
  • 4.阿里DruidDataSource数据源

Spring整合Springmvc和Mybatis_第5张图片
直接通过jdbcTemplate操作数据库(原始)
Spring整合Springmvc和Mybatis_第6张图片


2.spring事务管理机制__声明式事务管理
配置事务管理


	
	
	
		
	

	
	
		
			
			
			
			
		
	

	
		
		
		
		
	
	

1.DataSourceTransactionManager(事务管理器)

事务 说明
org.springframework.jdbc.datasource.DataSourceTransactionManager 使用Spring JDBC或iBatis 进行持久化数据时使用
org.springframework.orm.hibernate5.HibernateTransactionManager 使用Hibernate5.0版本进行持久化数据时使用
org.springframework.orm.jpa.JpaTransactionManager 使用JPA进行持久化时使用
org.springframework.jdo.JdoTransactionManager 当持久化机制是Jdo时使用
org.springframework.transaction.jta.JtaTransactionManager 使用一个JTA实现来管理事务,在一个事务跨越多个资源时必须使用

DataSourceTransactionManager和HibernateTransactionManager的区别

  • DataSourceTransactionManager针对JdbcTemplate、MyBatis 事务控制,使用Connection(连接)进行事务控制 :
  • HibernateTransactionManager针对Hibernate框架进行事务管理,使用Session的Transaction相关操作进行事务控制 :

用户根据选择和使用的持久层技术,来选择对应的事务管理器,我们使用的是mybatis,所以选择DataSourceTransactionManager

2.transactionManager(事务策略)
可以 去Spring第四天细看

3.aop:config:面向切面编程
中定义切面和切入点

一般我们切面都是service层的类,因为在service层可能存在多个对数据库的操作,要保证一致性

你可能感兴趣的:(Spring,springmvc,mybatis)