SSM整合中所需配置

因为博主初学ssm框架,创建xml文件配置有很多,记不清楚,所以在此做以下记录。

一、SSM

ssm是Spring+SpringMVC+Mybatis的框架整合。

这里我会将SpringSpringMVC的配置分别放applicationContext-root.xmlapplicationContext-mvc.xml在两个文件中。

两个文件中所需要配置的主要信息说明:
1.applicationContext-root.xml:

主要配置除了Controller以外的组件,如:数据持久层逻辑业务层POJO实体等,其中也包括dataSource数据源Mybatis的sqlSession事务管理器映射Mapper目录
 

2.applicationContext-mvc.xml:

主要配置Controller映射器适配器视图解析器

       

二、Spring的主要配置文件applicationContext-root.xml

1.xml根节点的属性:

xmlns:xsi ——是指xml文件遵守xml规范,xsi全名:xml schema instance,是指具体用到的schema资源文件里定义的元素所准守的规范。即/spring-beans-2.0.xsd这个文件里定义的元素遵守什么标准。

xsi:schemaLocation——是指,本文档里的xml元素所遵守的规范,schemaLocation 属性用来引用(schema)模式文档,解析器可以在需要的情况下使用这个文档对 XML 实例文档进行校验。它的值(URI)是成对出现的,第一个值表示命名空间,第二个值则表示描述该命名空间的模式文档的具体位置,两个值之间以空格分隔。
(参照)XML中<beans>属性 - hy叶子 - 博客园

<?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"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:aop="http://www.springframework.org/schema/aop" 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.3.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.1.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

2.(注解配置方式)配置自动扫包


<context:component-scan>标签的属性base-package:表示扫描该路径下的所有子路径的类中的注解。


<context:exclude-filter type="annotation"expression="org.springframework.stereotype.Controller">:标签表示除了类型为type(annotation注解)的express(注解名称为Controller)的类。在这里表示除了@Controller的不扫。(不扫的话注解为@Controller的类的实例就不会存储在applicationContext-root.xml容器中)

<context:component-scan base-package="com.oracle">
		<!-- 在扫描的时候 不包含控制层注解 -->
		<context:exclude-filter type="annotation"         
           expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

2.配置数据源(下面使用的是spel表达式写入 的形式来配置c3p0数据源)

第一步:

<!-- 在Spring启动时加载配置文件 -->
<context:property-placeholder location="classpath*:db.properties" 
        ignore-resource-not-found="true" ignore-unresolvable="true"/>

<context:property-placeholder>标签是用来加载location路径下的文件内容,文件是以java中常见的键值对的形式存储。

location="classpath:db.properties"(classpath就算src根路径下的文件)

ignore-resource-not-found="true"

ignore-unresolvable="true"

这两个属性在不设置的时候,默认为false,此时当加载文件的时候,如果找不到文件的话就会忽略,不会在控制台上报错。注意的是,ignore-resource-not-found设置为true的时候,ignore-unresolvable最好为true。
注意下面的代码是存储在db.properties文件中。

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/uno1?useUnicode\=true&characterEncoding\=UTF-8
jdbc.username=root
jdbc.password=root
connection_pools.min_pool_size=5
connection_pools.acquire_increment=5
connection_pools.max_pool_size=100
connection_pools.initial_pool_size=5
connection_pools.max_idle_time=600
connection_pools.checkout_timeout=60000

第二步:
下面的值使用到了spel表达式来传值。

<!-- 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<!-- spel表达式写入 -->
	<property name="driverClass" value="${jdbc.driver}"/>
	<property name="jdbcUrl" value="${jdbc.url}"/>
	<property name="user" value="${jdbc.username}"/>
	<property name="password" value="${jdbc.password}"/>
	<property name="initialPoolSize" value="${connection_pools.initial_pool_size}"/>
	<property name="minPoolSize" value="${connection_pools.min_pool_size}"/>
	<property name="maxPoolSize" value="${connection_pools.max_pool_size}"/>
	<property name="maxIdleTime" value="${connection_pools.max_idle_time}"/>
	<property name="acquireIncrement" value="${connection_pools.acquire_increment}"/>
	<property name="checkoutTimeout" value="${connection_pools.checkout_timeout}"/>
</bean>

3.配置会话工厂(SqlSessionFactory)(Spring和Mybatis整合的部分)

注意:会话工厂中注入数据源的值与上面的2.步骤中的id一直

因为你要开启会话的需要数据源。

<!-- 配置Mybatis的sqlSession -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 
        下面注释的标签是加载mybatis.xml文件配置
        因为,要进行ssm整合,此时mybatis.xml中就不能配置mapper、dataSource等组件,
        但是可以配置一些插件:如分页插件。
     -->
	<!-- <property name="configLocation" value="classpath:mybatis.xml"/> -->

	<property name="dataSource" ref="dataSource"/>
    
    <!-- 映射mapper文件(mapper.xml文件) -->
    <property name="mapperLocations" value="com.mapper.*Mapper.xml"></property>
</bean>
在pom.xml文件下需要加入以下代码
<resources>
  <resource>
    <directory>src/main/java</directory>
    <includes>
      <include>**/*.xml</include>
    </includes>
  </resource>
</resources>

4.配置事务管理器

<!-- 事务管理器 -->
<bean id="transactionManager"
	   class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"></property>
</bean>

下面是设置使用注解的方式配置自动提交事务

<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />

5.配置Mapper目录

<!-- 映射Mapper目录 -->
<!-- Mapper接口所在包名,Spring会自动查找其下的Mapper -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	 <!-- 会将你的mapper接口进行自动注册  成为Spring组件进行管理 在使用时进行依赖注入 -->
	 <property name="basePackage" value="com.oracle.mapper"/>
</bean>

三、SpringMVC的主要配置文件applicationContext-mvc.xml

1.配置自动扫包 

<!-- 开启注解扫描 -->
<context:component-scan base-package="com.oracle" use-default-filters="false">
	<context:include-filter type="annotation" 
		    expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

2.静态资源配置

当/WEB-INF/目录下的jsp文件中,需要加载静态资源的的时候,SpringMVC就会对URL路径进行解析,在/resource/**下的所有资源,会被前端控制器放行。
所以如image、css、js等资源都可放在resource目录下(注意这里的resource于/src/resource不同)

<!-- 在webapp/WebContent下创建一个目录名为resource的文件夹 -->
<mvc:resources location="/resource/" mapping="/resource/**"></mvc:resources>

3.配置文件上传组件

<!-- 文件上传   组件   -->
<bean id="commonsMultipartResolver"         
       class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    <property name="defaultEncoding" value="utf-8"/>
	<property name="maxUploadSize" value="1500000000000"/>
</bean>

4.视图解析器

导航栏上只需输入视图名称即可,必须要在输入.jsp了
在/WEB-INF/目录下放,更加安全,此时客户端访问该目录下的资源就,需要通过向前端控制器发送请求的方式,由前端控制器,分发请求,最后由适配器返回ModelAndView,才响应给客户端。

<!-- 视图解析器
		视图名称即可 不需要在指定 webinfo 和  .jsp
	 -->
<bean id="viewResolver"             
         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="/WEB-INF/"></property>
	<property name="suffix" value=".jsp"></property>
</bean>

5.配置编码,默认映射器和适配器。

<!-- 开启mvc注解 -->
<mvc:annotation-driven>
	 <mvc:message-converters>
		<bean class="org.springframework.http.converter.StringHttpMessageConverter">
			<property name="supportedMediaTypes">
				<list>
					<value>text/html;charset=UTF-8</value>
			    </list>
			</property>
		</bean>	 	
	 </mvc:message-converters>
</mvc:annotation-driven>

四、web.xml的配置

1.配置上下文路径

注意:在Maven中和在WebProject中配置web.xml是有区别的。

在Maven中下面配置是必须加的,如果不加,会报以下错误:

Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

博主就遇到这样的错误,参照下面大佬的文章解决。

关于“Could not open ServletContext resource [/WEB-INF/applicationContext.xml]”解决方案_小灯光环-CSDN博客

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext-root.xml</param-value>
</context-param>

2.配置应用监听器

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

3.配置过滤器

专门用来处理编码

<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>
  <init-param>
  	<param-name>forceEncoding</param-name>
  	<param-value>true</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>characterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

4.配置前端控制器

注意url中/和/*是不同的。

< url-pattern > / </ url-pattern >   :

        不会匹配到*.jsp,即:*.jsp不会进入spring的 DispatcherServlet类 。

< url-pattern > /* </ url-pattern > :

        会匹配*.jsp,会出现返回jsp视图时再次进入spring的DispatcherServlet 类,导致找不到对应的controller所以报404错。 

参考:https://blog.csdn.net/qq_38296051/article/details/84975174

<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>
  <url-pattern>/</url-pattern>
</servlet-mapping>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值