<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!--配置实现接口的bean-->
<bean id="employeeImpl" class="com.hsp.imp.EmployeeImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!--配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<property name="username" value="root"/>
<property name="password" value="xiongliang"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="30"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 连接池中释放连接所剩余的最大数 -->
<property name="maxIdle" value="2"/>
<!--当连接池空闲连接达到1时,会预先申请连接。 -->
<property name="minIdle" value="1"/>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mappingResources">
<list>
<value>com/hsp/domain/Employee.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
</value>
</property>
</bean>
<!-- 配置事务管理器,统一管理sessionFactory的事务。class不要写错了,name是固定的。 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 启动事务注解 -->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
employeeImpl类:
package com.hsp.imp;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.transaction.annotation.Transactional;
import com.hsp.domain.Employee;
import com.hsp.inter.EmployeeInter;
//将事务交给spring来管理
@Transactional
public class EmployeeImpl implements EmployeeInter {
//通过配置注入sessionFactory,也可以使用注解注入sessionFactory
private SessionFactory sessionFactory;
public EmployeeImpl() {
}
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
//业务逻辑方法
public void addEmploy(Employee employee) {
Session s=sessionFactory.openSession();
s.save(employee);
}
}
测试类App:
public class App {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
EmployeeInter employeeInter=(EmployeeInter) ac.getBean("employeeImpl");
Employee e=new Employee("张三","123",new java.util.Date(),220f);
employeeInter.addEmploy(e); //自己开启事务了啊。
}
}
对应数据库的结果:
自此hibernate+spring 的整合已经完成,下面整合struts2+hibernate+spring.