ssm的几个配置文件(持续更新)

spring+springmvc+mybatis的配置文件,会持续更新,对应jar包已上传,截图见文末

这里需要的配置文件是3个:web.xml、springMVC.xml(springMVC的配置文件)、applicationContext.xml(spring的配置文件)

1. web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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_2_5.xsd">
  <display-name></display-name>	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- spring的配置文件,applicationContext.xml在src下,所以加上classpath,我的数据库配置也在这里-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <!-- spring mvc核心:分发servlet -->
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- spring mvc的配置文件 -->
        <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>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

2. springMVC.xml(springMVC的配置文件)

 

映射器handlerMaping可以根据url查找Handler,在这里不需要手动配置,springMVC可以默认启动:DefaultAnnotaHandlerMapping annotation-driven HandlerMapping

<mvc:annotation-driven/>:用于扩充朱姐驱动,可以将请求参数绑定到控制器参数

<mvc:resources mapping="resources/**" location="/resources/" />:用于静态资源 处理,比如js/css.imgs等资源的映射,如果无法访问这些资源可以从常识配置该项。

配置ViewResolver时可以使用多个viewresolver。可以对这些viewresolver使用order属性进行排序,但是需要注意的是InternalResourceViewResolver必须放在最后

<?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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
 
    <context:annotation-config/>   <!-- 激活了spring基于annotation的DI-->
 	
 	<!-- 自动扫描方式,扫描包下面所有的Controller -->
    <context:component-scan base-package="com.system.controller">
          <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
          <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
    </context:component-scan>
     
 	<mvc:annotation-driven>
 		 <!-- 消息转换器,解决ssm返回json乱码 -->
	    <mvc:message-converters register-defaults="true">
	      <bean class="org.springframework.http.converter.StringHttpMessageConverter">
	        <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
	      </bean>
	    </mvc:message-converters>
 	</mvc:annotation-driven>
     
    <mvc:default-servlet-handler />
 
    <!-- 配置视图解析器 -->  
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
       <!--  <property name="prefix" value="/pages/" />
        <property name="suffix" value=".jsp" /> -->
    </bean>
    
  <!-- 拦截器  -->
   <mvc:interceptors>  
	     <mvc:interceptor>   
	        <mvc:mapping path="/**"/>    
	        <mvc:exclude-mapping path="/login/login"/>
 			<mvc:exclude-mapping path="/dist/**"/>
	        <bean id="loginInterceptor" class="com.utils.LoginInterceptor"></bean>  
	     </mvc:interceptor>    
	</mvc:interceptors>
</beans>

3. applicationContext.xml(spring的配置文件)

数据库连接池使用的是阿里的Druid

<?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:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
     
   <context:annotation-config />
    <context:component-scan base-package="com.system.service" />
    
     <!-- 数据库连接池 -->  
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 基本属性 url、user、password, 注意&需要使用&amp; -->
        <property name="url" value="jdbc:mysql://192.168.1.100:3306/demo?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&amp;failOverReadOnly=false" />
        <property name="username" value="root" />
        <property name="password" value="XXXXX" />
       <!--  <property name="driverClassName" value="com.mysql.jdbc.Driver" /> -->
 
        <!-- 初始化连接大小 -->
		<property name="initialSize" value="1" />
		<!-- 连接池最大使用连接数量 -->
		<property name="maxActive" value="10" />
		<!-- 连接池最大空闲 -->
		<property name="maxIdle" value="10" />
		<!-- 连接池最小空闲 -->
		<property name="minIdle" value="1" />
		<!-- 获取连接最大等待时间 -->
		<property name="maxWait" value="60000" />

		<property name="validationQuery" value="select 1" />
		<property name="testOnBorrow" value="false" />
		<property name="testOnReturn" value="false" />
		<property name="testWhileIdle" value="true" />

		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="25200000" />

		<!-- 打开removeAbandoned功能 -->
		<property name="removeAbandoned" value="true" />
		<!-- 1800秒,也就是30分钟 -->
		<property name="removeAbandonedTimeout" value="1800" />
		<!-- 关闭abanded连接时输出错误日志 -->
		<property name="logAbandoned" value="true" />

		<!-- 监控数据库 -->
		<!-- <property name="filters" value="stat" /> -->
		<property name="filters" value="mergeStat" />
    </bean>
     
    <!-- myBatis文件 -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.system.pojo" />
        <property name="dataSource" ref="dataSource"/>
        <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
        <property name="mapperLocations" value="classpath:com/system/mapper/*.xml"/>
    </bean>

    <!-- myBatis文件 添加了pageHelp的配置
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="typeAliasesPackage" value="com.system.pojo" />
        <property name="dataSource" ref="dataSource"/>
        <property name="mapperLocations" value="classpath:com/system/mapper/*.xml"/>
        <property name="plugins">
		    <array>
		      <bean class="com.github.pagehelper.PageInterceptor">
				<property name="properties">
                    <value>
                          helperDialect=mysql
                          reasonable=true
                          supportMethodsArguments=true
                          autoRuntimeDialect=true
                    </value>
                </property>
		      </bean>
		    </array>
		  </property>
		  <property name="configLocation" value="classpath:mybatis-config.xml">   </property>
        </bean>
    -->
 
 	<!-- 配置mapper扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    	<!-- 扫描包路径,如果需要扫描多个包中间用半角逗号隔开 -->
        <property name="basePackage" value="com.system.mapper"/>
    </bean>

    
    <!-- 配置事务管理器 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

	<!-- 注解方式配置事物 -->
	<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->
 
	<!-- 拦截器方式配置事物 -->
	<!-- <tx:advice id="txadvice" transaction-manager="transactionManager"> 
        <tx:attributes> 
            <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" /> 
            <tx:method name="del*" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="edit*" propagation="REQUIRED" rollback-for="Exception" />
            <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/> 
            <tx:method name="list*" propagation="REQUIRED" rollback-for="Exception"/> 
        </tx:attributes> 
    </tx:advice> --> 
    
    <!-- 配置aop切面 -->    
    <!-- <aop:config> 
        <aop:pointcut id="serviceMethod" expression="execution(* com.ssmDemo.service.*.*(..))"/> 
        <aop:advisor pointcut-ref="serviceMethod" advice-ref="txadvice"/> 
    </aop:config>  --> 

    <!--配置文件上传使用解析器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>
    
</beans>

事务管理器推荐上面未注释的方式, 只需要在开启事务的serviceImpl类的方法上加上@Transaction注解即可

拦截器方式配置事务的话, 在类中不需要加额外注释, ssm会根据方法名自动判断是否开启, 但是这一方法需要开发者对方法名的命名有十分严格的标准, 一旦命名有误可能导致bug

ssm的大致处理如下:

前端控制器DispatcherServelet接收请求并响应结果

映射器HandlerMapping根据url查找对应的Handler

适配器HandlerAdapter按特定的规则执行Handler

处理器Handler运行

Viewresolver进行试图解析

 

jar包:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值