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());
}
}