接上篇:
(8)下面我们写一个业务Bean来操作实体:
为了面向接口编程我们先创建一个接口:PersonService
package com.java.service;
import java.util.List;
import com.java.entity.Person;
public interface PersonService {
public abstract void save(Person person);
public abstract void update(Person person);
public abstract Person getPerson(Integer id);
public abstract void delete(Integer id);
public abstract List<Person> getPersons();
}
然后创建这个接口的实现类并且配置事务和注入sessionFactory:
package com.java.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.java.entity.Person;
import com.java.service.PersonService;
@Transactional
public class PersonServiceImpl implements PersonService {
@Resource private SessionFactory sessionFactory;
public void save(Person person){
sessionFactory.getCurrentSession().persist(person);
}
public void update(Person person){
sessionFactory.getCurrentSession().merge(person);
}
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
public Person getPerson(Integer id){
return (Person)sessionFactory.getCurrentSession().get(Person.class, id);
}
public void delete(Integer id){
sessionFactory.getCurrentSession().delete(
sessionFactory.getCurrentSession().load(Person.class, id));
}
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
@SuppressWarnings("unchecked")
public List<Person> getPersons(){
return sessionFactory.getCurrentSession().createQuery("FROM Person").list();
}
}
备注:上面的sessionFactory 应用了@Resource,把SessionFactory实例注入给sessionFactory。
Transactional表示在操作该类时,需要事务管理。
另外在查询数据时应该用只读事务,这样可以提高性能:
@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)。
(9)最后我们对这个类创建单元测试(Junit):因为在单元测试中需要获取PersonServiceImpl实例,所以还需要在beans.xml文件中配置如下:
<bean id="personService " class="com.java.service.impl.PersonServiceImpl"/>
下面把完整的spring配置文件复制如下:
<?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:annotation-config/>
<!-- 连接池的配置 -->
<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:@localhost:1521:orcl"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="1"/>
<!-- 连接池的最大值 -->
<property name="maxActive" value="500"/>
<!-- 最大空闲值 -->
<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>com/java/entity/Person.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.OracleDialect
hibernate.hbm2ddl.auto=update
<!-- 测试阶段使用 -->
hibernate.show_sql=true
hibernate.format_sql=true
</value>
</property>
</bean>
<!-- 配置事务管理 -->
<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="com.java.service.impl.PersonServiceImpl"/>
</beans>
====================================
package com.java.junit;
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 com.java.entity.Person;
import com.java.service.PersonService;
public class PersonTest {
private static PersonService personService;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml" );
personService=(PersonService)ctx.getBean("personService ");
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testSave() {
personService.save(new Person("oracle"));
}
@Test
public void testUpdate() {
Person person=personService.getPerson(41);
person.setName("oracle41");
personService.update(person);
}
@Test
public void testGetPerson() {
Person person=personService.getPerson(42);
System.out.println(person.getId()+"-->"+person.getName());
}
@Test
public void testDelete() {
personService.delete(43);
}
@Test
public void testGetPersons() {
List<Person> persons=personService.getPersons();
for(Person person:persons){
System.out.println(person.getId()+"-->"+person.getName());
}
}
}
接下来运行单元测试,测试完成,没有出错的话我们基本上就完成了spring+hibernate的整合了。