<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- 建立数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<!-- 数据库驱动 -->
<property name="driverClassName"
value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
</property>
<!-- 数据库连接地址 -->
<property name="url"
value="jdbc:microsoft:sqlserver://localhost:1433">
</property>
<!-- 数据库的用户名和密码 -->
<property name="username" value="sa"></property>
<property name="password" value="sa"></property>
</bean>
<!-- 把数据源注入给SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<!-- 配置数据库方言 -->
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- 配置映射文件 -->
<property name="mappingResources">
<list>
<value>com/test/pojo/TDepartment.hbm.xml</value>
<value>com/test/pojo/TEmployee.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置事务管理器bean,使用HibernateTransactionManager事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- 为事务管理器注入SessionFactory -->
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事务代理,注入事务管理transactionManager,由Spring来代理事务,设置事务属性 -->
<bean id="transactionProxy" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="remove*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="delete*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="update*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="create*">
PROPAGATION_REQUIRED,-Exception
</prop>
<prop key="query*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- DAO的注入配置与引用 -->
<bean id="employeeDao" class="com.test.dao.Impl.EmpDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="departmentDao" class="com.test.dao.Impl.DepDao">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<!-- Service的注入配置与引用 -->
<bean id="employTempService"
class="com.test.service.Impl.TEmployeeServiceImpl">
<property name="itemployee">
<ref bean="employeeDao" />
</property>
</bean>
<bean id="departTempService"
class="com.test.service.Impl.TDepartmentServiceImpl">
<property name="idepartment">
<ref bean="departmentDao" />
</property>
</bean>
<!-- 将Service注入给事务 -->
<bean id="employService" parent="transactionProxy">
<property name="target">
<ref bean="employTempService" />
</property>
</bean>
<bean id="departService" parent="transactionProxy">
<property name="target">
<ref bean="departTempService" />
</property>
</bean>
<!-- Action的注入配置与引用 -->
<bean name="/emp" class="com.test.action.EmpAction"
abstract="false">
<property name="employeeService">
<ref bean="employService" />
</property>
<property name="departmentService">
<ref bean="departService" />
</property>
</bean>
</beans>
本文介绍了一个使用Spring框架整合Hibernate进行数据库操作的例子。通过配置数据源、SessionFactory、事务管理器等组件,实现了DAO层、Service层及Action层的具体业务逻辑。
7119

被折叠的 条评论
为什么被折叠?



