📄 sblogdao.java
字号:
package cn.hope.front.pojo.dao;
import java.util.List;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import org.apache.log4j.Logger;
import cn.hope.front.pojo.SBlog;
import cn.hope.front.pojo.base.BaseSBlogDAO;
import cn.hope.front.util.Utility;
public class SBlogDAO extends BaseSBlogDAO {
Logger log = Logger.getLogger(TFileDAO.class.getName());
/**
* Default constructor. Can be used in place of getInstance()
*/
public SBlogDAO () {}
private int count = 0;
public int getCount() {
return this.count;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SBlogDAO sb=new SBlogDAO();
SBlog sblog=null;
int start=0;
int range=10;
try {
sb.searchBlog(sblog,start,range);
System.out.println(sb);
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List searchBlog(SBlog sblog,int start, int range ) throws HibernateException {
List list = null;
StringBuffer sqlCnt = new StringBuffer();// 取总记录数
StringBuffer sqlStr = new StringBuffer();// 长字符串的拼合一定要用StringBuffer.append()
StringBuffer condition = new StringBuffer();// 查询条件
sqlCnt.append("select count(sblog.sbId) from SBlog sblog where sblog.flag='0' ");
sqlStr.append("select sblog from SBlog sblog where sblog.flag='0'");
try {
initialize();
Query queryCnt = this.getSession().createQuery(
sqlCnt.toString() + condition);// 通过hibernate的Query方法查询数据库(自己看文档)
Integer count = (Integer) queryCnt.uniqueResult();// 得到总记录数
this.count = count.intValue();
condition.append(" order by sblog.sbDate desc");// 加入排序条件
Query query = this.getSession().createQuery(
sqlStr.toString() + condition);
query.setFirstResult(start);// 设置查询起始记录数
query.setMaxResults(range);// 设置查询总记录数
list = query.list();
} catch (HibernateException e) {
log.error(e);
e.printStackTrace();
throw new HibernateException(e);
} finally {
closeCurrentThreadSessions();
}
return list;
}
/*------------- 查找分类的文章----------------------*/
public List select(SBlog sblog,Integer stid) throws HibernateException {
List list = null;
String sqlStr = "select sblog from SBlog sblog where sblog.flag='0' and sblog.SBlogtype.stId='"
+ stid + "'";
try {
initialize();
Query query = this.getSession().createQuery(
sqlStr.toString());
list = query.list();
} catch (HibernateException e) {
log.error(e);
e.printStackTrace();
throw new HibernateException(e);
} finally {
closeCurrentThreadSessions();
}
return list;
}
/**
* 插入数据方法
*
* @param sblog
* @return
* @return
* @throws HibernateException
*/
/*---------------插入数据--------------------*/
public void insert(SBlog sblog) throws HibernateException {
try {
initialize();// 初始化session
this.save(sblog);// 调用生成好的父类方法save(),返回被插入数据的主键
} catch (HibernateException e) {
log.error(e);
e.printStackTrace();
throw new HibernateException(e);
} finally {
closeCurrentThreadSessions();// 关闭当前session
}
}
/*-----------------查找文章内容--------------------------*/
public SBlog searchBy(Integer id) throws HibernateException {
SBlog sblog = null;
String sqlStr = "select sblog from SBlog sblog where sblog.flag='0' and sblog.sbId='"
+ id + "'";
try {
initialize();
List list = this.getSession().find(sqlStr);
if (list.size() > 0) {
sblog = (SBlog) list.get(0);
Utility.formatEnterStr(sblog.getSbCon());
}
} catch (HibernateException e) {
log.error(e);
e.printStackTrace();
throw new HibernateException(e);
} finally {
closeCurrentThreadSessions();
}
return sblog;
}
/*-------------修改----------------*/
public void modify(SBlog sblog) throws HibernateException {
try {
initialize();
super.update(sblog);
} catch (HibernateException e) {
log.error(e);
e.printStackTrace();
throw new HibernateException(e);
} finally {
closeCurrentThreadSessions();
}
}
/*-------------删除-------------*/
public void del(String[] a) throws HibernateException {
SBlog sblog= null;
if(a!=null && a.length>0){
for(int j=0;j<a.length;j++){
Integer i = new Integer(a[j]);
String sqlStr = "select sblog from SBlog sblog where sblog.flag='0' and sblog.sbId='"+i+"'";
try {
initialize();
List list = this.getSession().find(sqlStr);
if (list.size() > 0) {
sblog = (SBlog) list.get(0);
sblog.setFlag("1");
this.save(sblog);
}
} catch (HibernateException e) {
log.error(e);
e.printStackTrace();
throw new HibernateException(e);
} finally {
closeCurrentThreadSessions();
}
}
}
}
public List search(SBlog sblog, int start, int range, boolean isEq)
throws HibernateException {
List list = null;
StringBuffer sqlCnt = new StringBuffer();// 取总记录数
StringBuffer sqlStr = new StringBuffer();// 长字符串的拼合一定要用StringBuffer.append()
StringBuffer condition = new StringBuffer();// 查询条件
sqlCnt.append("select count(sblog.sbId) from SBlog sblog where sblog.flag='0' ");
sqlStr.append("select sblog from SBlog sblog where sblog.flag='0' ");
/* 通过tFile中存储的条件组装HQL语句 */
try {
initialize();
Query queryCnt = this.getSession().createQuery(
sqlCnt.toString() + condition);// 通过hibernate的Query方法查询数据库(自己看文档)
Integer count = (Integer) queryCnt.uniqueResult();// 得到总记录数
this.count = count.intValue();
condition.append(" order by sblog.sbDate desc");// 加入排序条件
Query query = this.getSession().createQuery(
sqlStr.toString() + condition);
query.setFirstResult(start);// 设置查询起始记录数
query.setMaxResults(range);// 设置查询总记录数
list = query.list();
} catch (HibernateException e) {
log.error(e);
e.printStackTrace();
throw new HibernateException(e);
} finally {
closeCurrentThreadSessions();
}
return list;
}
/**************************************/
public int searchByName(String sbtitle) throws HibernateException{
String sqlCnt = "select count(sblog.sbId) from SBlog sblog where sblog.flag='0' and sblog.sbTitle='"
+sbtitle+"'";
try {
initialize();
Query queryCnt = this.getSession().createQuery(sqlCnt);
return ((Integer)queryCnt.uniqueResult()).intValue();
} catch (HibernateException e) {
log.error(e);
e.printStackTrace();
throw new HibernateException(e);
} finally {
closeCurrentThreadSessions();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -