第一步:配置文件
<?xml version="1.0" encoding="UTF-8"?>
<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:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- <context:property-placeholder location="classpath:jdbc.properties"/> -->
<context:annotation-config/>
<!-- 配置dataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@192.168.1.10:1521:orcl"/>
<property name="username" value="tsrescue"/>
<property name="password" value="123456"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1"/>
</bean>
<!-- 配置sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>cn/itcast/bean/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.current_session_context_class">thread</prop>这个配置不需要!!!!!!!!!!!!!!
</props> </property>
</bean>
<!-- spring提供的针对hibernate的事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 基于注解的方式声明事务 -->
<tx:annotation-driven transaction-manager="txManager"/>
<bean id = "personService" class="cn.itcast.Service.Impl.PersonServiceBean"/>
</beans>
代码部分:
1.person.java
package cn.itcast.bean; public class Person {
private Integer id;
private String name; public Person() {} public Person(String name) {
this.name = name;
} public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
} }
2.Person.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2016-6-1 10:08:44 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping package="cn.itcast.bean">
<class name="Person" table="person">
<id name="id">
<generator class="native" />
</id>
<property name="name"/>
</class>
</hibernate-mapping>
3.PersonService 接口
package cn.itcast.Service; import java.util.List; import cn.itcast.bean.Person; public interface PersonService { public abstract void save(Person person); public abstract void update(Person person); public abstract Person getPerson(Integer personId); public abstract void delete(Integer personId); public abstract List<Person> getPersons(); }
4.PersonService接口的实现类PersonServiceBean
package cn.itcast.Service.Impl; import java.util.List; import javax.annotation.Resource; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import cn.itcast.Service.PersonService;
import cn.itcast.bean.Person;
@Transactional
public class PersonServiceBean implements PersonService {
@Resource
private SessionFactory sessionFactory; @Override
public void save(Person person){
//取被spring容器管理的session
sessionFactory.getCurrentSession().persist(person);
} @Override
public void update(Person person){
sessionFactory.getCurrentSession().merge(person);
} @Transactional(propagation = Propagation.NOT_SUPPORTED,readOnly = true)
@Override
public Person getPerson(Integer personId){
return (Person) sessionFactory.getCurrentSession().get(Person.class, personId); } @Override
public void delete(Integer personId){
sessionFactory.getCurrentSession().delete(sessionFactory.getCurrentSession().load(Person.class, personId));
} @Transactional(propagation =Propagation.NOT_SUPPORTED,readOnly = true)
@SuppressWarnings("unchecked")
@Override
public List<Person> getPersons(){
return sessionFactory.getCurrentSession().createQuery("from person").list(); }
}
5.测试类
package cn.itcast.Service.Impl; import static org.junit.Assert.*; import java.util.List; import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.Service.PersonService;
import cn.itcast.bean.Person; public class PersonServiceBeanTest {
private static PersonService ps;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
ps = (PersonService) ctx.getBean("personService");
} catch (Exception e) {
e.printStackTrace();
}
} @Test
public void testSave() {
ps.save(new Person("小明3"));
} @Test
public void testUpdate() {
Person person = ps.getPerson(1);
person.setName("小明修改版");
ps.update(person);
} @Test
public void testGetPerson() {
Person p = ps.getPerson(1);
System.out.println(p.getName());
} @Test
public void testDelete() {
ps.delete(2);
} @Test
public void testGetPersons() {
List<Person> list = ps.getPersons();
for (Person person : list) {
person.getName();
}
} }