package com.soft.common;
import java.io.Serializable;
import java.util.List;
import com.soft.Exception.CommonException;
import com.soft.query.HqlExecute;
/**
*
* @author 通用接口
*/
public interface ICommon<POJO> {
/**
* 添加对象
*
* @param Object
* @throws CommonException
*/
public abstract void insert(POJO obj) throws CommonException;
/**
* 修改对象
*
* @param Object
* @throws CommonException
*/
public abstract void update(POJO obj) throws CommonException;
/**
* 删除对象
*
* @param object
* @throws CommonException
*/
public abstract void delete(POJO obj) throws CommonException;
/**
* 根据主键查询对象
*
* @param Class
* @param id
* @return Object
* @throws CommonException
*/
public abstract POJO getById(Class cl, Serializable id)
throws CommonException;
/**
* 根据主键删除对象
*
* @param Class
* @param id
* @throws CommonException
*/
public abstract void deleteById(Class cl, Serializable id)
throws CommonException;
/**
* 批量删除
*
* @param Class
* @param serializables(id数组)
* @throws CommonException
*/
public abstract void deletebatch(Class cl, Serializable... serializables)
throws CommonException;
/**
* 总记录条数
*
* @param Class
* @return int
* @throws CommonException
*/
public abstract int gettotal(Class cl) throws CommonException;
/**
* 总记录条数
*
* @param Class 类名
* @prama String 状态属性名
* @return int
* @throws CommonException
*/
public abstract int gettotal(Class cl,String state) throws CommonException;
/**
* 总页数,总记录
*
* @param Class
* @param pagesize
* @return int[]
* @throws CommonException
*/
public abstract int[] getpagetotal(Class cl, int pagesize)throws CommonException;
/**
* 总页数,总记录
* @param Class 类名
* @prama String 状态属性名
* @param pagesize ��大小
* @return int[]
* @throws CommonException
*/
public abstract int[] getpagetotal(Class cl,String state, int pagesize)throws CommonException;
/**
* 查询
* @param hqlexe
* @return
* @throws CommonException
*/
public abstract List query(HqlExecute hqlexe) throws CommonException;
}
-----------------------------------------------------------------------------------------------------------------
package com.soft.common;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.soft.Exception.CommonException;
import com.soft.hbm.HibernateSessionFactory;
import com.soft.query.HqlExecute;
import com.soft.query.QueryException;
/**
*
* @author 通用接口实现类
*/
public class Common<POJO> extends HibernateDaoSupport implements ICommon<POJO>{
/**
* 实现通用接口:删除
*/
public void delete(POJO obj) throws CommonException {
try {
this.getHibernateTemplate().delete(obj);
} catch (HibernateException e) {
e.printStackTrace();
throw new CommonException(e);
}
}
/**
* 实现通用接口:根据主键删除对象
*/
public void deleteById(Class cl, Serializable id) throws CommonException {
try {
this.getHibernateTemplate().delete(this.getHibernateTemplate().load(cl, id));
} catch (HibernateException e) {
e.printStackTrace();
throw new CommonException(e);
}
}
/**
* 实现通用接口:批量删除
*/
public void deletebatch(Class cl, Serializable... serializables)
throws CommonException {
try {
// 连接参数
String ins = "";
for (Serializable a : serializables) {
ins += a + ",";
}
if (ins.endsWith(",")) {
ins = ins.substring(0, ins.length() - 1);
}
final StringBuffer sb = new StringBuffer("delete from ").append(
cl.getSimpleName()).append(" as a where a.id in(").append(
ins).append(")");
this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
try {
session.createQuery(sb.toString());
return session.createQuery(sb.toString()).executeUpdate();
} catch (RuntimeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new HibernateException(e);
}
}
});
} catch (HibernateException e) {
e.printStackTrace();
throw new CommonException(e);
}
}
/**
* 实现通用接口:根据主键查询对象
*
*/
public POJO getById(Class cl, Serializable id) throws CommonException {
try {
return (POJO) this.getHibernateTemplate().get(cl, id);
} catch (HibernateException e) {
e.printStackTrace();
throw new CommonException(e);
}
}
/**
* 实现通用接口:总页数,总记录
* 参数:class:类名
* pagesize:页大小
*/
public int[] getpagetotal(Class cl, int pagesize) throws CommonException {
int[] a = null;
try {
int total = this.gettotal(cl);
a = new int[] {
total % pagesize == 0 ? total / pagesize : total / pagesize
+ 1, total };
} catch (CommonException e) {
e.printStackTrace();
throw new CommonException(e);
}
return a;
}
/**
* 实现通用接口:总记录条数(getSimpleName得到对象)(在表中�]有���B的)
* 参数:class:类名
*/
public int gettotal(Class cl) throws CommonException {
int total = 0;
try {
final StringBuffer hql = new StringBuffer("select count(a) from ")
.append(cl.getSimpleName()).append(" a");
total=Integer.parseInt((this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
return session.createQuery(hql.toString()).uniqueResult();
}
})).toString());
System.out.println("com total:"+total);
return total;
} catch (DataAccessException e) {
e.printStackTrace();
throw new CommonException(e);
}
}
/**
* 实现通用接口:总记录条数(getSimpleName得到对象)(在表中有���B的),要传个状态字段
* 参数:class:类名
* State:状态属性
*
*/
public int gettotal(Class cl,String state) throws CommonException {
int total = 0;
try {
final StringBuffer hql = new StringBuffer("select count(a) from ")
.append(cl.getSimpleName()).append(" a").append(" where a.").append(state).append("<>1");
total=Integer.parseInt((this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
try {
return session.createQuery(hql.toString()).uniqueResult();
} catch (RuntimeException e) {
e.printStackTrace();
throw new HibernateException(e);
}
}
}
)).toString());
System.out.println("total:"+total);
return total;
} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new CommonException(e);
}
}
/**
* 实现通用接口:总页数,总记录(有状态字段的)
* 参数:class:类名
* State:状态属性
* pagesize:页大小
*/
public int[] getpagetotal(Class cl,String state, int pagesize) throws CommonException {
int[] a = null;
try {
int total = this.gettotal(cl,state);
a = new int[] {
total % pagesize == 0 ? total / pagesize : total / pagesize
+ 1, total };
} catch (CommonException e) {
e.printStackTrace();
throw new CommonException(e);
}
return a;
}
/**
* 实现通用接口:添加对象
*/
public void insert(POJO obj) throws CommonException {
try {
this.getHibernateTemplate().save(obj);
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new CommonException(e);
}
}
/**
* 实现通用接口:修改对象
*/
public void update(POJO obj) throws CommonException {
try {
this.getHibernateTemplate().update(obj);
} catch (HibernateException e) {
e.printStackTrace();
throw new CommonException(e);
}
}
/**
* 实现通用接口:查询
*/
public List query(final HqlExecute hqlexe) throws CommonException {
try {
List list=this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
try {
return hqlexe.execute(session);
} catch (QueryException e) {
e.printStackTrace();
throw new HibernateException(e);
}
}
});
return list;
} catch (DataAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new CommonException(e);
}
}
}