您好,欢迎光临本网站![请登录][注册会员]  
文件名称: 数据库操作
  所属分类: Java
  开发工具:
  文件大小: 868kb
  下载次数: 0
  上传时间: 2013-12-17
  提 供 者: u0131*****
 详细说明: package dao; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceException; import javax.persistence.Query; import org.springframework.context.ApplicationContext; import org.springframework.orm.jpa.JpaCallback; import org.springframework.orm.jpa.support.JpaDaoSupport; import org.springframework.transaction.annotation.Transactional; /** * A data access object (DAO) providing persistence and search support for * Student entities. Transaction control of the save(), update() and del ete() * operations can directly support Spring container-managed transactions or they * can be augmented to handle user-managed Spring transactions. Each of these * methods provides additional information for how to configure it for the * desired type of transaction control. * * @see dao.Student * @author MyEclipse Persistence Tools */ @Transactional public class StudentDAO extends JpaDaoSupport implements IStudentDAO { // property constants public static final String USERNAME = "username"; public static final String PASSWORD = "password"; public static final String AGE = "age"; /** * Perform an initial save of a previously unsaved Student entity. All * subsequent persist actions of this entity should use the #update() * method. This operation must be performed within the a database * transaction context for the entity's data to be permanently saved to the * persistence store, i.e., database. This method uses the * {@link javax.persistence.EntityManager#persist(Object) EntityManager#persist} * operation. *

* User-managed Spring transaction example: * *

 * TransactionStatus txn = txManager * .getTransaction(new DefaultTransactionDefinition()); * StudentDAO.save(entity); * txManager.commit(txn); * 
* * @see Spring * container-managed transaction examples * @param entity * Student entity to persist * @throws RuntimeException * when the operation fails */ public void save(Student entity) { logger.info("saving Student instance"); try { getJpaTemplate().persist(entity); logger.info("save successful"); } catch (RuntimeException re) { logger.error("save failed", re); throw re; } } /** * Delete a persistent Student entity. This operation must be performed * within the a database transaction context for the entity's data to be * permanently deleted from the persistence store, i.e., database. This * method uses the * {@link javax.persistence.EntityManager#remove(Object) EntityManager#delete} * operation. *

* User-managed Spring transaction example: * *

 * TransactionStatus txn = txManager * .getTransaction(new DefaultTransactionDefinition()); * StudentDAO.delete(entity); * txManager.commit(txn); * entity = null; * 
* * @see Spring * container-managed transaction examples * @param entity * Student entity to delete * @throws RuntimeException * when the operation fails */ public void delete(Student entity) { logger.info("deleting Student instance"); try { entity = getJpaTemplate().getReference(Student.class, entity.getId()); getJpaTemplate().remove(entity); logger.info("delete successful"); } catch (RuntimeException re) { logger.error("delete failed", re); throw re; } } /** * Persist a previously saved Student entity and return it or a copy of it * to the sender. A copy of the Student entity parameter is returned when * the JPA persistence mechanism has not previously been tracking the * updated entity. This operation must be performed within the a database * transaction context for the entity's data to be permanently saved to the * persistence store, i.e., database. This method uses the * {@link javax.persistence.EntityManager#merge(Object) EntityManager#merge} * operation. *

* User-managed Spring transaction example: * *

 * TransactionStatus txn = txManager * .getTransaction(new DefaultTransactionDefinition()); * entity = StudentDAO.update(entity); * txManager.commit(txn); * 
* * @see Spring * container-managed transaction examples * @param entity * Student entity to update * @returns Student the persisted Student entity instance, may not be the * same * @throws RuntimeException * if the operation fails */ public Student update(Student entity) { logger.info("updating Student instance"); try { Student result = getJpaTemplate().merge(entity); logger.info("update successful"); return result; } catch (RuntimeException re) { logger.error("update failed", re); throw re; } } public Student findById(Integer id) { logger.info("finding Student instance with id: " + id); try { Student instance = getJpaTemplate().find(Student.class, id); return instance; } catch (RuntimeException re) { logger.error("find failed", re); throw re; } } /** * Find all Student entities with a specific property value. * * @param propertyName * the name of the Student property to query * @param value * the property value to match * @param rowStartIdxAndCount * Optional int varargs. rowStartIdxAndCount[0] specifies the the * row index in the query result-set to begin collecting the * results. rowStartIdxAndCount[1] specifies the the maximum * number of results to return. * @return List found by query */ @SuppressWarnings("unchecked") public List findByProperty(String propertyName, final Object value, final int... rowStartIdxAndCount) { logger.info("finding Student instance with property: " + propertyName + ", value: " + value); try { final String queryString = "select model from Student 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 findByUsername(Object username, int... rowStartIdxAndCount) { return findByProperty(USERNAME, username, rowStartIdxAndCount); } public List findByPassword(Object password, int... rowStartIdxAndCount) { return findByProperty(PASSWORD, password, rowStartIdxAndCount); } public List findByAge(Object age, int... rowStartIdxAndCount) { return findByProperty(AGE, age, rowStartIdxAndCount); } /** * Find all Student entities. * * @param rowStartIdxAndCount * Optional int varargs. rowStartIdxAndCount[0] specifies the the * row index in the query result-set to begin collecting the * results. rowStartIdxAndCount[1] specifies the the maximum * count of results to return. * @return List all Student entities */ @SuppressWarnings("unchecked") public List findAll(final int... rowStartIdxAndCount) { logger.info("finding all Student instances"); try { final String queryString = "select model from Student 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 IStudentDAO getFromApplicationContext(ApplicationContext ctx) { return (IStudentDAO) ctx.getBean("StudentDAO"); } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)

下载文件列表

相关说明

  • 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
  • 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度
  • 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
  • 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
  • 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
  • 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.
 相关搜索: JPA
 输入关键字,在本站1000多万海量源码库中尽情搜索: