SSM整合配置文件进阶版

基于构建一个简单SSM框架的流程整理,将SpringMVC和MyBatise配置文件和Spring核心配置文件整合优化。其中项目创建以及基础设置和构建一个简单SSM框架的流程中一样。

搭建SpringMVC配置文件

web.xml 配置文件中需要包括以下几个组件

  • CharacterEncodingFilter:字符过滤器
  • HiddenHttpMethodFilter:RESTful方法过滤器
  • DispatcherServlet:SpringMVC核心控制器
  • ContextLoaderListener:Spring容器加载的监听器
  • context-param:自定义Spring核心配置文件的名称和路径
<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SSM</display-name>
  
  <!-- 配置字符过滤器 -->
  <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>
  
  <!-- 配置RESTful方法过滤器 -->
  <filter>
  	<filter-name>HiddenHttpMethodFilter</filter-name>
  	<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>HiddenHttpMethodFilter</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 配置Spring 加载的监听器  -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 自定义spring配置文件的位置和名称 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:spring.xml</param-value>
  </context-param>
  
  <!-- 配置springMVC核心控制器  -->
  <servlet>
  	<servlet-name>DispatcherServlet</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- 指定springMVC 配置文件的位置 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:springMVC.xml</param-value>
  	</init-param>
  	<!-- 配置加载时间 -->
  	<load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  	<servlet-name>DispatcherServlet</servlet-name>
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

springMVC.xml 配置文件中需要包括以下几个组件

  • 扫描控制层组件
  • 视图解析器
  • DefaultServlet:<mvc:default-servlet-handler/>

    此配置的作用是:优雅 REST 风格的 URL 不希望带 .html 或 .do 等后缀,而如果将 DispatcherServlet 请求映射配置为"/",则Spring MVC将捕获Web容器所有的请求,静态资源的请求找不到对应处理器将导致错误。在xml文件中配置<mvc:default-servlet-handler/>,Spring MVC上下文中定义一个org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler,对进入 DispatcherServlet 的 URL 进行筛查,静态资源由 Web 服务器默认的Servlet 处理,非静态资源的请求,由 DispatcherServlet 处理。

  • MVC驱动
  • MultipartResource、拦截器(可选)
<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	<!-- 配置扫描控制层组件 -->
	<context:component-scan base-package="cn.cz.ssm.controller"></context:component-scan>
	
	<!-- 配置扫视图解析器 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/view/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 配置DefualtServlet -->
	<mvc:default-servlet-handler/>
	
	<!-- 配置springMVC 驱动 -->
	<mvc:annotation-driven />
	
</beans>

搭建MyBatis核心配置文件

mybatis.xml 配置文件中需要包括以下几个组件

  • 加载数据库配置文件dataProperties.properties(此操作可转移到spring.xml配置文件中)
  • MyBatis核心设置settings
  • 起别名(此操作可转移到spring.xml配置文件中)
  • 配置插件
  • 连接数据库(数据源)(此操作可转移到spring.xml配置文件中)
  • Mapper接口映射(此操作可转移到spring.xml配置文件中)
<?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>

 	<!-- 核心设置 -->
 	<settings>
 		<!-- 将下划线映射成驼峰,user_name映射为userName -->
		<setting name="mapUnderscoreToCamelCase" value="true"/>
		<!-- 开启延迟加载 -->
		<setting name="lazyLoadingEnabled" value="true"/>
		<!-- 是否查询所有数据 -->
		<setting name="aggressiveLazyLoading" value="false"/>
		<!-- 是否开启二级缓存 -->
		<setting name="cacheEnabled" value="true"/>
 	</settings>
 	
 	<!-- 配置分页插件 -->
 	<plugins>
		<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
	</plugins>
 	
 </configuration>

Spring核心配置文件

spring.xml 配置文件中需要包括以下几个组件

  • 扫描组件(排除控制层组件)
  • 加载数据库配置文件dataProperties.properties
  • DataSource 数据源
  • 事务管理器
  • 开启事务驱动
  • SqlSessionFactoryBean:mybatis操作数据库的会话对象SqlSession
  • MapperScannerConfigurer:通过反射获得Mapper接口的实现
<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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">

	<!-- 配置扫描组件 -->
	<context:component-scan base-package="cn.cz.ssm">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	
	<!-- 数据源 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="driverClassName" value="${jdbc.driver}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	
	<!-- 声明事务管理器 -->
	<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>		
	</bean>
	
	<!-- 开启事务注解驱动 -->
	<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
	
	<!-- 管理mybatis操作数据库的会话对象SqlSession -->
	<bean class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 设置 mybatis 核心配置文件路径 -->
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
		
		<!-- 设置数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		
		<!-- 设置类型名(起别名) -->
		<property name="typeAliasesPackage" value="cn.cz.ssm.bean"></property>
		
		<!-- 设置映射文件路径(注意写法) -->
		<property name="mapperLocations" value="classpath:cn/cz/ssm/mapper/*.xml"></property>
	</bean>
	
	<!-- 在所设置的包下,将所有的接口生成动态代理实现类,并由spring容器管理 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.cz.ssm.mapper"></property>
	</bean>

</beans>

至此,配置文件整合基本完成。

原文地址:https://www.cnblogs.com/chaozhengtx/p/14276126.html