SSH之Hibernate的Dao层和Service层

1、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 getQuery(String hql, Map<String, Object> map);

    /**
     * 查询实体
     * @param hql
     * @param map
     * @return
     */
    List<T> list(String hql, Map<String, Object> map);

    /**
     * 获取记录总数
     * @param hql
     * @param map
     * @return
     */
    int getTotalCount(String hql, Map<String, Object> map);
    
}

Dao层实现

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 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 = getQuery(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 = getQuery(hql, map);
        System.out.println("hql"+hql);
        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 = getQuery(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() {
        return sessionFactory.getCurrentSession();
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @Override
    public Query getQuery(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;  
    }

}

Service层接口

package com.life.service;

import java.util.List;
import java.util.Map;

/**
 * 这个父接口不能作为bean在applicationContext.xml中,因为serviceImpl是虚函数
 * 
 * @author JL Service接口,基本方法
 */
public interface IService<T> {

    /**
     * 根据id和对象类查找实体
     */
    public T find(Class<T> clazz, int id);

    /**
     * 创建实体
     */
    public void create(T t);

    /**
     * 保存实体
     */
    public void save(T t);

    /**
     * 删除实体
     */
    public void delete(T t);

    /**
     * 分页实体
     */
    public List<T> list(String hql, int firstResult, int maxResults,
            Map<String, Object> map);

    int getTotalCount(String hql, Map<String, Object> map);

    List<T> list(String hql, Map<String, Object> map);

    List<T> listPage(String hql, int page, int maxResults,
            Map<String, Object> map);
}

 

Service层实现

package com.life.service.impl;

import java.util.List;
import java.util.Map;

import com.life.dao.IDao;
import com.life.service.IService;

/**
 * 由于各个实体类的create()方法有不同的业务逻辑(例如创建用户前需要检查账号是否已经存在),所以把类定义成abstract
 * 把create()方法定义成abstract,由具体的Service单独实现
 * @author JL
 */
public abstract class ServiceImpl<T> implements IService<T>{

    /**
     * 通过spring的IoC注射进来
     */
    protected IDao<T> dao; 
    
    public IDao<T> getDao() {
        return dao;
    }

    public void setDao(IDao<T> dao) {
        this.dao = dao;
    }

    @Override
    public T find(Class<T> clazz, int id) {
        // TODO Auto-generated method stub
        return dao.find(clazz, id);
    }
    
    @Override
    public void create(T t){
        dao.create(t);
    }; 
    
    @Override
    public void save(T t) {
        // TODO Auto-generated method stub
        dao.save(t);
    }

    @Override
    public void delete(T t) {
        // TODO Auto-generated method stub
        dao.delete(t);
    }

    @Override
    public List<T> list(String hql, Map<String, Object> map) {
        // TODO Auto-generated method stub
        return dao.list(hql, map);
    }
    
    @Override
    public int getTotalCount(String hql, Map<String, Object> map) {
        // TODO Auto-generated method stub
        return dao.getTotalCount(hql, map);
    }

    @Override
    public List<T> list(String hql, int firstResult, int maxResults,
            Map<String, Object> map) {
        // TODO Auto-generated method stub
        return dao.list(hql, firstResult, maxResults, map);
    }

    @Override
    public List<T> listPage(String hql, int page, int maxResults,
            Map<String, Object> map) {
        // TODO Auto-generated method stub
        return dao.list(hql, maxResults*(page-1), maxResults, map);

    }
}

 

Done

SSH之Hibernate的Dao层和Service层,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。