Hibernate封装类
来源:互联网 发布:淘宝限制购买怎么解决 编辑:程序博客网 时间:2024/05/20 07:36
Hibernate封装类
2011-08-06 17:43 8495人阅读 评论(6) 收藏 举报
hibernatesessionexceptionpropertiesobjectnull
最近研究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);
- }
- }
0 0
- Hibernate封装类
- Hibernate封装类
- Hibernate封装类
- 封装Hibernate工具类
- hibernate的封装工具类
- hibernate封装
- 封装hibernate
- hibernate DAO类的封装范文
- 封装Hibernate的SessionFactory类介绍
- Hibernate封装好了的工具类
- Java Hibernate常用方法封装工具类
- Hibernate之封装utils工具类
- 封装Hibernate数据库操作方法
- SpringSide的Hibernate封装
- hibernate query分页封装
- hibernate底层dao封装
- spring hibernate custom 封装
- Hibernate封装通用数据操作基类DAO
- json数据与JAVA数据的转换
- 用已有16M文件系统制作32M文件系统,修改 uboot 参数
- java事务大总结(一) 先理解数据库的事务以mysql为例
- 网站重构——轻量化的网站架构设计三,angular restify
- python server android
- Hibernate封装类
- Atlas源码剖析(一)
- SEO优化步骤
- javascript eval和JSON之间的联系
- 使用Android Studio遇到的问题及解决过程
- 新手学习Cadence16.3笔记
- 16进制转化为8进制
- thrift使用示例二
- usaco training 6.1.3 Cow XOR 题解