Dict.CN 在线词典, 英语学习, 在线翻译

都市淘沙者

荔枝FM Everyone can be host

统计

留言簿(23)

积分与排名

优秀学习网站

友情连接

阅读排行榜

评论排行榜

Spring封装Hibernate

 

  有两种手段: 1 、使用 HibernateTemplate 2 、继承 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 2006-04-30 17:37 都市淘沙者 阅读(564) 评论(0)  编辑  收藏


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


网站导航: