jinfeng_wang

G-G-S,D-D-U!

BlogJava 首页 新随笔 联系 聚合 管理
  400 Posts :: 0 Stories :: 296 Comments :: 0 Trackbacks

Spring封装Hibernate

  有两种手段:1、使用HibernateTemplate2、继承HibernateDaoSupport(实际上,HibernateDaoSupport本身就是对HibernateTemplate的一个封装)

   一切尽在代码中,查看API唉!

注意:hibernate 2.X和hibernate3.X的区别,我用的是2.X 。  
 

package com.company.example.dao;

 

import java.util.List;

 

import com.company.example.model.Student;

 

public interface StudentDao {

    public abstract Student getStudent(final Integer id);

    public abstract void updateStudent(Student student);

    public abstract List findStudentsByLastName(String lastName);

}

==================

package com.company.example.dao.hibernateImpl;

 

import java.util.List;

 

import net.sf.hibernate.Hibernate;

import net.sf.hibernate.HibernateException;

import net.sf.hibernate.Session;

 

import org.springframework.orm.hibernate.HibernateCallback;

import org.springframework.orm.hibernate.HibernateTemplate;

 

import com.company.example.dao.StudentDao;

import com.company.example.model.Student;

 

public class StudentDaoHibernate implements StudentDao {

    HibernateTemplate hibernateTemplate;

 

    public static void main(String[] args) {

    }

 

    public HibernateTemplate getHibernateTemplate() {

        return hibernateTemplate;

    }

 

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {

        this.hibernateTemplate = hibernateTemplate;

    }

 

    public Student getStudent(final Integer id) {

        return (Student) hibernateTemplate.execute(new HibernateCallback() {

            public Object doInHibernate(Session session)

                    throws HibernateException {

                return session.load(Student.class, id);

            }

        });

    }

 

    public Student getStudent2(Integer id) {

        return (Student) hibernateTemplate.load(Student.class, id);

    }

 

    public void updateStudent(Student student) {

        hibernateTemplate.update(student);

    }

 

    public List findStudentsByLastName(String lastName) {

        return hibernateTemplate.find("from Student student "

                + "where student.lastName = ?", lastName, Hibernate.STRING);

    }

}

public class StudentDaoHibernate extends HibernateDaoSupport

implements StudentDao {

 ...........

}
======The Spring XML Configuration===========
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
 <bean id="dataSource"
  class="org.springframework.jndi.JndiObjectFactoryBean">
  <property name="jndiName">
   <value>java:comp/env/jdbc/trainingDatasource</value>
  </property>
 </bean>
 
 <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="dataSource"/>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.MySQLDialect</prop>
   </props>
  </property>
  <property name="mappingResources">
   <list> <!--list of each hbm.xml-->
    <value>Student.hbm.xml</value>
    <value>Course.hbm.xml</value>
   </list>
  </property>
  <property name="mappingDirectoryLocations">
   <list>
    <!--list the directory of the hbm.xml, including each xml file in the dir-->
    <value>classpath:/com/springinaction/training/model</value>
   </list>
  </property>
 </bean>
 
 <bean id="hibernateTemplate"
  class="org.springframework.orm.hibernate.HibernateTemplate">
  <property name="sessionFactory">
   <ref bean="sessionFactory"/>
  </property>
 </bean>

 <bean id="studentDao" class="com.company.example.dao.hibernateImpl.StudentDaoHibernate">
  <property name="hibernateTemplate">
   <ref bean="hibernateTemplate"/>
  </property>
 </bean>

 <bean id="courseDao" class="com.company.example.dao.hibernateImpl.CourseDaoHibernate">
  <property name="hibernateTemplate">
   <ref bean="hibernateTemplate"/>
  </property>
 </bean>
</beans>

posted on 2005-03-03 16:58 jinfeng_wang 阅读(4013) 评论(0)  编辑  收藏 所属分类: spring

只有注册用户登录后才能发表评论。


网站导航: