ssm-环境搭建

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>myssmTest</display-name>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext-*.xml</param-value>
  </context-param>
  
  
  <!-- springMVC前端控制器 -->
    <!-- springmvc前端控制器 、配置处理器、映射器、适配器等等-->
  <servlet>
  	<servlet-name>springmvc</servlet-name>
  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  	<!-- contextConfigLocation配置springmvc加载配置文件 -->
  	<init-param>
  		<param-name>contextConfigLocation</param-name>
  		<param-value>classpath:spring/springmvc.xml</param-value>
  	</init-param>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>springmvc</servlet-name>
  	<!-- 第一种:*.action,访问以.action结尾由DispatcherServlet进行解析
  	第二种:/,所有访问的地址都由DispatcherServlet进行解析,对于金泰文件的解析需要配置不让DispatcherServlet进行解析
  		使用这种方法可以实现RESTful风格的url
  	第三种:/*,这样配置不对,使用这种配置,最终要转发到一个jsp页面是,
  		任然会有DispatcherServlet解析jsp,不能根据jsp页面找到handler,会报错
  	 -->
  	<url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 配置spring提供的字符过滤器 -->
  <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>
</web-app>

springmvc.xml

<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/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd  ">
<!-- 注解扫描 -->
 <context:component-scan base-package="com.itcast.ssm.controller"/>  
<!-- 注解映射器 
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
 注解适配器 
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/>
-->
<!-- conversion-service="conversionService"加载自定义参数绑定 -->
<!-- validator:加载校验器 -->
<mvc:annotation-driven validator="validator"/>


<!-- 视图解析器 
解析jsp视图,默认使用jstl标签,classpath下得有jstl的包
-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<!-- 配置jsp路径的前缀 -->
	<property name="prefix" value="/WEB-INF/jsp/"/>
	<!-- 配置jsp路径的后缀 -->
	<property name="suffix" value=".jsp"/>
</bean>

<!-- 自定义参数绑定 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
	<!-- 转换器 -->
	<property name="converters">
		<list>
			<!-- 日期类型转换 -->
			<bean class="com.itcast.ssm.controller.converter.CustomDateConverter"/>
		</list>
	</property>
</bean>

<!-- 校验器 (spring提供的接口)-->
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
	<!-- 校验器提供方 -->
	<property name="providerClass" value="org.hibernate.validator.HibernateValidator"></property>
	<!-- 指定校验使用的资源文件,在文件中配置校验错误信息,如果不注定默认使用classpath下的ValidationMessage.properties -->
	<property name="validationMessageSource" ref="messageSource"/>
</bean>
<!-- 检验错误信息配置文件 -->
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
	<!-- 资源文件名 -->
	<property name="basenames">
		<list>
			<value>classpath:CustomValidationMessages</value>
		</list>
	</property>
	<!-- 资源文件编码格式 -->
	<property name="fileEncodings" value="utf-8"/>
	<!-- 对资源文件内容缓存时间,单位秒 -->
	<property name="cacheSeconds" value="120"/>
</bean>

<!-- 全局异常处理器
只要实现HandlerExceptionResolver接口就是全局异常处理器
有且仅有一个
 -->
<bean class="com.itcast.ssm.exception.CustomExceptionResolver"/>

<!-- 文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
	<property name="maxUploadSize">
		<value>5242880</value>
	</property>
</bean>
<!-- 拦截器 
针对HandleMapping,可单个配置
-->
	<mvc:interceptors>
	<!-- 多个拦截器,顺序执行 -->
		<mvc:interceptor>
			<!-- /**表示所有url包括子url路径 -->
			<mvc:mapping path="/**"/>
			<bean class="com.itcast.ssm.interceptor.HandlerInterceptor1"/>
		</mvc:interceptor>
				<mvc:interceptor>
			<!-- /**表示所有url包括子url路径 -->
			<mvc:mapping path="/**"/>
			<bean class="com.itcast.ssm.interceptor.HandlerInterceptor2"/>
		</mvc:interceptor>

</mvc:interceptors>


</beans>

SqlMapConfig.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>

	<mappers>

	</mappers>
</configuration>

applicationContext-dao.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:context="http://www.springframework.org/schema/context"
		xmlns:tx="http://www.springframework.org/schema/tx"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
	
	<!-- 加载db.properties文件中的内容 -->
	<context:property-placeholder location="classpath:db.properties"/>	
	
	
<!-- 配置数据源,dbcp -->	
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=stdb1"></property>
		<property name="username" value="sa"></property>
		<property name="password" value="hyj84884824"></property>
		<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
	</bean>


<!-- sqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 加载mybatis的配置文件 -->
		<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"/>
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource"/>
	</bean>

<!-- mapper扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 指定扫描的包名 -->
		<property name="basePackage" value="com.itcast.ssm.mapper"/>
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
	</bean>

</beans>
applicationContext-service.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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-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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">	
	<!-- 事务管理器
	对mybatis操作数据库事务控制,spring使用jdbc的事务控制类
	
	 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	
	<!-- 数据源
	dataSource在applicationContext-dao.xml中配置了
	 -->
	<property name="dataSource" ref="dataSource"/>
</bean>
	<!-- 通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 传播行为 -->
			<tx:method name="save*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED"/>
			<tx:method name="insert*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
			<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
			<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- aop -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itcast.ssm.service.impl.*.*(..))"/>
	</aop:config>
	
</beans>
applicationContext-transaction.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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
	
<!-- 商品管理的service -->

<bean id="itemsService" class="com.itcast.ssm.service.impl.ItemsServiceImpl"></bean>

<!--  -->
</beans>





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值