Chinaunix首页 | 论坛 | 博客
  • 博客访问: 182995
  • 博文数量: 92
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1413
  • 用 户 组: 普通用户
  • 注册时间: 2013-02-04 21:12
文章分类
文章存档

2013年(92)

我的朋友

分类: 信息化

2013-03-14 05:35:28

主动生成的hibernate DAO的几个办法 save delete findbyid?按ID查询数据. findbyexample?相当于"select * from Usertable"完成的功用就是查询一切 * 数据. findbyproperty ?用来灵敏的供给一种按条件查询的办法,你能够个人界说要按什么样的方 * 式查询. * save()办法供给了向数据库中增加数据的功用,但只能增加,这个DAO没有生成Update()的办法 * 但你能够简略的把save()办法改成具有Update功用:将getSession().save * (transientInstance);这句改成 * getSession().merge(transientInstance);或许getSession().saveOrUpdate * (transientInstance); public class SignupDAO extends HibernateDaoSupport { private static final Logger log = LoggerFactory.getLogger(SignupDAO.class); // property constants public static final String USERNAME = "username"; public static final String PASSWORD = "password"; protected void initDao() { // do nothing } public void save(Signup transientInstance) { log.debug("saving Signup instance"); try { getHibernateTemplate().save(transientInstance); log.debug("save successful"); } catch (RuntimeException re) { log.error("save failed", re); throw re; } } public void delete(Signup persistentInstance) { log.debug("deleting Signup instance"); try { getHibernateTemplate().delete(persistentInstance); log.debug("delete successful"); } catch (RuntimeException re) { log.error("delete failed", re); throw re; } } public Signup findById(int id) { System.out.println(id); log.debug("getting Signup instance with id: "   id); try { Signup instance = (Signup) getHibernateTemplate().get( "com.jjbond.model.Signup", id); return instance; } catch (RuntimeException re) { log.error("get failed", re); throw re; } } public List findByExample(Signup instance) { log.debug("finding Signup instance by example"); try { List results = getHibernateTemplate().findByExample(instance); log.debug("find by example successful, result size: "   results.size()); return results; } catch (RuntimeException re) { log.error("find by example failed", re); throw re; } } public List findByProperty(String propertyName, Object value) { log.debug("finding Signup instance with property: "   propertyName   ", value: "   value); try { String queryString = "from Signup as model where model."   propertyName   "= ?"; System.out.println(log); // HibernateTemplate ht = new HibernateTemplate(); // System.out.println(ht); return getHibernateTemplate().find(queryString, value); } catch (RuntimeException re) { log.error("find by property name failed", re); throw re; } } public List findByUsername(Object username) { return findByProperty(USERNAME, username); } public List findByPassword(Object password) { return findByProperty(PASSWORD, password); } public List findAll() { log.debug("finding all Signup instances"); try { String queryString = "from Signup"; return getHibernateTemplate().find(queryString); } catch (RuntimeException re) { log.error("find all failed", re); throw re; } } public Signup merge(Signup detachedInstance) { log.debug("merging Signup instance"); try { Signup result = (Signup) getHibernateTemplate().merge( detachedInstance); log.debug("merge successful"); return result; } catch (RuntimeException re) { log.error("merge failed", re); throw re; } } public void attachDirty(Signup instance) { log.debug("attaching dirty Signup instance"); try { getHibernateTemplate().saveOrUpdate(instance); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public void attachClean(Signup instance) { log.debug("attaching clean Signup instance"); try { getHibernateTemplate().lock(instance, LockMode.NONE); log.debug("attach successful"); } catch (RuntimeException re) { log.error("attach failed", re); throw re; } } public static SignupDAO getFromApplicationContext(ApplicationContext ctx) { return (SignupDAO) ctx.getBean("SignupDAO"); } }
阅读(601) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~