ssm整合配置文件

  1. 导入jar包
  2. 编写配置文件
  3. 编写代码

 


applicationContext-dao.xml---配置数据源、SqlSessionFactory、mapper扫描器

applicationContext-service.xml---配置service接口

applicationContext-transaction.xml--事务管理

sprintmvc.xml---springmvc的配置,配置处理器映射器、适配器、视图解析器

SqlMapConfig.xml---mybatis的配置文件,配置别名、settings、mapper

1 编写SqlMapConfig.xml---mybatis文件

<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

	<!-- 定义 别名 -->
	<typeAliases>
		<!-- 批量别名定义 指定包路径,自动扫描包下边的pojo,定义别名,别名默认为类名(首字母小写或大写) -->
		<package name="实体类包路径" />

	</typeAliases>



</configuration>

2 applicationContext-dao.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 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.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 加载配置文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 数据库连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10" />
		<property name="maxIdle" value="5" />
	</bean>


	<!-- SqlsessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource"/>
		<!-- mybatis配置文件 -->
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
	</bean>
	
	
	<!-- 
	MapperScannerConfigurer:mapper的扫描器,将包下边的mapper接口自动创建代理对象,
	自动创建到spring容器中,bean的id是mapper的类名(首字母小写)
	 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 配置扫描包的路径
		如果要扫描多个包,中间使用半角逗号分隔
		要求mapper.xml和mapper.java同名且在同一个目录 
		 -->
		<property name="basePackage" value="cn.itcast.ssm.mapper"/>
		<!-- 使用sqlSessionFactoryBeanName -->
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
		
		
	</bean>
	
</beans>

3 applicationContext-service.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 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.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!-- 商品管理 的service -->
<bean id="itemsService" class="service实现类"/>
	
	
</beans>

4 applicationContext-transaction.xml--事务管理

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 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.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">


	 <!-- 使用声明式事务配置,可以有效规范代码 -->
	 
	 <!-- 事务管理器 -->
	 <bean id="transactionManager" 
	 		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	 	<property name="dataSource" ref="dataSource"/>
	 </bean>
	 
	 <!-- 通知 -->
	 <tx:advice id="txAdvice" transaction-manager="transactionManager">
	 	<tx:attributes>
	 		<tx:method name="save*" propagation="REQUIRED"/>
	 		<tx:method name="insert*" propagation="REQUIRED"/>
	 		<tx:method name="update*" propagation="REQUIRED"/>
	 		<tx:method name="delete*" propagation="REQUIRED"/>
	 		<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
	 		<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
	 		<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
	 	</tx:attributes>
	 </tx:advice>
	 
	 <!-- aop -->
	 <aop:config>
	 	<aop:advisor advice-ref="txAdvice"
	 				 pointcut="execution(* cn.it.ssm.service.impl.*.*(..))"/>
	 </aop:config>
	
</beans>

5 sprintmvc.xml---springmvc的配置,配置处理器映射器、适配器、视图解析器

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 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.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 使用spring组件扫描 -->
	<context:component-scan base-package="cn.it.ssm.controller" />

	<!-- 通过annotation-driven可以替代下边的处理器映射器和适配器 -->
<!-- 	<mvc:annotation-driven conversion-service="conversionService">
</mvc:annotation-driven>
	-->
	
	<!-- 注解处理器映射器 -->
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
	</bean>


	<!-- 注解适配器 -->
	<bean
		class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
		<!-- 在webBindingInitializer中注入自定义属性编辑器、自定义转换器 -->
		<property name="webBindingInitializer" ref="customBinder"></property>
	</bean>

	<!-- 配置视图解析器 要求将jstl的包加到classpath -->
	<!-- ViewResolver -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>

	
	<!-- 自定义webBinder -->
	<bean id="customBinder"
		class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
		<!-- 使用converter进行参数转 -->
		<property name="conversionService" ref="conversionService" />
		
		<!-- propertyEditorRegistrars用于属性编辑器 -->
		<!-- <property name="propertyEditorRegistrars">
			<list>
				<ref bean="customPropertyEditor" />
			</list>
		</property> -->
	</bean>
	
	<!-- 注册属性编辑器 -->
	<bean id="customPropertyEditor" class="cn.it.ssm.controller.propertyeditor.CustomPropertyEditor"></bean>
	
	<!-- 转换器 -->
    <bean id="conversionService"
		class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
		<property name="converters">
			<list>
				<bean class="cn.it.ssm.controller.converter.CustomDateConverter"/>
				<bean class="cn.it.ssm.controller.converter.StringTrimConverter"/>
			</list>
		</property>
	</bean> 


</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值