Chinaunix首页 | 论坛 | 博客
  • 博客访问: 390106
  • 博文数量: 165
  • 博客积分: 436
  • 博客等级: 下士
  • 技术积分: 887
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-10 02:49
文章分类

全部博文(165)

文章存档

2012年(95)

2011年(70)

分类:

2012-01-10 22:45:22

原文地址:SSH架构开发流程2 作者:xueliangfei

Hibernate开发第二步
 
2  开发并配置Hibernate DAO层AddressDAO.java
开发一个DAO类的AddressDAO.java 使用HibernateTemplate实现对数据表address的增 删 改 查的操作
 
[1]实现接口类IAddressDAO.java
首先创建一个接口类com.demo.hibernate.dao.IAdressDAO.java 创建接口的原因是为了让Spring的AOP机制能够进行事务的管理 因为事务的管理是基于AOP实现的
  1. package com.demo.hibernate.dao;

  2. import java.util.List;

  3. import com.demo.hibernate.beans.Address;

  4. public interface IAddressDAO {

  5.     public List findAllByUsername(final String username);


  6.     public Address findById(String id);

  7.     public boolean i***ist(final String username, final String name);

  8.     public void insert(Address address);

  9.     public void update(Address address);

  10.     public void delete(String id);
  11. }
 
[2]开发实现类AddressDAO.java
下面依次实现接口中的函数 根据用户登录和用户注册的实际操作逻辑 实现这些函数
  1. package com.demo.hibernate.dao;

  2. import java.util.List;

  3. import org.hibernate.Criteria;
  4. import org.hibernate.HibernateException;
  5. import org.hibernate.Session;
  6. import org.hibernate.criterion.Order;
  7. import org.hibernate.criterion.Projections;
  8. import org.hibernate.criterion.Restrictions;
  9. import org.springframework.orm.hibernate3.HibernateCallback;
  10. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

  11. import com.demo.hibernate.beans.Address;
  12. import com.demo.struts.util.Constants;


  13. public class AddressDAO extends HibernateDaoSupport implements IAddressDAO {

  14.     /**
  15.      * get address list
  16.      */
  17.     public List findAllByUsername(final String username) {
  18.         return (List) getHibernateTemplate().execute(new HibernateCallback() {
  19.             public Object doInHibernate(Session session)
  20.                     throws HibernateException {
  21.                 List result = session.createCriteria(Address.class).add(
  22.                         Restrictions.eq("username", username)).list();
  23.                 return result;
  24.             }
  25.         });
  26.     }
  27.     
  28.            
  29.         public Address findById(String id) {
  30.         return (Address) getHibernateTemplate().get(Address.class,
  31.                 new Integer(id));
  32.     }

  33.     public boolean i***ist(final String username, final String name) {
  34.         List list = (List) getHibernateTemplate().execute(
  35.                 new HibernateCallback() {
  36.                     public Object doInHibernate(Session session)
  37.                             throws HibernateException {
  38.                         List result = session.createCriteria(Address.class)
  39.                                 .add(Restrictions.eq("username", username))
  40.                                 .add(Restrictions.eq("name", name)).list();
  41.                         return result;
  42.                     }
  43.                 });
  44.         if (list.size() > 0) {
  45.             return true;
  46.         } else {
  47.             return false;
  48.         }
  49.     }

  50.     public void insert(Address address) {
  51.         getHibernateTemplate().save(address);
  52.     }

  53.     public void update(Address address) {
  54.         getHibernateTemplate().update(address);
  55.     }

  56.     public void delete(String id) {
  57.         Object p = getHibernateTemplate().load(Address.class, new Integer(id));
  58.         getHibernateTemplate().delete(p);
  59.     }

  60. }
 
 
 
[3]在applicationContext.xml中配置DAO及其代理
 
开发完了基于HibernateTemplate的DAO类com.demo.hibernate.dao.AddressDAO.java 下面在applicationContext.xml中配置改组件的Bean对象 并为改对象注入SessionFactory对象 同时为该DAO对象配置事务组件 使得对addressDAO的访问都在Spring的事务监管之下 该组件需要注入上面配置的事务对象transactionManager DAO对象addressDAO 并配置事务管理的策略
  1. <!-- 定义DAO -->
  2.     <bean id="addressDAO" class="com.demo.hibernate.dao.addressDAO">
  3.         <property name="sessionFactory">
  4.             <ref local="sessionFactory" />
  5.         </property>
  6.     </bean>
  7.     
  8.     
  9.     <!-- 定义DAO代理 -->
  10.     <bean id="addressDAOProxy"
  11.         class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  12.         <property name="transactionManager">
  13.             <ref bean="transactionManager" />
  14.         </property>
  15.         <property name="target">
  16.             <ref local="addressDAO" />
  17.         </property>
  18.         <property name="transactionAttributes">
  19.             <props>
  20.                 <prop key="insert*">PROPAGATION_REQUIRED</prop>
  21.                 <prop key="update*">PROPAGATION_REQUIRED</prop>
  22.                 <prop key="delete*">PROPAGATION_REQUIRED</prop>
  23.                 <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
  24.             </props>
  25.         </property>
  26.     </bean>
 
 
阅读(711) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~