自动生成的hibernate DAO的几个方法自动生成的hibernate DAO的几个方法savedeletefindbyid 按ID查询数据.f
自动生成的hibernate DAO的几个方法
自动生成的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");
}
}