层接口定义IBaseDao.java
package dao;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import org.hibernate.Session;
/**
* Dao层基本操作接口定义
*
* @author Administrator
*
* @param <T>
* @param <K>
*/
public interface IBaseDao<T, K extends Serializable> {
/**
* 获取当前session对象
*
* @return
*/
public Session getSession();
/**
* 将实体对象保存到数据库中
*
* @param t
* 待保存的实体对象
* @return 实体对象的ID
*/
public K save(T t);
/**
* 将实体对象【集合】保存到数据库中
*
* @param ct
* 实体对象【集合】
*/
public void saveAll(Collection<T> ct);
/**
* 根据Id查询实体对象
*
* @param id
* 表记录中的对应的id字段
* @return 对应的实体对象
*/
public T findById(K id);
/**
* 更新一条记录(未注入属性值默认为null)
*
* @param t
* 待更新记录对应的实体对象
* @return 更新后的实体对象
*/
public T update(T t);
/**
* 删除一个实体对象对应的表记录
*
* @param t
* 待删除的实体对象
*/
public void delete(T t);
/**
* 删除一组记录
*
* @param ct
* 待删除记录集合
*/
public void deleteAll(Collection<T> ct);
/**
* 根据id删除一条记录
*
* @param id
* 待删除记录id
* @return 是否删除成功(id是否有效)
*/
public boolean deleteById(K id);
/**
* 加载所有记录集合
*
* @return 所有记录集合
*/
public List<T> loadAll();
/**
* 分页加载记录集合
*
* @param page
* 当前第多少页
* @param rows
* 每页最多多少行数据
* @return 第page页的数据集合
*/
public List<T> pages(int page, int rows);
/**
* 分页加载记录集合
*
* @param page
* 当前第多少页
* @param rows
* 每页最多多少行数据
* @param propertyName
* 分页参数,对应实体类属性
* @param orderType
* 排序方式,"asc"为升序,"desc"为降序
* @return 第page页的数据集合
*
*/
public List<T> pages(int page, int rows, String propertyName, String orderType);
/**
* 获取总记录数
*
* @return 总数
*/
public long getTotalCount();
}
BaseDao实现类BaseDaoImpl.java
package dao;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import javax.transaction.Transactional;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import model.User;
@Transactional
public class BaseDaoImpl<T, K extends Serializable> implements IBaseDao<T, K> {
private Class<T> clatt;
public BaseDaoImpl() {
}
public BaseDaoImpl(Class<T> clatt) {
this.clatt = clatt;
}
@Override
public Session getSession() {
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session session = sf.openSession();
return session;
}
@Override
public K save(T t) {
Session session = getSession();
session.beginTransaction();
System.out.println(((User) t).getPw());
@SuppressWarnings("unchecked")
K k = (K) session.save(t);
session.getTransaction().commit();
session.close();
return k;
}
@Override
public void saveAll(Collection<T> ct) {
Session session = getSession();
session.beginTransaction();
for (T t : ct) {
session.save(t);
}
session.getTransaction().commit();
session.close();
}
@Override
public T findById(K id) {
Session session = getSession();
session.beginTransaction();
T t = (T) (session.get(clatt, id));
session.getTransaction().commit();
session.close();
return t;
}
@Override
public T update(T t) {
Session session = getSession();
session.beginTransaction();
session.update(t);
session.getTransaction().commit();
session.close();
return t;
}
@Override
public void delete(T t) {
Session session = getSession();
session.beginTransaction();
session.delete(t);
session.getTransaction().commit();
session.close();
}
@Override
public void deleteAll(Collection<T> ct) {
Session session = getSession();
session.beginTransaction();
for (T t : ct) {
session.delete(t);
}
session.getTransaction().commit();
session.close();
}
@Override
public boolean deleteById(K id) {
Session session = getSession();
session.beginTransaction();
T t = (T) (session.get(clatt, id));
if (t != null) {
session.getTransaction().commit();
session.close();
return false;
} else {
session.getTransaction().rollback();
session.close();
return false;
}
}
@SuppressWarnings("unchecked")
@Override
public List<T> loadAll() {
Session session = getSession();
return session.createQuery("from " + clatt.getSimpleName()).list();
}
public Class<?> getClatt() {
return this.getClass();
}
public void setClatt(Class<T> clatt) {
this.clatt = clatt;
}
@Override
public List<T> pages(int page, int rows) {
return pages(page, rows, null, null);
}
@Override
public List<T> pages(int page, int rows, String propertyName, String orderType) {
Session session = getSession();
@SuppressWarnings("deprecation")
Criteria cr = session.createCriteria(clatt);
if (propertyName != null && orderType != null) {
if (orderType.equals("desc"))
cr.addOrder(Order.desc(propertyName));
else
cr.addOrder(Order.asc(propertyName));
}
cr.setFirstResult((page - 1) * rows);
cr.setMaxResults(rows);
@SuppressWarnings("unchecked")
List<T> results = cr.list();
session.close();
return results;
}
@Override
public long getTotalCount() {
Session session = getSession();
@SuppressWarnings("deprecation")
Criteria criteria = session.createCriteria(clatt);
Object object = criteria.setProjection(Projections.rowCount()).uniqueResult();
long totalCount = 0;
if (object != null) {
totalCount = Long.parseLong(object.toString());
}
return totalCount;
}
}
UserDao接口定义:IUserDao.java
package dao;
import model.User;
public interface IUserDao extends IBaseDao<User,Integer>{
}
UserDao接口实现:UserDaoImpl.java
package dao;
import model.User;
public class UserImpl extends BaseDaoImpl<User, Integer> implements IUserDao {
public UserImpl() {
super(User.class);
}
}