整合Hibernate-下(Lee-Day12-09)

泛型的public class BaseDao<T> 里面放一些通用的CRUD操作

业务Dao(接口)里面放与业务相关的查询方法,让业务Dao继承BaseDao<Entity_Class>

让BaseDao实现类实现BaseDao接口(里面放一些门面方法,门面方法用protected修饰,只能给子类使用,不继承不可以用,这种封装更好)

让业务类的实现(DaoImpl)继承BaseDaoImpl<Entity_Class> implements 业务Dao,这样业务类就干净很多,通用的CRUD都放在了BaseDaoImpl中

接下来在Spring的配置文件中添加业务Dao的bean

<bean id="userDao" class="com.ssh.dao.impl.UserDaoImpl"
		p:sessionFactory-ref="sessionFactory"	  
	/>

Dao又依赖Service,所以把Service的实现类交由Spring管理同时如果业务实现类依赖Dao组件,要把ServiceImpl类依赖的Dao也注入进去,多个Spring配置文件最终都要融合到一个容器里,所以不在同一个xml文件也可以直接使用xxx-ref引用对应的bean

<bean id="userService" class="com.ssh.service.impl.UserServiceImpl"
		p:userDao-ref="userDao"
/>

接下来配置事务

-------------------------------------------以下为Spring的事务配置----------------------------------------------------------

添加标签

<beans
                xmlns:aop="http://www.springframework.org/schema/aop"
		xmlns:tx="http://www.springframework.org/schema/tx"
                http://www.springframework.org/schema/aop
	        http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/tx 
	        http://www.springframework.org/schema/tx/spring-tx.xsd>

在Spring的xml文件里配置事务:

<bean id="transactionManager"
		  class="org.springframework.orm.hibernate4.HibernateTransactionManager"
		  p:sessionFactory-ref="sessionFactory" />
	<tx:advice id="txAdvice">
		<tx:attributes>
			<tx:method name="get*" read-only="true" timeout="8"/>
			<tx:method name="*" timeout="5"/>
		</tx:attributes>
	</tx:advice>	
	<aop:config>
		<aop:pointcut expression="execution(* com.ssh.service.impl.*.*(..))" id="csmPc"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="csmPc"/>
	</aop:config>

导aop的jar包

1.aopalliance-1.0.jar

2.aspectj1.8(四个jar包)

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值