package cn.itcast.dao;

import java.io.Serializable;
import java.util.List; /**
* BaseDao
* @author Administrator
*
* @param <T>
*/
public interface BaseDao<T> {
/**
* 保存一个对象
*
* @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); /**
* 查询集合
*
* @param hql
* @param param
* @return
*/
public List<T> find(String hql, Object[] param); /**
* 查询集合
*
* @param hql
* @param param
* @return
*/
public List<T> find(String hql, List<Object> param); /**
* 查询集合(带分页)
*
* @param hql
* @param param
* @param page
* 查询第几页
* @param rows
* 每页显示几条记录
* @return
*/
public List<T> find(String hql, Object[] param, Integer page, Integer rows); /**
* 查询集合(带分页)
*
* @param hql
* @param param
* @param page
* @param rows
* @return
*/
public List<T> find(String hql, List<Object> param, Integer page, Integer rows); /**
* 获得一个对象
*
* @param c
* 对象类型
* @param id
* @return Object
*/
public T get(Class<T> c, Serializable id); /**
* 获得一个对象
*
* @param hql
* @param param
* @return Object
*/
public T get(String hql, Object[] param); /**
* 获得一个对象
*
* @param hql
* @param param
* @return
*/
public T get(String hql, List<Object> param); /**
* select count(*) from 类
*
* @param hql
* @return
*/
public Long count(String hql); /**
* select count(*) from 类
*
* @param hql
* @param param
* @return
*/
public Long count(String hql, Object[] param); /**
* select count(*) from 类
*
* @param hql
* @param param
* @return
*/
public Long count(String hql, List<Object> param); /**
* 执行HQL语句
*
* @param hql
* @return 响应数目
*/
public Integer executeHql(String hql); /**
* 执行HQL语句
*
* @param hql
* @param param
* @return 响应数目
*/
public Integer executeHql(String hql, Object[] param); /**
* 执行HQL语句
*
* @param hql
* @param param
* @return
*/
public Integer executeHql(String hql, List<Object> param); }
package cn.itcast.daoImpl;

import java.io.Serializable;
import java.util.List; import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import cn.itcast.dao.BaseDao; @Repository("BaseDAO")
@SuppressWarnings("all")
/**
* BaseDaoImpl
* @author Administrator
*
* @param <T>
*/
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();
} 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();
} }
package cn.itcast.dao;

import cn.itcast.entity.TEmail;

/**
* EmailDao
* @author Administrator
*
*/ public interface TEmailDao {
public TEmail Login(String LoginName,String Password); public TEmail FindByEmail(String param); public Integer count(String param); public void saveOrUpdate(TEmail T); public void save(TEmail T); public void update(TEmail T); public TEmail get(Integer id);
}
package cn.itcast.daoImpl;

import cn.itcast.dao.BaseDao;
import cn.itcast.dao.TEmailDao;
import cn.itcast.entity.TEmail; /**
* EmailDao实现类调用BaseDao实现EmailDao中的方法
* @author Administrator
*
*/
public class TEmailDaoImpl implements TEmailDao {
private BaseDao<TEmail> baseDao; public BaseDao<TEmail> getBaseDao() {
return baseDao;
} public void setBaseDao(BaseDao<TEmail> baseDao) {
this.baseDao = baseDao;
} public TEmail Login(String LoginName,String Password) {
String hql="From TEmail where loginName=? and password=?";
return baseDao.get(hql, new Object[]{LoginName,Password});
} public TEmail FindByEmail(String param) {
String hql="From TEmail where email=?";
return baseDao.get(hql, new Object[]{param});
} public Integer count(String param) {
String hql="Select count(*) From TEmail where email=?";
Long s=baseDao.count(hql, new Object[]{param});
return s.intValue();
} public void saveOrUpdate(TEmail T){
baseDao.saveOrUpdate(T);
} public void save(TEmail T){
baseDao.save(T);
} public void update(TEmail T){
baseDao.update(T);
} public TEmail get(Integer id) {
return baseDao.get(TEmail.class, id);
} }

最新文章

  1. WPF:设置MenuItem多种不同状态图标
  2. 在做excel导出时如何将excel直接写在输出流中
  3. Android权限安全(1)自定义,检查,使用权限
  4. 如何使用SVN管理我们的源代码
  5. 转: Android开发中的MVP架构详解(附加链接比较不错)
  6. Oracle11g用户密码过期
  7. [转] Linux文件系统之hard link&amp;symbol link
  8. iOS Socket 整理以及CocoaAsyncSocket、SRWebSocket源码解析(一)
  9. 个性化推荐调优:重写spark推荐api
  10. 用户创建,删除and并发注册and系统登陆的API研究(学习汇总网上资料)
  11. hbase-0.92.1集群部署
  12. Callable抛出异常与future.get
  13. fastjson与各类型的转换
  14. jqGrid时间转换
  15. 数据格式XML、JSON详解
  16. The query below helps you to locate tables without a primary key:
  17. php之快速入门学习-8(if-else)
  18. goahead3.6.3就基本使用(后台上传信息到html页面),高手请忽略
  19. Boost asio基本概念
  20. 剑指 offer set 26 不用加减乘除做加法

热门文章

  1. Tame Me【驯服我】
  2. Scrapy框架中选择器的用法【转】
  3. winform中使用webBrowser时如何与JS交互
  4. Kali 网络配置
  5. appcompat_v7\res\values-v21\themes_base.xml:158: error: Error: No resource
  6. loj2537 「PKUWC 2018」Minimax
  7. 可拖动jquery插件
  8. Android 使用intent传递返回值:startActivityForResult()与onActivityResult()与setResult()参数分析,activity带参数的返回
  9. Mac进行一些操作时提醒Operation not permitted的完美解决
  10. springcloud 高可用分布式配置中心