最近研究Hibernate,写了一个对Hibernate数据访问的一个封装,现在贴出来,供大家分析讨论。
- package com.lynn.db.dao;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class BaseDao {
- static Configuration conf = null;
- static SessionFactory factory = null;
- static Session session = null;
- static{
- try {
- conf = new Configuration().configure();
- factory = conf.buildSessionFactory();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 获得Session连接
- * */
- public static Session getSession(){
- session = factory.openSession();
- return session;
- }
- /**
- * 关闭session连接
- * */
- public static void closeSession(){
- if(session!=null)
- session.close();
- }
- }
下面是封装的增、删、改、查
- package com.lynn.db.dao;
- import java.util.List;
- import org.hibernate.Criteria;
- import org.hibernate.FetchMode;
- import org.hibernate.Session;
- import org.hibernate.Transaction;
- import org.hibernate.criterion.Order;
- import org.hibernate.criterion.Restrictions;
- import com.lynn.page.PageInfo;
- public class HibernateDao {
- protected Session session = null;
- protected Transaction ts = null;
- protected Criteria criteria;
- /**
- * session保存方法
- * */
- public boolean save(Object obj){
- boolean check = false;
- try {
- session = BaseDao.getSession();//获得连接
- ts = session.beginTransaction();//创建事务
- session.save(obj);//保存对象
- ts.commit();//提交事务
- check = true;
- } catch (Exception e) {
- e.printStackTrace();
- ts.rollback();//出现异常则回滚事务
- }
- return check;
- }
- /**
- * session删除方法
- * */
- public boolean delete(Object obj){
- boolean check = false;
- try {
- session = BaseDao.getSession();
- ts = session.beginTransaction();
- session.delete(obj);
- ts.commit();
- check = true;
- } catch (Exception e) {
- e.printStackTrace();
- ts.rollback();
- }
- return check;
- }
- /**
- * session修改方法
- * */
- public boolean update(Object obj){
- boolean check = false;
- try {
- session = BaseDao.getSession();
- ts = session.beginTransaction();
- session.delete(obj);
- ts.commit();
- check = true;
- } catch (Exception e) {
- e.printStackTrace();
- ts.rollback();
- }
- return check;
- }
- /**
- * session查询方法(抓取)查询所有
- * @return Criteria
- * */
- public Criteria QueryByFetch(String...fetch){
- try {
- if(fetch!=null && fetch.length>0){
- for (int i = 0; i < fetch.length; i++) {
- criteria = criteria.setFetchMode(fetch[i], FetchMode.JOIN);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- return criteria;
- }
- /**
- * 根据单条件或者多条件查询
- * 至少2个参数,第一个为属性名,第二个为属性值
- * @return Criteria
- * */
- public Criteria QueryByProperties(Object...properties){
- try {
- if(properties!=null && properties.length>0){
- for (int i = 0; i < properties.length; i=i+2) {
- criteria = criteria.add(Restrictions.like((String)properties[i], properties[i+1]));
- }
- }
- return criteria;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 查询所有
- * */
- public Criteria Query(Class c){
- session = BaseDao.getSession();
- criteria = session.createCriteria(c);
- return criteria;
- }
- /**
- * 将查询结果返回List
- * @return List
- * */
- public List QueryList(){
- try {
- return criteria.list();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 查询单个数据
- * @return Object
- * */
- public Object QueryUnique(){
- try {
- return criteria.uniqueResult();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 此类专门用于分页
- * */
- public Criteria QueryByPage(PageInfo pageInfo){
- return criteria.setFirstResult((pageInfo.getPageIndex()-1)* pageInfo.getPageSize())
- .setMaxResults(pageInfo.getPageSize());
- }
- /**
- * 此类专门用于排序
- * */
- public Criteria QueryByOrder(Order o){
- return criteria.addOrder(o);
- }
- }