<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    kapok

    垃圾桶,嘿嘿,我藏的這么深你們還能找到啊,真牛!

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      455 隨筆 :: 0 文章 :: 76 評論 :: 0 Trackbacks

    http://m.tkk7.com/jinfeng_wang/archive/2005/03/03/1654.html

    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-04-03 23:12 笨笨 閱讀(311) 評論(0)  編輯  收藏 所屬分類: HibernateAndSpringALL
    主站蜘蛛池模板: 一本色道久久88亚洲精品综合| 亚洲av日韩av无码黑人| 亚洲精品中文字幕无乱码麻豆| 最近2019中文免费字幕在线观看| 亚洲精品无码鲁网中文电影| 精品97国产免费人成视频| 亚洲码国产精品高潮在线| 亚洲免费观看视频| 亚洲美女人黄网成人女| 四虎永久在线观看免费网站网址| 亚洲一级毛片免费看| 国产自偷亚洲精品页65页| 一级毛片完整版免费播放一区| 亚洲一区二区三区在线视频| 久久99久久成人免费播放| 亚洲国产成人一区二区精品区| 99xxoo视频在线永久免费观看| 91亚洲精品麻豆| 日韩精品视频免费网址| www免费黄色网| 久久久久亚洲精品影视| 无码人妻精品中文字幕免费东京热| 亚洲中文字幕久久久一区| www.亚洲精品.com| 亚洲午夜无码毛片av久久京东热| 免费看AV毛片一区二区三区| 粉色视频成年免费人15次| 国产亚洲精品自在线观看| 5g影院5g天天爽永久免费影院| 国产亚洲精aa在线看| 亚洲片国产一区一级在线观看 | 无码乱肉视频免费大全合集| 亚洲а∨精品天堂在线| 亚洲中文字幕不卡无码| 日韩一区二区a片免费观看| 免费无毒a网站在线观看| 亚洲自偷自拍另类12p| 国产老女人精品免费视频| 国产成人久久AV免费| 久久无码av亚洲精品色午夜| 亚洲av无码一区二区三区网站|