SSH整合之个人博客项目开发-特点

包层结构如下

稍详细:

首先从dao层就可以看到只有简单的一个basedao和basedaoimpl类。不再为每一个实体类各自创建一个一dao层的操作了。不仅用起来简便也减少了许多代码量。

从代码中,你现在可能已经注意到另一个知识点了,注解!因为在service层我会用到basedao,所以在basedao进行注解,在service层需要注册basedao

在basedao的代码如下:

package com.dao;

import java.io.Serializable;
import java.util.List;


/**
 * 基础数据库操作类
 * @author Pan
 *
 */
public interface BaseDao<T> {
	/**
	 * 原生sql
	 * @param sql
	 * @return
	 */
	
	public List queryBySql(String sql) ;
	/**
	 * 保存一个对象
	 * @param o
	 * @return
	 */
  public Serializable save(T o);
  /**
   * 删除一个对象
   * @param o
   */
  public void delete(T o);
  /**
   * 更新一个对象
   * @param o
   */
  public void update(T o);
  /**
   * 保存或更新一个对象
   * @param o
   */
  public void saveOrUpdate(T o);
  
  /**
   * 查询
   * @param hql
   * @return
   */
  public List<T> find(String hql);
  
  public List<T> find(String hql,Object[] param);
  
  public List<T> find(String hql,List<Object> param);
  
  /**
   * 带分页的查找
   * @param hql
   * @param param
   * @param page
   * @param row
   * @return
   */
  public List<T> find(String hql,Object[] param ,Integer page,Integer row);
  
  public List<T> find(String hql, List<Object> param, Integer page, Integer rows);
  
  /**
   * 获得一个对象
   * @param c
   * @param id
   * @return
   */
  public T get(Class<T> c,Serializable id);
  
  public T get(String hql,Object[] param);
  
  public T get(String hql,List<Object>param);
  
  /**
   * select count(*) from 类
   * @param hql
   * @return
   */
  public Long count(String hql);
  
  public Long count(String hql,Object[] param);
  
  public Long count(String hql,List<Object> param);
  
  /**
   * 执行HQL语句
   * @param hql
   * @return 返回响应数目
   */
  public Integer executeHql(String hql);
  
  public Integer executeHql(String hql,Object[] param);
  
  public Integer executeHql(String hql,List<Object> param);
  
}

basedaoimpl的代码如下:

package com.dao;

import java.io.Serializable;
import java.util.List;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


@Transactional
@Repository("baseDao")
public class BaseDaOImpl<T> implements BaseDao<T> {

	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

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

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	/**
	 * 原生sql
	 */
	public List queryBySql(String sql) {
		List<Object[]> list = getCurrentSession().createSQLQuery(sql).list();    
	        return  list;
	}
	
	public Serializable save(T o) {
		return this.getCurrentSession().save(o);
	}

	public void delete(T o) {
		this.getCurrentSession().delete(o);
	}

	public void update(T o) {
		this.getCurrentSession().update(o);
	}

	public void saveOrUpdate(T o) {
		this.getCurrentSession().saveOrUpdate(o);
	}

	public List<T> find(String hql) {
		return this.getCurrentSession().createQuery(hql).list();
	}

	public List<T> find(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.list();
	}

	public List<T> find(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.list();
	}

	public List<T> find(String hql, Object[] param, Integer page, Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
		
	}

	public List<T> find(String hql, List<Object> param, Integer page, Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
		
	}

	public T get(Class<T> c, Serializable id) {
		return (T) this.getCurrentSession().get(c, id);
	}

	public T get(String hql, Object[] param) {
		List<T> l = this.find(hql, param);
		if (l != null && l.size() > 0) {
			return l.get(0);
		} else {
			return null;
		}
	}

	public T get(String hql, List<Object> param) {
		List<T> l = this.find(hql, param);
		if (l != null && l.size() > 0) {
			return l.get(0);
		} else {
			return null;
		}
	}

	public Long count(String hql) {
		return (Long) this.getCurrentSession().createQuery(hql).uniqueResult();
	}

	public Long count(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return (Long) q.uniqueResult();
	}

	public Long count(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return (Long) q.uniqueResult();
	}

	public Integer executeHql(String hql) {
		return this.getCurrentSession().createQuery(hql).executeUpdate();
	}

	public Integer executeHql(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.executeUpdate();
	}

	public Integer executeHql(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.executeUpdate();
	}

}

猜你喜欢

转载自blog.csdn.net/lp15203883326/article/details/80337295