spring -database

          一 Spring DAO philosophy
          ?1 Understanding Spring's DataAccesssException
          ?Spring's DAO frameworks donot throw teechnology-specific exceptions such as SQLException
          or HibernateeExcepiton.Instead ,all exceptions thrown are subclasses of DataAccessException
          ?2 You are not forced to handle DataAccessExceptions
          ?DataAccessException is a RuntimeException,so it si an unchecked exception.Since these are quite often unrecoverable,you are not forced to handle these exception.
          ? Instead ,you can catch the exception if recovery is possible.since DataAccessException is not only a RuntimeException,but it subclasses Spring's NestedRuntimeException. This menas that the root Exception is alwarys via NestedRuntimeException's getCause() method.
          ?3 Work with DataSources
          ? a getting a Datasource from JNDI
          ?? <bean id="dataSource"
          ????? class="org.springframework.jndi.JndiObjectFactoryBean">
          ?? <property name="jndiName">
          ??? <value>java:comp/env/jdbc/myDatasource</value>
          ?? </property>
          ?? </bean>
          ? b Creating a Datasource connection pool
          ?? <bean id="dataSource"
          ????? class="org.apache.commons.dbcp.BasicDataSource">
          ? <property name="driver">
          ??? <value>${db.driver}</value>
          ? </property>
          ? <property name="url">
          ??? <value>${db.url}</value>
          ? </property>
          ? <property name="username">
          ??? <value>${db.username}</value>
          ? </property>
          ? <property name="password">
          ??? <value>${db.password}</value>
          ? </property>
          </bean>
          ?c Using a DataSource while testing
          ? DriverManagerDataSource dataSource = new DriverManagerDataSource();
          ? dataSource.setDriverClassName(driver);
          ? dataSource.setUrl(url);
          ? dataSource.setUsername(username);
          ? dataSource.setPassword(password);
          ?4 Consistent DAO support
          ? Spring template class handle the invariant part of data access-controling the trancsaction
          manage resource,handling exception .Implementation of callback interface define what is specific to your application--creating statement,binding parameter and marshalling result set.
          ?Spring separates the fixed an vaiant parts of data access process into tow distince classes:
          template and callbacks.Template manage the fixed parts of the process while callback are where you fill in the implement details;
          ?one the top of template-callback desing ,spring framework provide a support class which your own data access subclass it. And the support class already have a property for holding a template.
          ?二 Integerating Hibernate with Spring
          ? 1 Managing Hibernate resources
          ?? you will keep a single instance of SessionFactory throughtout your application
          ?? <bean id="sessionFactory"class="org.springframework.
          ?????????? orm.hibernate.LocalSessionFactoryBean">
          ???? <bean id="sessionFactory" class="org.springframework.
          ?????? orm.hibernate.LocalSessionFactoryBean">
          ???? <property name="dataSource">
          ?????? <ref bean="dataSource"/>
          ???? </property>
          ? </bean>
          ? you also want to manager how hibernate is configured
          ? <bean id="sessionFactory" class="org.springframework.
          ?????? orm.hibernate.LocalSessionFactoryBean">
          ? <property name="hibernateProperties">
          ??? <props>
          ????? <prop key="hibernate.dialect">net.sf.hibernate.
          ?????????? dialect.MySQLDialect</prop>

          ??? </props>
          ? </property>
          ? …
          ?</bean>
          ?and the last thing is whick map files is read
          ? <bean id="sessionFactory" class="org.springframework.
          ?????? orm.hibernate.LocalSessionFactoryBean">
          ? <property name="mappingResources">
          ???? <list>
          ???? <value>Student.hbm.xml</value>
          ???? <value>Course.hbm.xml</value>
          ???? …
          ?? </list>
          ? </property>
          ???? …
          ? </bean>

          ?Now? you have fully configured your sessionfactory ,so we need do create an object which we
          will access hibernate. As we know, we will use a template class
          ? <bean id="hibernateTemplate"
          ????? class="org.springframework.orm.hibernate.HibernateTemplate">
          ? <property name="sessionFactory">
          ??? <ref bean="sessionFactory"/>
          ? </property>
          ?</bean>

          ? <bean id="courseDao" class="com.springinaction.
          ?????? training.dao.hibernate.CourseDaoHibernate">
          ? <property name="hibernateTemplate">
          ??? <ref bean="hibernateTemplate"/>
          ? </property>
          ?</bean>

          ?2 Accessing Hibernate through HibernatTemplate
          ? The template-callback mechanism in Hibernatee is pretty simple.There is the HibernatTmpplate and one callback interface
          ? 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);
          ????? }
          ??? });
          ?
          ? The HibernateTemplate class provides some convience methods that implicit create a HibernateCallback instance:
          ? (Student) hibernateTemplate.load(Student.class, id);
          ?? hibernateTemplate.update(student);
          ? hibernateTemplate.find("from Student student " +
          ??????????????????????????????? "where student.lastName = ?",
          ??????????????????????????????? lastName, Hibernate.STRING);
          ? 3 Subclassing HibernateDaoSupport
          ? public class StudentDaoHibernate extends HibernateDaoSupport
          ??? implements StudentDao {
          ? …
          ? }
          ? getHibernateTemplate()
          ? getSession()

          posted on 2006-09-14 11:45 康文 閱讀(272) 評論(0)  編輯  收藏 所屬分類: java

          <2006年9月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 盐山县| 阿巴嘎旗| 麻栗坡县| 西昌市| 汉中市| 承德市| 岳阳县| 宜昌市| 淮阳县| 兴安县| 来宾市| 沂南县| 紫云| 中宁县| 南汇区| SHOW| 天等县| 朝阳市| 南木林县| 莱州市| 乌什县| 四平市| 东明县| 都兰县| 满城县| 大竹县| 嘉义县| 芮城县| 遂川县| 商河县| 淮滨县| 金阳县| 巧家县| 揭阳市| 望城县| 武宁县| 阳曲县| 塘沽区| 重庆市| 兴业县| 无锡市|