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 康文 閱讀(271) 評論(0)  編輯  收藏 所屬分類: java

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

          導航

          統計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 彭泽县| 武川县| 隆回县| 南平市| 津南区| 潞西市| 宾川县| 蒲江县| 监利县| 隆德县| 洛南县| 彭山县| 肥东县| 红桥区| 南华县| 金寨县| 盐城市| 雷山县| 封丘县| 乌拉特后旗| 新龙县| 遵化市| 成都市| 民和| 桓台县| 黄陵县| 张家川| 霍林郭勒市| 调兵山市| 卢氏县| 镇原县| 绥中县| 商水县| 沙洋县| 齐河县| 洛宁县| 大石桥市| 山西省| 子洲县| 万盛区| 望奎县|