- package
org.lc.hibernate; - import
java.io.Serializable; - import
java.util.List; - import
org.hibernate.HibernateException; - import
org.hibernate.Query; - import
org.hibernate.Session; - import
org.hibernate.Transaction; - import
org.lc.fatory.HibernateSessionFactory; -
- public
class Encapsulation<Pojo> { -
-
public void insertObject(Pojo pojo) -
{ -
Session session = HibernateSessionFactory.getSession(); -
Transaction transaction = session.beginTransaction(); -
try { -
session.save(pojo); -
transaction.commit(); -
} catch (HibernateException e) { -
e.printStackTrace(); -
transaction.rollback(); -
}finally{ -
HibernateSessionFactory.closeSession(); -
} -
} -
-
-
public void updateObject(Pojo pojo) -
{ -
Session session = HibernateSessionFactory.getSession(); -
Transaction transaction = session.beginTransaction(); -
try { -
session.update(pojo); -
transaction.commit(); -
} catch (HibernateException e) { -
e.printStackTrace(); -
transaction.rollback(); -
}finally{ -
HibernateSessionFactory.closeSession(); -
} -
} -
-
public void deleteObject(Pojo pojo) -
{ -
Session session = HibernateSessionFactory.getSession(); -
Transaction transaction = session.beginTransaction(); -
try { -
session.delete(pojo); -
transaction.commit(); -
} catch (HibernateException e) { -
e.printStackTrace(); -
transaction.rollback(); -
}finally{ -
HibernateSessionFactory.closeSession(); -
} -
} -
-
-
public Pojo loadObject(Class<Pojo> clazz, Serializable id) -
{ -
Session session = HibernateSessionFactory.getSession(); -
Transaction transaction = session.beginTransaction(); -
try { -
Pojo p = (Pojo) session.load(clazz, id); -
transaction.commit(); -
return p; -
} catch (HibernateException e) { -
e.printStackTrace(); -
transaction.rollback(); -
} -
return null; -
} -
-
public List queryObject(String hql, Object...objects) -
{ -
Session session = HibernateSessionFactory.getSession(); -
Transaction transaction = session.beginTransaction(); -
try { -
Query query = session.createQuery(hql); -
this.bindParameters(query, objects); -
List list = query.list(); -
transaction.commit(); -
-
return list; -
} catch (HibernateException e) { -
e.printStackTrace(); -
transaction.rollback(); -
}finally{ -
HibernateSessionFactory.closeSession(); -
} -
return null; -
} -
-
-
public List queryObjectPage(String hql, int curpage, int pagesize, Object...objects) -
{ -
Session session = HibernateSessionFactory.getSession(); -
Transaction transaction = session.beginTransaction(); -
try { -
Query query = session.createQuery(hql); -
this.bindParameters(query, objects); -
-
query.setFirstResult((curpage - 1) * pagesize); -
query.setMaxResults(pagesize); -
List list = query.list(); -
-
transaction.commit(); -
return list; -
} catch (HibernateException e) { -
e.printStackTrace(); -
transaction.rollback(); -
}finally{ -
HibernateSessionFactory.closeSession(); -
} -
return null; -
} -
-
-
public Integer getPageSize(String hql, Object...objects) -
{ -
Session session = HibernateSessionFactory.getSession(); -
Transaction transaction = session.beginTransaction(); -
try { -
Query query = session.createQuery(hql); -
this.bindParameters(query, objects); -
Object object = query.uniqueResult(); -
transaction.commit(); -
-
return (Integer) object; -
} catch (HibernateException e) { -
e.printStackTrace(); -
transaction.rollback(); -
}finally{ -
HibernateSessionFactory.closeSession(); -
} -
return 0; -
} -
-
-
public List namedQuery(String namedHql, Object...objects) -
{ -
Session session = HibernateSessionFactory.getSession(); -
Transaction transaction = session.beginTransaction(); -
try { -
Query query = session.getNamedQuery(namedHql); -
this.bindParameters(query, objects); -
List list = query.list(); -
transaction.commit(); -
return list; -
} catch (HibernateException e) { -
e.printStackTrace(); -
transaction.rollback(); -
}finally{ -
HibernateSessionFactory.closeSession(); -
} -
return null; -
} -
-
protected void bindParameters(Query query, Object...objects) -
{ -
if(objects == null) return; -
int i = 0; -
for(Object o : objects) -
{ -
query.setParameter(i ++, o); -
} -
} - }
封装Hibernate对数据库的操作,包括: 添加,删除,修改,加载,查询,分页查询,命名查询
最新推荐文章于 2022-01-12 11:47:48 发布