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>

你可能感兴趣的:(java,DAO,spring,Hibernate,ssh)