package com.dayang.common.basic;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import java.util.Map;
/**
* <p>Title:BasicDAO</p>
* <p>project: MIS<p>
* <p>Description: BasicDAO</p>
* <p>Copyright: Copyright (c) 2007 dayang</p>
* <p>Company: dayang</p>
* @author zhangxu
* @version 0.1 26/10/07
*/
public class BasicDAO extends HibernateDaoSupport{
private JdbcTemplate jdbcTemplate;
/**
* 批量更新对象
* 2016-5-5下午03:43:59,
*/
public void batchUpdate(String hql){
Session session=this.getSession();
Transaction tx=session.beginTransaction();
session.createQuery(hql).executeUpdate();
tx.commit();
}
/**
* 批量保存或更新对象
* 2015-10-20下午02:57:26,合同管理系统V1.3.8
*/
public void saveOrUpdateAllObjects(Collection<Object> collection){
this.getHibernateTemplate().saveOrUpdateAll(collection);
}
/**
* map存放每个更新对象sql语句中对应的占位符,list为map对象列表
* 2019-1-8下午4:37:34,
*/
public void batchUpdateAllObjects(final List<Map<Integer, Object>> list,String sql){
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
@Override
public int getBatchSize() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public void setValues(PreparedStatement pstm, int i)
throws SQLException {
Map<Integer,Object> map=list.get(i);
if (map!=null) {
for(Map.Entry<Integer, Object> entry : map.entrySet()){
pstm.setObject(entry.getKey(),entry.getValue());
}
}
}
});
}
/**
* 根据参数查询列表
* 2015-10-19下午04:51:07,,合同管理系统V1.3.8
*/
public List searchByHqlWithParam(final Map<Integer,Object> map,final String hql){
List list=new ArrayList();
if (map.size() > 0) {
list = (List) this.getHibernateTemplate().execute(
new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(hql);
for (Map.Entry<Integer, Object> entry : map.entrySet()) {
query.setParameter(entry.getKey(), entry.getValue());
}
return query.list();
}
});
} else {
list = this.searchByHQL(hql);
}
return list;
}
public void saveObject(Object obj) {
this.getHibernateTemplate().save(obj);
}
public Object saveObjectWithReturn(Object obj) {
this.getHibernateTemplate().save(obj);
return obj;
}
public Object searchObjById(Class name, Integer id) {
return this.getHibernateTemplate().get(name,id);
}
public void updateObj(Object obj) {
this.getHibernateTemplate().update(obj);
}
public List searchByHQL(String hql) {
return this.getHibernateTemplate().find(hql);
}
public void deleteObj(Object obj) {
this.getHibernateTemplate().delete(obj);
}
public void deleteObj(Class name, Integer id) {
Object o = this.getHibernateTemplate().get(name,id);
if(o!=null)
this.getHibernateTemplate().delete(o);
}
public void deleteObjects(List objs) {
this.getHibernateTemplate().deleteAll(objs);
}
public void deleteByHql(final String hql){
this.getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session) throws HibernateException,SQLException {
Query query = session.createQuery(hql);
int count = query.executeUpdate();
return count;
}
});
}
public void updateByHql(final String hql){
this.getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session) throws HibernateException,SQLException {
Query query = session.createQuery(hql);
query.executeUpdate();
return "";
}
});
}
//OA-V1.3.0 by 20140609
public List searchByHQLPage(final String hql,final Integer offset,final Integer length) {
return (List<Object>) getHibernateTemplate().execute(new HibernateCallback() {
public List<Object> doInHibernate(Session session) throws HibernateException {
Query query = session.createQuery(hql);
query.setFirstResult(offset);
query.setMaxResults(length);
List<Object> results = query.list();
return results;
}
});
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}