spring2.5.6与hibernate3.3整合

时间:2022-02-23 05:01:10

1.整合spring与hibernate,spring的ioc与hibernate均运用注解,hibernate操作数据库用hibernateTemplate,加上了声明式事务管理。

2.
hibernate需要的包:spring2.5.6与hibernate3.3整合

spring需要的相关包: spring2.5.6与hibernate3.3整合

连接 oracle 所需包:spring2.5.6与hibernate3.3整合

3.新建Java项目,创建lib文件夹,将以上jar包复制在里面,并且右键jar包,add to build path,将其加入构建路径。

4.加入model层,包括Student和Teacher

package com.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

@Entity
public class Student {
    private int id;
    private String name;
    private int age;
    @SequenceGenerator(name="sequenceGenerator",sequenceName="student_sequence",allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequenceGenerator")
    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}
package com.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

@Entity
public class Teacher {
    private int id;
    private String name;
    private int wage;

    @SequenceGenerator(name="sequenceGenerator",sequenceName="teacher_sequence",allocationSize=1)
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="sequenceGenerator")
    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getWage() {
        return wage;
    }
    public void setWage(int wage) {
        this.wage = wage;
    }

}

5.加入dao层,包括StudentDao和TeacherDao

package com.dao;

import java.util.List;
import com.model.Student;

public interface StudentDao {
    public void save(Student s);
    public List<Student> queryByName(String name);
}
package com.dao;

import java.util.List;
import com.model.Teacher;

public interface TeacherDao {
    public void save(Teacher t);
    public List<Teacher> queryByName(String name);
}

6.加入DaoImpl层,包括StudentDaoImpl和TeacherDaoImpl

package com.dao.impl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import com.dao.StudentDao;
import com.model.Student;

@Component
public class StudentDaoImpl implements StudentDao {
    private HibernateTemplate hibernateTemplate;
    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }
    @Resource
    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    public void save(Student s) {
        hibernateTemplate.save(s);
    }

    public List<Student> queryByName(String name){
        String sql = "from Student s where s.name=?";
        List<Student> list = hibernateTemplate.find(sql,name);
        return list;
    }
}
package com.dao.impl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;
import com.dao.TeacherDao;
import com.model.Teacher;
@Component
public class TeacherDaoImpl implements TeacherDao{
     private HibernateTemplate hibernateTemplate;

    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }
    @Resource
    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    public void save(Teacher t){
        hibernateTemplate.save(t);
    }

    @SuppressWarnings("unchecked")
    public List<Teacher> queryByName(String name){
        String sql = "from Teacher t where t.name=?";
        List<Teacher> list = hibernateTemplate.find(sql,name);
        return list;
    }
}

7.加入service层,包括ST_Service

package com.service;

import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.dao.StudentDao;
import com.dao.TeacherDao;
import com.model.Student;
import com.model.Teacher;

@Component("sT_Service")
public class ST_Service {
    private StudentDao studentDao;
    private TeacherDao teacherDao;

    public StudentDao getStudentDao() {
        return studentDao;
    }
    @Resource(name="studentDaoImpl")
    public void setStudentDao(StudentDao studentDao) {
        this.studentDao = studentDao;
    }
    public TeacherDao getTeacherDao() {
        return teacherDao;
    }
    @Resource(name="teacherDaoImpl")
    public void setTeacherDao(TeacherDao teacherDao) {
        this.teacherDao = teacherDao;
    }

    public void addStudentAndTeacher(Student s,Teacher t){
            studentDao.save(s);
            teacherDao.save(t);
    }

}

8.在src目录下加入bean.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 />
    <context:component-scan base-package="com.dao.impl,com.service" />

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
        <property name="username" value="cat"/>
        <property name="password" value="cat"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
        <property name="dataSource" ref="myDataSource" />  
        <property name="packagesToScan">
            <list>
                <value>com.model</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.hbm2ddl.auto">update</prop>  
                <prop key="hibernate.max_fetch_depth">1</prop>  
                <prop key="hibernate.use_sql_comments">true</prop>  
            </props>  
        </property>  
    </bean>

    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="serviceOperation" expression="execution(public * com.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
    </aop:config>

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

</beans>

9.加入测试类 Test

package com.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.model.Student;
import com.model.Teacher;
import com.service.ST_Service;

public class Test {

    public static void main(String[] args) {

        Student s = new Student();
        s.setName("tom");
        s.setAge(11);
        Teacher t = new Teacher();
        t.setName("lily");
        t.setWage(3000000);

        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        ST_Service ss = (ST_Service)context.getBean("sT_Service");
        ss.addStudentAndTeacher(s, t);

    }

}

10.
完整目录:spring2.5.6与hibernate3.3整合

相关源码下载地址:http://download.csdn.net/detail/qwcs163/9116001