之后写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进行事务处理。