1、先设计一个基类BaseBean
(1)@MappedSuperclass表示实体类父类
package com.life.hibernate.bean; import java.util.Date; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Version; @MappedSuperclass public class BaseBean { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Integer id; @Version private Integer version; private boolean deleted; @Temporal(value = TemporalType.TIMESTAMP) private Date dateCreated; }
2、通用Dao层设计
package com.life.dao; import java.util.List; import java.util.Map; import org.hibernate.Query; public interface IDao<T> { /** * 根据id查找实体 * * @param clazz * @param id * @return */ public T find(Class<T> clazz, int id); public void create(T t); public void save(T t); public void delete(T t); /** * 查询某页实体 * @param hql 查询语句 * @param firstResult 从第几条开始,注意索引从0开始 * @param maxResults 最多返回的数据条数 * @param map 参数键值对 * @return */ public List<T> list(String hql, int firstResult, int maxResults, Map<String, Object> map); public Query createQuery(String hql, Map<String, Object> map); /** * 查询实体 * @param hql * @param map * @return */ public List<T> list(String hql, Map<String, Object> map); /** * 获取记录总数 * @param hql * @param map * @return */ public int getTotalCount(String hql, Map<String, Object> map); }
3、实现Dao层接口
(1)getSession() throws CannotCreateTransactionException,当连接不上数据库时会抛出异常,可以在struts.xml配置异常页面
package com.life.dao.impl; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.springframework.transaction.CannotCreateTransactionException; import com.life.dao.IDao; public class DaoImpl<T> implements IDao<T> { private SessionFactory sessionFactory; @Override public T find(Class<T> clazz, int id) { // TODO Auto-generated method stub return (T) getSession().get(clazz, id); } @Override public void create(T t) { // TODO Auto-generated method stub getSession().persist(t); } @Override public void save(T t) { // TODO Auto-generated method stub getSession().saveOrUpdate(t); } @Override public void delete(T t) { // TODO Auto-generated method stub getSession().delete(t); } @Override public List<T> list(String hql, Map<String, Object> map) { // TODO Auto-generated method stub Query query = createQuery(hql, map); List<T> list = query.list(); return list; } @Override public int getTotalCount(String hql, Map<String, Object> map) { // TODO Auto-generated method stub Query query = createQuery(hql, map); Object obj = query.uniqueResult(); return ((Long) obj).intValue(); } @Override public List<T> list(String hql, int firstResult, int maxResults, Map<String, Object> map) { // TODO Auto-generated method stub Query query = createQuery(hql, map); List<T> list = query.setFirstResult(firstResult) .setMaxResults(maxResults).list(); System.out.println("起始:"+firstResult); System.out.println("最大值:"+maxResults); System.out.println("实际值:"+list.size()); return list; } public Session getSession() throws CannotCreateTransactionException{ return sessionFactory.getCurrentSession(); } public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override public Query createQuery(String hql, Map<String, Object> map) { Query query = getSession().createQuery(hql); // TODO Auto-generated method stub if (map != null) { Set<String> keySet = map.keySet(); for (String string : keySet) { Object obj = map.get(string); //这里考虑传入的参数是什么类型,不同类型使用的方法不同 if(obj instanceof Collection<?>){ query.setParameterList(string, (Collection<?>)obj); }else if(obj instanceof Object[]){ query.setParameterList(string, (Object[])obj); }else{ query.setParameter(string, obj); } } } return query; } }
Done
Hibernate之实体类设计基本步骤
时间: 2024-10-10 01:51:53