dao 层封装(利用spring + hibernate)
程序员文章站
2022-04-18 09:31:58
...
接口 baseDao
package com.dx.dao;
import java.io.Serializable;
import java.util.List;
import org.hibernate.SQLQuery;
public interface BaseDao {
public void save(Object object);
public void update(Object object);
public void delete(Object object);
public <T> T get(Class<T> type,Serializable id);
public List<?> query(String hql);
public List<?> query(String hql,int pageNum,int pageSize);
public List<?> queryBySql(String sql);
public List<?> queryBySql(String sql,int pageNum,int pageSize);
}
baseDaoImpl
package com.dx.dao.impl;
import java.io.Serializable;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.dx.dao.BaseDao;
public class BaseDaoImpl extends HibernateDaoSupport implements BaseDao{
private SessionFactory sessionFactory;
public void setSuperSessionFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
@Override
public void save(Object object) {
this.getHibernateTemplate().save(object);
}
@Override
public void update(Object object) {
this.getHibernateTemplate().update(object);
}
@Override
public void delete(Object object) {
this.getHibernateTemplate().delete(object);
}
@Override
public <T> T get(Class<T> type, Serializable id) {
return this.getHibernateTemplate().get(type, id);
}
@Override
public List<?> query(String hql) {
return this.getHibernateTemplate().find(hql);
}
@Override
public List<?> query(String hql, int pageNum, int pageSize) {
List<?> list=null;
Session session = this.getSession();
Transaction transaction = session.beginTransaction();
transaction.begin();
Query query = session.createQuery(hql);
query.setFirstResult(pageNum*pageSize);
query.setMaxResults(pageSize);
list = query.list();
transaction.commit();
session.close();
return list;
}
@Override
public List<?> queryBySql(String sql) {
List<?> list=null;
Session session = this.getSession();
Transaction transaction = session.beginTransaction();
transaction.begin();
SQLQuery sqlQuery = session.createSQLQuery(sql);
list = sqlQuery.list();
transaction.commit();
session.close();
return list;
}
@Override
public List<?> queryBySql(String sql, int pageNum, int pageSize) {
List<?> list=null;
Session session = this.getSession();
Transaction transaction = session.beginTransaction();
transaction.begin();
SQLQuery query = session.createSQLQuery(sql);
query.setFirstResult(pageNum*pageSize);
query.setMaxResults(pageSize);
list = query.list();
transaction.commit();
session.close();
return list;
}
}
ok
上一篇: 支持10W无线充电:米物智能鼠标垫拆解
推荐阅读
-
利用spring mvc,hibernate重构系统(2) MVCHibernateSpringJSPJPA
-
利用spring mvc,hibernate重构系统(1) MVCSpringHibernateServletJSP
-
利用spring mvc,hibernate重构系统(2) MVCHibernateSpringJSPJPA
-
利用spring mvc,hibernate重构系统(1) MVCSpringHibernateServletJSP
-
利用Spring框架封装的JavaMail现实同步或异步邮件发送
-
dao层采用hibernate实现了,是否还需要扩展mybatis?
-
dao层采用hibernate实现了,是否还需要扩展mybatis?
-
mybatis+spring 省去dao实现层直接匹配mapper
-
利用java工程配置文件生成数据表的增删改查dao层(xml)
-
利用泛型封装通用的service层