JSF+Spring+JPA(Hibernate实现)的环境搭建(3)

之后写DAO,无论你是用IDE生成的DAO还是自己写的DAO,代码大致如下:

 

@Transactional

public class PlayerDAO extends JpaDaoSupport implements IPlayerDAO {

    // property constants

    public static final String NAME = "name";

    public static final String MESS = "mess";

    public static final String PIC = "pic";

 

    public void save(PlayersVO entity) {

       logger.info("saving Player instance");

       try {

           getJpaTemplate().persist(entity);

           logger.info("save successful");

       } catch (RuntimeException re) {

           logger.error("save failed", re);

           throw re;

       }

    }

 

    public void delete(PlayersVO entity) {

 

       logger.info("deleting Player instance");

       try {

           entity = getJpaTemplate().getReference(PlayersVO.class,

                  entity.getId());

           getJpaTemplate().remove(entity);

           logger.info("delete successful");

       } catch (RuntimeException re) {

           logger.error("delete failed", re);

           throw re;

       }

    }

    public PlayersVO update(PlayersVO entity) {

       logger.info("updating Player instance");

       try {

           PlayersVO result = getJpaTemplate().merge(entity);

           logger.info("update successful");

           return result;

       } catch (RuntimeException re) {

           logger.error("update failed", re);

           throw re;

       }

    }

 

    public PlayersVO findById(Integer id) {

       logger.info("finding Player instance with id: " + id);

       try {

           PlayersVO instance = getJpaTemplate().find(PlayersVO.class, id);

           return instance;

       } catch (RuntimeException re) {

           logger.error("find failed", re);

           throw re;

       }

    }

 

    @SuppressWarnings("unchecked")

    public List<PlayersVO> findByProperty(String propertyName,

           final Object value, final int... rowStartIdxAndCount) {

       logger.info("finding Player instance with property: " + propertyName

              + ", value: " + value);

       try {

           final String queryString = "select model from PlayersVO model where model."

                  + propertyName + "= :propertyValue";

           return getJpaTemplate().executeFind(new JpaCallback() {

              public Object doInJpa(EntityManager em)

                     throws PersistenceException {

                  Query query = em.createQuery(queryString);

                  query.setParameter("propertyValue", value);

                  if (rowStartIdxAndCount != null

                         && rowStartIdxAndCount.length > 0) {

 

                     // 有分页

                     int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);

 

                     // 开始页

                     if (rowStartIdx > 0) {

                         query.setFirstResult(rowStartIdx);

                     }

 

                     // 一页最大记录数目

                     if (rowStartIdxAndCount.length > 1) {

                         int rowCount = Math.max(0, rowStartIdxAndCount[1]);

                         if (rowCount > 0) {

                            query.setMaxResults(rowCount);

                         }

                     }

                  }

                  return query.getResultList();

              }

           });

       } catch (RuntimeException re) {

           logger.error("find by property name failed", re);

           throw re;

       }

    }

 

    public List<PlayersVO> findByName(Object name, int... rowStartIdxAndCount) {

       return findByProperty(NAME, name, rowStartIdxAndCount);

    }

 

    public List<PlayersVO> findByMess(Object mess, int... rowStartIdxAndCount) {

       return findByProperty(MESS, mess, rowStartIdxAndCount);

    }

 

    public List<PlayersVO> findByPic(Object pic, int... rowStartIdxAndCount) {

       return findByProperty(PIC, pic, rowStartIdxAndCount);

    }

 

    public List<PlayersVO> findAll(final int... rowStartIdxAndCount) {

       logger.info("finding all Player instances");

       try {

           final String queryString = "select model from PlayersVO model";

           return getJpaTemplate().executeFind(new JpaCallback() {

              public Object doInJpa(EntityManager em)

                     throws PersistenceException {

                  Query query = em.createQuery(queryString);

                  if (rowStartIdxAndCount != null

                         && rowStartIdxAndCount.length > 0) {

                     int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);

                     if (rowStartIdx > 0) {

                         query.setFirstResult(rowStartIdx);

                      }

 

                     if (rowStartIdxAndCount.length > 1) {

                         int rowCount = Math.max(0, rowStartIdxAndCount[1]);

                         if (rowCount > 0) {

                            query.setMaxResults(rowCount);

                         }

                     }

                  }

                  return query.getResultList();

              }

           });

       } catch (RuntimeException re) {

           logger.error("find all failed", re);

           throw re;

       }

    }

 

         

    public static IPlayerDAO getFromApplicationContext(ApplicationContext ctx) {

       return (IPlayerDAO) ctx.getBean("PlayerDAO");

    }

}

注意:之所以加入注解:@Transactional,是为了AOP进行事务处理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值