通用的DAO接口:
package org.dave.base.dao; import java.util.List; public interface BaseDAO { public void save(Object entity); public void delete(Object entity); public Object findById(String id); public List findAll(); public List findByExample(Object exampleEntity); public List findByProperty(String field, Object value); public List findByProperty(String field, Object minValue, Object maxValue); public List findByLinkProperty(String field, Object value); public Object merge(Object entity); public void attachDirty(Object entity); public void attachClean(Object entity); }
公共的DAO适配器:
package org.dave.base.dao.adapter; import java.util.List; import org.dave.base.dao.BaseDAO; import org.hibernate.LockMode; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public abstract class BaseDAOAdapter extends HibernateDaoSupport implements BaseDAO { protected BaseDAOAdapter() { } public void save(Object entity) { getHibernateTemplate().saveOrUpdate(entity); } public void delete(Object entity) { getHibernateTemplate().delete(entity); } public List findAll(String entityName) { String queryString = "from " + entityName; return getHibernateTemplate().find(queryString); } public Object findById(String entityName, String id) { return getHibernateTemplate().get(entityName, id); } public List findByProperty(String entityName, String field, Object value) { String queryString = "from " + entityName + " as model where model." + field + "= ?"; return getHibernateTemplate().find(queryString, value); } public List findByProperty(String entityName, String field, Object minValue, Object maxValue) { String queryString = "from " + entityName + " as model where model." + field + ">? and model." + field + "<?"; String[] values = { minValue, maxValue }; return getHibernateTemplate().find(queryString, values); } public List findByProperty(String entityName, String field, String value) { String queryString = "from " + entityName + " as model where model." + field + "link '%" + value + "%'"; return getHibernateTemplate().find(queryString); } public List findByExample(Object exampleEntity) { return getHibernateTemplate().findByExample(exampleEntity); } public Object merge(Object entity) { return getHibernateTemplate().merge(entity); } public void attachDirty(Object entity) { getHibernateTemplate().saveOrUpdate(entity); } public void attachClean(Object entity) { getHibernateTemplate().lock(entity, LockMode.NONE); } }
例子:
2.DAO接口:
package org.dave.user.dao; import java.util.List; import org.dave.base.dao.BaseDAO; public interface UserDAO extends BaseDAO { public List findByUsername(String username); public List findByUserpass(String userpass); }
3.DAO类:
package org.dave.user.dao.impl; import java.util.List; import org.dave.base.dao.adapter.BaseDAOAdapter; import org.dave.user.dao.UserDAO; public class UserDAOImpl extends BaseDAOAdapter implements UserDAO { public static final String USER = "org.dave.user.model.User"; public static final String USERNAME = "username"; public static final String USERPASS = "userpass"; public List findAll() { return super.findAll(USER); } public Object findById(String id) { return super.findById(USER, id); } public List findByProperty(String field, Object value) { return super.findByProperty(USER, field, value); } public List findByProperty(String field, Object minValue, Object maxValue) { return super.findByProperty(USER, field, minValue, maxValue); } public List findByLinkProperty(String field, String value) { return super.findByProperty(USER, field, value); } public List findByUsername(String username) { return this.findByProperty(USERNAME, username); } public List findByUserpass(String userpass) { return this.findByProperty(USERPASS, userpass); } }