ssh学习(二)spring-hibernate整合

1.配置数据源
	<bean id="myDataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/prefo">
</property>
<property name="username" value="root"></property>
<property name="password" value=""></property>
</bean>

2.配置sessionfactory
	<!-- (2)装配SessionFactory -->
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 传入dataSource -->
<property name="dataSource">
<ref bean="myDataSource" />
</property>
<!-- 添加映射文件 -->
<property name="mappingResources">
<list>
<value>
cn/com/jacken/prefo/books/vo/CatelogList.hbm.xml
</value>
<value>
cn/com/jacken/prefo/books/vo/BooksList.hbm.xml
</value>
<value>
cn/com/jacken/prefo/users/vo/UserList.hbm.xml
</value>
</list>
</property>
<!--hibernate属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

如果不需要事务,直接就可以配置Dao 且调用了(dao 要继承HibernateDaoSupport)
	<!-- 装配catelogDao -->
<bean id="catelogDao"
class="cn.com.jacken.prefo.books.dao.CatelogDaoHibernateImpl">
<!-- 传入sessionFactory -->
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>

3.配置事务
(1)配置Dao (dao 要继承HibernateDaoSupport)
	<!-- 装配catelogDao -->
<bean id="catelogDao"
class="cn.com.jacken.prefo.books.dao.CatelogDaoHibernateImpl">
<!-- 传入sessionFactory -->
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>

(2)装配事务管理器
	<!-- (3)装配事务管理器 -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 传入session -->
<property name="sessionFactory">
<ref bean="mySessionFactory" />
</property>
</bean>

(3)配置抽象事务代理
	<!-- (4)抽象事务代理 -->
<bean id="abstractTxProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<!-- 传入事务管理器 -->
<property name="transactionManager">
<ref bean="myTransactionManager" />
</property>
<property name="transactionAttributes">
<props>
<!-- key 指定方法 -->
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

(4)装配Service 层
	<!-- 装配CatelogService -->
<bean id="catelogServiceTarget"
class="cn.com.jacken.prefo.books.services.CatelogServiceSpringImpl">
<!-- 传入dao -->
<property name="catelogDao">
<ref bean="catelogDao" />
</property>
<property name="booksDao">
<ref bean="booksDao" />
</property>
</bean>

(5)装配具体事务代理
	<!-- 具体代理事务代理--CatelogService事务 -->
<bean id="catelogService" parent="abstractTxProxy">
<!-- 传入CatelogService -->
<property name="target">
<ref bean="catelogServiceTarget" />
</property>
</bean>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值