com.bookssys.action控制器中查询方法
@Controller
@ParentPackage("json-default")
public class BookInfoAction extends ActionSupport {
//注入
private BookInfoBiz bookInfoBiz;//com.bookssys.biz包下BookInfoBiz.java的类名
if( totalCnt%pageSize==0){
totalPage= totalCnt/pageSize;
}else{
totalPage=totalSize/pageSize+1;
}
//判断当前页(不能小于1,不能大于总页数),进行页码控制
if(pageIndex<1){
pageIndex=1;
}else if(pageIndex>totalPage){
pageIndex=totalPage;
}
}
com.bookssys.dao.impl包中根据多条件动态分页查询图书信息--findBookInfosByConditions方法的实现
public class BookInfoDaoImpl extends HibernateDaoSupport implements BookInfoDao {
@Override
public List<BookInfo> findBookInfosByConditions(final Integer bookType,final String bookName,
final Integer pageIndex, final Integer pageSize) {
// TODO Auto-generated method stub// getHibernateTemplate().find(hql.toString(), params);
return getHibernateTemplate().executeFind(new HibernateCallback() {// 利用回调进行分页
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder(
"from BookInfo where 1=1");// 单线程的时候用StringBuilder相对于Sting比较节省资源
int index = 0;
// 将条件拼接到查询语句中 总共2个参数,分别对条件判断是否要传到查询方法中
if (bookType != null && bookType > 0) {
hql.append(" and bookType.id = ?");//调用BookType对象bookType中的id,and前面有空格
}
if (bookName != null && !bookName.equals("")) {
hql.append(" and bookName like ?");//模糊查询 步骤1,and前面有空格
}
// TODO Auto-generated method stub
Query query = session.createQuery(hql.toString());// 创建查询模版将查询语句放到模版中
// 配置参数(将参数送到查询语句中)
if (bookType != null && bookType > 0) {
query.setParameter(index++, bookType);
}
if (bookName != null && !bookName.equals("")) {
query.setParameter(index++, "%" + bookName + "%");// 模糊查询 步骤2
}
// 对信息进行分页
query.setFirstResult((pageIndex - 1) * pageSize);// 从第几页开始查询
query.setMaxResults(pageSize);// 每页显示的数量
return query.list();
}
});
com.bookssys.dao.impl包中根据多条件动态查询图书信息总数--findBookInfosByConditionsCnt方法的实现
@Override
public Integer findBookInfosByConditionsCnt(final Integer bookType,
final String bookName, final Integer isBorrow) {
// TODO Auto-generated method stub
Long count = (Long) getHibernateTemplate().execute(new HibernateCallback() {// 利用回调进行分页
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
StringBuilder hql = new StringBuilder(
"select count(bookId) from BookInfo where 1=1");// 单线程的时候用StringBuilder相对于Sting比较节省资源
int index = 0;
// 将条件拼接到查询语句中 总共2个参数,分别对条件判断是否要传到查询方法中
if (bookType != null && bookType > 0) {
hql.append(" and bookType.id = ?");// 调用BookType对象bookType中的id and前面有空格
}
if (bookName != null && !bookName.equals("")) {
hql.append(" and bookName like ?");// 模糊查询 步骤1 and前面有空格
}
// TODO Auto-generated method stub
Query query = session.createQuery(hql.toString());// 创建查询模版将查询语句放到模版中
// 配置参数(将参数送到查询语句中)
if (bookType != null && bookType > 0) {
query.setParameter(index++, bookType);
}
if (bookName != null && !bookName.equals("")) {
query.setParameter(index++, "%" + bookName + "%");//模糊查询 步骤2
}
return query.uniqueResult();//返回单个对象用uniqueResult
}
});
return Integer.parseInt(count.toString()); //将Long类型先转成字符串类型再转换成Integer类型
}
}