hibernate3与spring2.5.6整合Demo

时间:2021-10-23 05:02:52
Entity

package entity;

public class Person implements java.io.Serializable {

    private Integer id;
    private String name;


    public Person() {
    }


    public Person(Integer id, String name) {
        this.id = id;
        this.name = name;
    }



    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
Person.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="entity.Person" table="PERSON" schema="SCOTT">
        <id name="id" type="java.lang.Integer">
            <column name="ID" precision="6" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="20" not-null="true" />
        </property>
    </class>
</hibernate-mapping>


service

package service;

import java.util.List;

import 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();

}
service.impl

package 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 service.PersonService;
import entity.Person;
@Transactional
public class PersonServiceBean implements PersonService {
    @Resource     private SessionFactory sessionFactory;
    
    @Override
    public void save(Person person){
        sessionFactory.getCurrentSession().persist(person);
    }

    @Override
    public void update(Person person){
        sessionFactory.getCurrentSession().merge(person);
    }
    
    @Override
    @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
    public Person getPerson(Integer id){
        return (Person)sessionFactory.getCurrentSession().get(Person.class, id);
        
    }
    

    @Override
    public void delete(Integer id){
        sessionFactory.getCurrentSession().delete(
                sessionFactory.getCurrentSession().load(Person.class, id));
    }
    
    @SuppressWarnings("unchecked")
    @Override
    @Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
    public List<Person> getPersons(){
        return sessionFactory.getCurrentSession().createQuery("from Person").list();
        
    }
}

applicationContext.xml

<?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">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
        <!-- 提供连接数据库的URL地址 -->
        <property name="username" value="scott"></property>
        <property name="password" value="bdqn"></property>
    </bean>
    <!-- 配置sessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <value>
                hibernate.show_sql=true
                hibernate.format_sql=true
                hibernate.dialect=org.hibernate.dialect.Oracle9Dialect
                hibernate.hbm2ddl.auto=update
            </value>
        </property>
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath:entity/</value>
            </list>
        </property>
    </bean>
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    
    <tx:annotation-driven transaction-manager="txManager"/>
    <bean id="personService" class="service.impl.PersonServiceBean">
        
    </bean>
</beans>


JunitTest

package test;

import java.util.List;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.PersonService;
import entity.Person;

public class PersonServiceBeanTest {
    private static PersonService personService;
    
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        try {
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
            personService = (PersonService)applicationContext.getBean("personService");
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testSave() {
        personService.save(new Person(4,"小张"));
    }

    @Test
    public void testUpdate() {
        Person person = personService.getPerson(4);
        //....
        person.setName("小丽");
        personService.update(person);
    }

    @Test
    public void testGetPerson() {
        Person person = personService.getPerson(4);
        System.out.println(person.getName());
        try {
            System.out.println("请关闭数据库");
            Thread.sleep(1000*15);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("第二次开始获取");
        person = personService.getPerson(2);
        System.out.println(person.getName());
    }

    @Test
    public void testDelete() {
        personService.delete(1);
    }

    @Test
    public void testGetPersons() {
        List<Person> persons = personService.getPersons();
        for(Person person : persons){
            System.out.println(person.getName());
        }
    }

}

完整项目。密码:vz7c