basedao.java
字号:
package dao;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
//TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
public class BaseDAO extends
org.springframework.orm.hibernate3.support.HibernateDaoSupport implements IBaseDAO{
static Session session = null;
static Transaction tx = null;
static Logger log = Logger.getLogger(BaseDAO.class);
/*------------创建新对象-----------------*/
public void createObj(Object o) {
try {
getHibernateTemplate().save(o);
} catch (HibernateException e) { // 捕捉例外
log.error("保存 " + o.getClass().getName() + " 实例到数据库失败", e);
}
}
/*------------删除对象-----------------*/
public void delObject(Object o) {
try {
getHibernateTemplate().delete(o);
} catch (HibernateException e) { // 捕捉例外
log.error("删除对象失败!", e);
}
}
/*------------修改对象-----------------*/
public void mdfObj(Object o) {
try {
getHibernateTemplate().update(o);
} catch (HibernateException e) { // 捕捉例外
log.error("修改 " + o.getClass().getName() + " 实例失败", e);
}
}
public List getAll(String s) {
List list = null;
try {
list=getHibernateTemplate().find("from "+s);
} catch (HibernateException e) { // 捕捉例外
log.error("查找所有对象失败", e);
}
return list;
}
public void del(String hql,String[] idd) {
getSession().createQuery(hql).setParameterList("idd",idd).executeUpdate();
// Session session=getHibernateTemplate().getSessionFactory().openSession();
// Transaction tx = session.beginTransaction(); //开启事务
// tx.commit();
// session.close();
}
public List getList(String hql,int max,int first) {
List list = null;
try {
Query q=getSession().createQuery(hql);
q.setMaxResults(max);
q.setFirstResult(first);
list=q.list();
// list=getHibernateTemplate().find("from Student order by newsDate desc limit 1,2");
} catch (HibernateException e) { // 捕捉例外
log.error("查找所有对象失败", e);
}
return list;
}
public List getList(String hql,String[] pa,int max,int first) {
List list = null;
try {
Query q=getSession().createQuery(hql);
q.setMaxResults(max);
q.setFirstResult(first);
for (int i = 0; i < pa.length; i=i+2) {
if(pa[i]!=null) q.setParameter(pa[i],pa[i+1]);
}
list=q.list();
// list=getHibernateTemplate().find("from Student order by newsDate desc limit 1,2");
} catch (HibernateException e) { // 捕捉例外
log.error("查找所有对象失败", e);
}
return list;
}
public Object findById(Class cla,String id) {
Object obj = null;
try {
obj = getHibernateTemplate().get(cla, id);
} catch (HibernateException e) { // 捕捉例外
log.error("查找id为 " + id+ " 的对象失败", e);
}
return obj;
}
public int getCount(String hql){//得到表的总行数
int count=0;
try {
count = ((Integer)getSession().createQuery(hql).iterate().next()).intValue();
} catch (HibernateException e) {
}
return count;
}
public int getCount(String hql,String[] pa){//得到表的总行数
int count=0;
try {
Query q=getSession().createQuery(hql);
for (int i = 0; i < pa.length; i=i+2) {
if(pa[i]!=null) q.setParameter(pa[i],pa[i+1]);
}
count = ((Integer)q.iterate().next()).intValue();
} catch (HibernateException e) {
}
return count;
}
public int login(String na,String pa){//得到表的总行数
int count=0;
try {
Query q=getSession().createQuery("select count(id) from User where uname=:na and upass=:pa");
q.setParameter("na",na);
q.setParameter("pa",pa);
count = ((Integer)q.iterate().next()).intValue();
} catch (HibernateException e) {
}
return count;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -