Spring框架技术实战③——SSM框架整合

目录

1、整合Spring和Mybatis框架

1、SSM框架整合介绍

1、SSM框架的系统结构

2、SSM框架整合步骤

2、为Mybatis配置数据源对象

1、在Spring容器中配置dbcp和c3p0数据源

2、在Web容器中配置数据源

3、配置SqlSessionFactory对象

4、配置MapperScannerConfigurer

5、实现并配置Dao和Service

1、实现Dao

2、实现Service

3、在Spring中配置Dao和Service

2、使用Spring AOP 实现声明式事务管理

1、基于XML配置的声明式事务

2、使用annotation配置声明式事务

3、整合Spring 和 Struts2框架

1、通过Listener加载Spring容器

2、使用Spring管理Action


1、整合Spring和Mybatis框架

1、SSM框架整合介绍

1、SSM框架的系统结构

2、SSM框架整合步骤

2、为Mybatis配置数据源对象

1、在Spring容器中配置dbcp和c3p0数据源

2、在Web容器中配置数据源

3、配置SqlSessionFactory对象

	<!-- 配置sqlsessionfactory -->
	<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 把连接池交给该对象 -->
		<!-- 此处的ref值(dataSouser)为上述配置的id为dataSouser的bean -->
		<property name="dataSource" ref="dataSouser"></property>
		<!-- 设置mybatis主配置文件 -->
		<property name="configLocation" value="classpath:mybatis.xml"></property>
	</bean>
	<!-- 引入外部资源文件 -->
	<context:property-placeholder location="classpath:jdbc.properties" />

	<!-- 配置数据源(连接池) -->
	<bean id="dataSouser" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 用户名 -->
		<property name="user" value="${jdbc.username}"></property>
		<!-- 密码 -->
		<property name="password" value="${jdbc.password}"></property>
		<!-- 驱动地址 -->
		<property name="driverClass" value="${jdbc.driver}"></property>
		<!-- 数据库连接地址 -->
		<property name="jdbcUrl" value="${jdbc.url}"></property>
	</bean>

4、配置MapperScannerConfigurer

	<!-- 让spring自动帮我们生成dao层接口实现类,并且放到IOC容器中 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 此处的value值(sessionFactory)为上述配置的id为sessionFactory的bean -->
		<property name="sqlSessionFactoryBeanName" value="sessionFactory"></property>
		<!-- 指定接口所在的位置 -->
		<property name="basePackage" value="com.zl.dao"></property>
	</bean>

5、实现并配置Dao和Service

1、实现Dao

2、实现Service

3、在Spring中配置Dao和Service

2、使用Spring AOP 实现声明式事务管理

声明式事务处理,可以通过XML配置实现,也可以通过annotation实现,实际开发中,前者使用较多 。

1、基于XML配置的声明式事务

实现的基本步骤如下:

 配置了事务管理类,还要为该类配置事务的属性,配置的代码如下:

	<!-- 配置事务管理切面对象:事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSouser"></property>
	</bean>

	<!-- 配置事物属性 -->
	<tx:advice id="myAdvice1" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 给不同的方法配置不同的事物属性 -->
			<!-- <tx:method name="add*" isolation="SERIALIZABLE" propagation="REQUIRED"/> -->
			<tx:method name="query*" read-only="true" />

			<!-- 不满足以上配置的方法采用下面事物属性:必须放置在最后面 -->
			<tx:method name="*" /><!-- 其他的方法均采用默认的属性处理事务 -->
		</tx:attributes>
	</tx:advice>

	<!-- 通过AOP的技术指定那些方法需要添加事务 -->
	<aop:config>
		<!-- 切点表达式 -->
		<aop:pointcut expression="execution(* com.zl.service..*.*(..))"
			id="pointcut1" />
		<!-- 把事务属性和切点表达式结合 -->
		<aop:advisor advice-ref="myAdvice1" pointcut-ref="pointcut1" />
	</aop:config>

2、使用annotation配置声明式事务

	<!-- 开启spring事务管理注解开发功能 -->
	<tx:annotation-driven transaction-manager="transactionManager" />

	<!-- 配置事务管理切面对象:事务管理器 --><!-- 开启spring事务管理注解开发功能 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSouser"></property>
	</bean>
@Service
public class BrankServiceImpl implements BrankService {
	@Autowired
	private BankDao bd;

	@Override
	@Transactional
	public int quqian() {
		return bd.quqian();
	}

	@Override
	@Transactional
	public int cunqian() {
		int flag = bd.cunqian();
		return flag;
	}
}

3、整合Spring 和 Struts2框架

1、通过Listener加载Spring容器

2、使用Spring管理Action

 

 

 

 

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值