Dict.CN 在線詞典, 英語學習, 在線翻譯

          都市淘沙者

          荔枝FM Everyone can be host

          導航

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          公告


          我的blog中的部分資源是來自于網絡上,如果你認為侵犯了你的權利,請及時聯系jelver#163.com,我會盡快刪除!另外如果要留言最好通過郵件的形式跟我交流因為我不是很經常寫文章,前面有的朋友留言好久了我才發現,所以對不住大家,現在留下MSN:jelver#163.com和QQ:253840881,歡迎交流! (用@代替#)

          隨筆分類

          文章分類

          隨筆檔案

          文章檔案

          相冊

          統計

          留言簿(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"
          "<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 都市淘沙者 閱讀(568) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 德钦县| 同仁县| 太仆寺旗| 秦安县| 毕节市| 新乡县| 楚雄市| 调兵山市| 郧西县| 孝昌县| 礼泉县| 盘山县| 沙河市| 泽州县| 泾川县| 清丰县| 永定县| 襄垣县| 陕西省| 田阳县| 茌平县| 睢宁县| 湟中县| 彭阳县| 海丰县| 突泉县| 广安市| 渑池县| 黎城县| 黄梅县| 类乌齐县| 广德县| 绵阳市| 靖江市| 金阳县| 桐梓县| 高台县| 泸定县| 茂名市| 荆门市| 鹰潭市|