java用ajax做分页查询,(100%原创)java+Ajax制作分页程序(三)

这篇文章详细讲解了BaseDAO类中用于执行分页查询的方法find,展示了如何使用Finder构建查询语句,并通过Pagination类处理分页参数和返回结果。主要涉及的技术包括HQL查询、参数绑定和Pagination的使用。
摘要由CSDN通过智能技术生成

BaseDAO代码:

public Pagination find(Finder finder, int pageNo, int pageSize)

{

int totalCount =

countQueryResult(finder);

Pagination p = new

Pagination(pageNo, pageSize, totalCount);

if (totalCount <

1) {

p.setList(new

ArrayList());

return

p;

}

Query query =

getSession().createQuery(finder.getOrigHql());

finder.setParamsToQuery(query);

query.setFirstResult(p.getFirstResult());

query.setMaxResults(p.getPageSize());

List list = query.list();

p.setList(list);

return p;

}

Pagination代码:

package com.ykrc.tools.page;

import java.util.List;

@SuppressWarnings("serial")

public class Pagination extends SimplePage implements

java.io.Serializable,

Paginable {

public Pagination() {

}

public Pagination(int pageNo, int pageSize,

int totalCount) {

super(pageNo, pageSize,

totalCount);

}

@SuppressWarnings("unchecked")

public Pagination(int pageNo, int pageSize, int

totalCount, List list) {

super(pageNo, pageSize,

totalCount);

this.list = list;

}

public int getFirstResult() {

return (pageNo - 1) *

pageSize;

}

@SuppressWarnings("unchecked")

private List list;

@SuppressWarnings("unchecked")

public List getList() {

return list;

}

@SuppressWarnings("unchecked")

public void setList(List list) {

this.list = list;

}

}

Finder代码:

package com.ykrc.tools.base.dao.util;

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

import java.util.Map;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.type.Type;

public class Finder {

protected Finder() {

}

public Finder(String hql) {

hqlBuilder = new

StringBuilder(hql);

}

public static Finder create(String hql)

{

Finder finder = new

Finder(hql);

return finder;

}

public Finder append(String hql) {

hqlBuilder.append(hql);

return this;

}

public String getOrigHql() {

return

hqlBuilder.toString();

}

public String getRowCountHql() {

String hql =

hqlBuilder.toString();

int fromIndex =

hql.toLowerCase().indexOf(FROM);

String projectionHql =

hql.substring(0, fromIndex);

hql =

hql.substring(fromIndex);

String rowCountHql =

hql.replace(HQL_FETCH, "");

int index =

rowCountHql.indexOf(ORDER_BY);

if (index > 0)

{

rowCountHql =

rowCountHql.substring(0, index);

}

return

wrapProjection(projectionHql) + rowCountHql;

}

public int getFirstResult() {

return firstResult;

}

public void setFirstResult(int firstResult)

{

this.firstResult =

firstResult;

}

public int getMaxResults() {

return maxResults;

}

public void setMaxResults(int maxResults)

{

this.maxResults =

maxResults;

}

public Finder setParam(String param, Object

value) {

return setParam(param, value,

null);

}

public Finder setParam(String param, Object

value, Type type) {

getParams().add(param);

getValues().add(value);

getTypes().add(type);

return this;

}

public Finder

setParams(Map

paramMap) {

for

(Map.Entry entry :

paramMap.entrySet()) {

setParam(entry.getKey(),

entry.getValue());

}

return this;

}

public Finder setParamList(String name,

Collection vals, Type type)

{

getParamsList().add(name);

getValuesList().add(vals);

getTypesList().add(type);

return this;

}

public Finder setParamList(String name,

Collection vals) {

return setParamList(name, vals,

null);

}

public Finder setParamList(String name, Object[]

vals, Type type) {

getParamsArray().add(name);

getValuesArray().add(vals);

getTypesArray().add(type);

return this;

}

public Finder setParamList(String name, Object[]

vals) {

return setParamList(name, vals,

null);

}

public Query setParamsToQuery(Query query)

{

if (params != null) {

for (int i =

0; i < params.size(); i++) {

if

(types.get(i) == null) {

query.setParameter(params.get(i),

values.get(i));

}

else {

query.setParameter(params.get(i),

values.get(i), types

.get(i));

}

}

}

if (paramsList != null) {

for (int i =

0; i < paramsList.size(); i++) {

if

(typesList.get(i) == null) {

query

.setParameterList(paramsList.get(i),

valuesList

.get(i));

}

else {

query.setParameterList(paramsList.get(i),

valuesList.get(i),

typesList.get(i));

}

}

}

if (paramsArray != null)

{

for (int i =

0; i < paramsArray.size(); i++) {

if

(typesArray.get(i) == null) {

query.setParameterList(paramsArray.get(i),

valuesArray

.get(i));

}

else {

query.setParameterList(paramsArray.get(i),

valuesArray

.get(i),

typesArray.get(i));

}

}

}

return query;

}

public Query createQuery(Session s) {

return

setParamsToQuery(s.createQuery(getOrigHql()));

}

private String wrapProjection(String

projection) {

if

(projection.indexOf("select") == -1) {

return

ROW_COUNT;

} else {

return

projection.replace("select", "select count(") + ") ";

}

}

private

List getParams() {

if (params == null) {

params = new

ArrayList();

}

return params;

}

private

List getValues() {

if (values == null) {

values = new

ArrayList();

}

return values;

}

private

List getTypes() {

if (types == null) {

types = new

ArrayList();

}

return types;

}

private

List getParamsList() {

if (paramsList == null) {

paramsList =

new ArrayList();

}

return paramsList;

}

private

List>

getValuesList() {

if (valuesList == null) {

valuesList =

new

ArrayList>();

}

return valuesList;

}

private

List getTypesList() {

if (typesList == null) {

typesList =

new ArrayList();

}

return typesList;

}

private

List getParamsArray() {

if (paramsArray == null)

{

paramsArray =

new ArrayList();

}

return paramsArray;

}

private

List getValuesArray()

{

if (valuesArray == null)

{

valuesArray =

new ArrayList();

}

return valuesArray;

}

private

List getTypesArray() {

if (typesArray == null) {

typesArray =

new ArrayList();

}

return typesArray;

}

private StringBuilder hqlBuilder;

private

List params;

private

List values;

private List

types;

private

List paramsList;

private

List>

valuesList;

private List

typesList;

private

List paramsArray;

private

List valuesArray;

private List

typesArray;

private int firstResult = 0;

private int maxResults = 0;

public static final String ROW_COUNT = "select

count(*) ";

public static final String FROM = "from";

public static final String DISTINCT =

"distinct";

public static final String HQL_FETCH =

"fetch";

public static final String ORDER_BY =

"order";

public static void main(String[] args) {

Finder find = Finder

.create("select

distinct p FROM BookType join fetch p");

System.out.println(find.getRowCountHql());

System.out.println(find.getOrigHql());

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值