/**
* @param obj
* @return
* 添加数据
*/
public Serializable
saveObject(Object obj){
return
this.getHibernateTemplate().save(obj);
}
/**
* @param obj
* 修改数据
*/
public void uodateObject(Object
obj){
this.getHibernateTemplate().update(obj);
}
public void
deleteObject(Object obj){
this.getHibernateTemplate().delete(obj);
}
/**
* @param obj
*
有id号就会看作为修改数据集,没有就会看作为添加数据
*/
public void saveOrUpdate(Object
obj){
this.getHibernateTemplate().saveOrUpdate(obj);
}
/**
* @param cls
* @param id
* @return 当对象的为空的时候用get方法去查询返回null
*/
public Object get(Class cls,Serializable id){
return
this.getHibernateTemplate().get(cls, id);
}
/**
* @param cls
*
@param id
* @return 当查询对象为空的时候就会报异常(这个方法一开始就认为一定有数据)
*/
public Object
load(Class cls,Serializable id){
return
this.getHibernateTemplate().load(cls, id);
}
/**
* @param cls
* @return
* 查询所有
*/
public List loadAll(Class cls){
return
this.getHibernateTemplate().loadAll(cls);
}
/**
* @param
hql
* @param params
* @return
* 代条件查询
*/
public List
findByHql(String hql,Object[] params){
return
this.getHibernateTemplate().find(hql,params);
}
/**
* @param
sql
* @param params
* @return
*/
public List findBySql(final
String sql,final Object[] params){
return
this.getHibernateTemplate().executeFind(new HibernateCallback()
{
@Override
public Object doInHibernate(Session session) throws
HibernateException,
SQLException {
SQLQuery
query=session.createSQLQuery(sql);
if(params!=null&&
params.length>0){
for(int
i=0;i<params.length;i++){
Object
object=params[i];
query.setParameter(i,object);
}
}
return
query.list();
}
});
}
/**
* @param sql
*sql
删除/修改 数据
*/
public void deleteSql(final String sql){
try
{
this.getHibernateTemplate().execute(new HibernateCallback()
{
@Override
public Object doInHibernate(Session session)
throws HibernateException,
SQLException {
SQLQuery
query=session.createSQLQuery(sql);
return
query.executeUpdate();
}
});
} catch (Exception e)
{
e.printStackTrace();
}
}
/**
* @param sql
* 原始更改数据回滚和提交数据(spring事物配置取代)
*/
public void UpdateInfo(String
sql){
Session session =null;
Transaction tx=null;
try
{
session=this.getHibernateTemplate().getSessionFactory().openSession();
tx=session.beginTransaction();
tx.commit();
}
catch (Exception e)
{
tx.rollback();
e.printStackTrace();
}finally{
if(session!=null)
session.close();
}
}
/**
* @param hql
*
@param pager
* @return hql分页查询结果集
*/
public List allHqlList(String
hql,Pager pager){
return
this.getSession().createQuery(hql).setFirstResult((pager.getCurrentPage()
-1)*pager.getPageSize()).setMaxResults(pager.getPageSize()).list();
}
/**
* @param sql
* @param pager
* @return sql分页查询结果集
*/
public List
allSqlList(String sql,Pager pager){
return
this.getSession().createSQLQuery(sql).setFirstResult((pager.getCurrentPage()-1)*pager.getPageSize()).setMaxResults(pager.getPageSize()).list();
}
/**
* @param hql
* @return 拿到查询语句的数据总数
*/
public Integer totleSize(String
hql){
int str=hql.indexOf("from");
hql="select count(*)
"+hql.substring(str,hql.length());
return
(Integer)this.getSession().createQuery(hql).uniqueResult();
}
/**
* @param hql
* @param pager
* @return 填充pager对象信息
*/
public
Pager queryPager(String hql,Pager pager){
int
maxpage,currentPage=0;
int
totelSize=this.totleSize(hql);
pager.setTotalRows(totelSize);//总过信息条数
currentPage=pager.getCurrentPage()==0
?
1:pager.getCurrentPage();
pager.setCurrentPage(currentPage);
//显示页数
if(totelSize%pager.getPageSize()==0)
maxpage=totelSize/pager.getPageSize();
else
maxpage=totelSize/pager.getPageSize()+1;
pager.setTotalPages(maxpage);//最大页数
pager.setResults(this.allHqlList(hql,
pager));
return pager;
}
/**
* 批量更新/删除(Bulk
update/delete)
public void batchDelete(String name, String newName) {
this.hibernateTemplate.bulkUpdate("update Teacher set name=? where name=?",
new Object[]{newName, name});
*/
public void bulkUpdate(String
queryString,Object[] objs){
try
{
this.getHibernateTemplate().bulkUpdate(queryString,objs);
} catch
(Exception e)
{
e.printStackTrace();
}
}
//查询语句
//select b from
book b left join fetch b.autohr where b.id=148;
//select b from Author a
left join fethc a.books;
//select a.name,count(b) from Author a left join
a.books b group by a.id order by count(b) desc;
//select a from Author a
where a.id in (select b.author.id form Book b where b.name like
‘%a%‘);
/**
* @param list
* @return
*
把集合当作查询条件
*/
public List queryList1(List list){
String hql="from
Book where id in(:ids)";
return
getSession().createQuery(hql).setParameterList("ids",
list).list();
}
/**
* @return
* 占位符修改数据
*/
public
List queryList2(){
String hql = "update Book set name=? where
id=?";
return getSession().createQuery(hql)
.setParameter(0,
"hibernate")
.setParameter(1, 109)
.list();
}
//以下是分页Pager对象属性
public class Pager<E> {
private int totalRows=0;//总行数数
private
int pageSize=25;//每页显示多少条
private int currentPage=1;//当前页数
private int
totalPages=0;//最大页数
private List<E> results;//查询结果集
public int
getTotalRows() {
return totalRows;
}
public void setTotalRows(int
totalRows) {
this.totalRows = totalRows;
}
public int getPageSize()
{
return pageSize;
}
public void setPageSize(int pageSize)
{
this.pageSize = pageSize;
}
public int getCurrentPage()
{
return currentPage;
}
public void setCurrentPage(int currentPage)
{
this.currentPage = currentPage;
}
public int getTotalPages()
{
return totalPages;
}
public void setTotalPages(int totalPages)
{
this.totalPages = totalPages;
}
public List<E> getResults()
{
return results;
}
public void setResults(List<E> results)
{
this.results = results;
}
}