码迷,mamicode.com
首页 > 系统相关 > 详细

Hibernate之实体类设计基本步骤

时间:2014-08-11 17:18:12      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   使用   io   数据   for   

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之实体类设计基本步骤,布布扣,bubuko.com

Hibernate之实体类设计基本步骤

标签:style   blog   color   java   使用   io   数据   for   

原文地址:http://www.cnblogs.com/xingyyy/p/3904860.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!