Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
JeeSpringCloudV3.0
Commits
d5ba54ba
Commit
d5ba54ba
authored
Nov 12, 2018
by
Huang
Browse files
no commit message
parent
da9d3b1b
Changes
399
Show whitespace changes
Inline
Side-by-side
Too many changes to show.
To preserve performance only
20 of 399+
files are displayed.
Plain diff
Email patch
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/Page.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence
;
import
java.io.Serializable
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.regex.Pattern
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
import
com.jeespring.common.config.Global
;
import
org.apache.commons.lang3.StringUtils
;
import
com.fasterxml.jackson.annotation.JsonIgnore
;
import
com.jeespring.common.utils.CookieUtils
;
import
org.apache.commons.lang3.builder.ReflectionToStringBuilder
;
/**
* 分页类
* @author 黄炳桂 516821420@qq.com
* @version 2013-7-2
* @param <T>
*/
public
class
Page
<
T
>
implements
Serializable
{
protected
int
pageNo
=
1
;
// 当前页码
protected
int
pageSize
=
Integer
.
valueOf
(
Global
.
getConfig
(
"page.pageSize"
));
// 页面大小,设置为“-1”表示不进行分页(分页无效)
protected
long
count
;
// 总记录数,设置为“-1”表示不查询总数
protected
int
first
;
// 首页索引
protected
int
last
;
// 尾页索引
protected
int
prev
;
// 上一页索引
protected
int
next
;
// 下一页索引
private
boolean
firstPage
;
//是否是第一页
private
boolean
lastPage
;
//是否是最后一页
protected
int
length
=
8
;
// 显示页面长度
protected
int
slider
=
1
;
// 前后显示页面长度
private
List
<
T
>
list
=
new
ArrayList
<
T
>();
private
String
orderBy
=
""
;
// 标准查询有效, 实例: updatedate desc, name asc
protected
String
funcName
=
"page"
;
// 设置点击页码调用的js函数名称,默认为page,在一页有多个分页对象时使用。
protected
String
funcParam
=
""
;
// 函数的附加参数,第三个参数值。
private
String
message
=
""
;
// 设置提示消息,显示在“共n条”之后
public
Page
()
{
this
.
pageSize
=
-
1
;
}
/**
* 构造方法
* @param request 传递 repage 参数,来记住页码
* @param response 用于设置 Cookie,记住页码
*/
public
Page
(
HttpServletRequest
request
,
HttpServletResponse
response
){
this
(
request
,
response
,
-
2
);
}
public
Page
(
int
pageNo
,
int
pageSize
,
String
orderBy
){
this
(
pageNo
,
pageSize
,
0
);
this
.
orderBy
=
orderBy
;
}
/**
* 构造方法
* @param request 传递 repage 参数,来记住页码
* @param response 用于设置 Cookie,记住页码
* @param defaultPageSize 默认分页大小,如果传递 -1 则为不分页,返回所有数据
*/
public
Page
(
HttpServletRequest
request
,
HttpServletResponse
response
,
int
defaultPageSize
){
// 设置页码参数(传递repage参数,来记住页码)
String
no
=
request
.
getParameter
(
"pageNo"
);
if
(
StringUtils
.
isNumeric
(
no
)){
CookieUtils
.
setCookie
(
response
,
"pageNo"
,
no
);
this
.
setPageNo
(
Integer
.
parseInt
(
no
));
}
else
if
(
request
.
getParameter
(
"repage"
)!=
null
){
no
=
CookieUtils
.
getCookie
(
request
,
"pageNo"
);
if
(
StringUtils
.
isNumeric
(
no
)){
this
.
setPageNo
(
Integer
.
parseInt
(
no
));
}
}
// 设置页面大小参数(传递repage参数,来记住页码大小)
String
size
=
request
.
getParameter
(
"pageSize"
);
if
(
StringUtils
.
isNumeric
(
size
)){
CookieUtils
.
setCookie
(
response
,
"pageSize"
,
size
);
this
.
setPageSize
(
Integer
.
parseInt
(
size
));
}
else
if
(
request
.
getParameter
(
"repage"
)!=
null
){
no
=
CookieUtils
.
getCookie
(
request
,
"pageSize"
);
if
(
StringUtils
.
isNumeric
(
size
)){
this
.
setPageSize
(
Integer
.
parseInt
(
size
));
}
}
else
if
(
defaultPageSize
!=
-
2
){
this
.
pageSize
=
defaultPageSize
;
}
// 设置排序参数
String
orderBy
=
request
.
getParameter
(
"orderBy"
);
if
(
StringUtils
.
isNotBlank
(
orderBy
)){
this
.
setOrderBy
(
orderBy
);
}
}
/**
* 构造方法
* @param pageNo 当前页码
* @param pageSize 分页大小
*/
public
Page
(
int
pageNo
,
int
pageSize
)
{
this
(
pageNo
,
pageSize
,
0
);
}
/**
* 构造方法
* @param pageNo 当前页码
* @param pageSize 分页大小
* @param count 数据条数
*/
public
Page
(
int
pageNo
,
int
pageSize
,
long
count
)
{
this
(
pageNo
,
pageSize
,
count
,
new
ArrayList
<
T
>());
}
/**
* 构造方法
* @param pageNo 当前页码
* @param pageSize 分页大小
* @param count 数据条数
* @param list 本页数据对象列表
*/
public
Page
(
int
pageNo
,
int
pageSize
,
long
count
,
List
<
T
>
list
)
{
this
.
setCount
(
count
);
this
.
setPageNo
(
pageNo
);
this
.
pageSize
=
pageSize
;
this
.
list
=
list
;
}
/**
* 初始化参数
*/
public
void
initialize
(){
//1
this
.
first
=
1
;
this
.
last
=
(
int
)(
count
/
(
this
.
pageSize
<
1
?
20
:
this
.
pageSize
)
+
first
-
1
);
if
(
this
.
count
%
this
.
pageSize
!=
0
||
this
.
last
==
0
)
{
this
.
last
++;
}
if
(
this
.
last
<
this
.
first
)
{
this
.
last
=
this
.
first
;
}
if
(
this
.
pageNo
<=
1
)
{
this
.
pageNo
=
this
.
first
;
this
.
firstPage
=
true
;
}
if
(
this
.
pageNo
>=
this
.
last
)
{
this
.
pageNo
=
this
.
last
;
this
.
lastPage
=
true
;
}
if
(
this
.
pageNo
<
this
.
last
-
1
)
{
this
.
next
=
this
.
pageNo
+
1
;
}
else
{
this
.
next
=
this
.
last
;
}
if
(
this
.
pageNo
>
1
)
{
this
.
prev
=
this
.
pageNo
-
1
;
}
else
{
this
.
prev
=
this
.
first
;
}
//2
if
(
this
.
pageNo
<
this
.
first
)
{
// 如果当前页小于首页
this
.
pageNo
=
this
.
first
;
}
if
(
this
.
pageNo
>
this
.
last
)
{
// 如果当前页大于尾页
this
.
pageNo
=
this
.
last
;
}
}
@Override
public
String
toString
()
{
return
ReflectionToStringBuilder
.
toString
(
this
);
}
/**
* 默认输出当前分页标签
* <div class="page">${page}</div>
*/
public
String
toStringPage
()
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
"<div class=\"fixed-table-pagination\" style=\"display: block;\">"
);
// sb.append("<div class=\"dataTables_info\">");
// sb.append("<li class=\"disabled controls\"><a href=\"javascript:\">当前 ");
// sb.append("<input type=\"text\" value=\""+pageNo+"\" onkeypress=\"var e=window.event||this;var c=e.keyCode||e.which;if(c==13)");
// sb.append(funcName+"(this.value,"+pageSize+",'"+funcParam+"');\" onclick=\"this.select();\"/> / ");
// sb.append("<input type=\"text\" value=\""+pageSize+"\" onkeypress=\"var e=window.event||this;var c=e.keyCode||e.which;if(c==13)");
// sb.append(funcName+"("+pageNo+",this.value,'"+funcParam+"');\" onclick=\"this.select();\"/> 条,");
// sb.append("共 " + count + " 条"+(message!=null?message:"")+"</a></li>\n");
// sb.append("</div>");
long
startIndex
=
(
pageNo
-
1
)*
pageSize
+
1
;
long
endIndex
=
pageNo
*
pageSize
<=
count
?
pageNo
*
pageSize:
count
;
sb
.
append
(
"<div class=\"pull-left pagination-detail\">"
);
sb
.
append
(
"<span class=\"pagination-info\">显示第 "
+
startIndex
+
" 到第 "
+
endIndex
+
" 条记录,总共 "
+
count
+
" 条记录</span>"
);
sb
.
append
(
"<span class=\"page-list\">每页显示 <span class=\"btn-group dropup\">"
);
sb
.
append
(
"<button type=\"button\" class=\"btn btn-sm btn-primary dropdown-toggle\" data-toggle=\"dropdown\" aria-expanded=\"false\">"
);
sb
.
append
(
"<span class=\"page-size\">"
+
pageSize
+
"</span> <span class=\"caret\"></span>"
);
sb
.
append
(
"</button>"
);
sb
.
append
(
"<ul class=\"dropdown-menu\" role=\"menu\">"
);
sb
.
append
(
"<li class=\""
+
getSelected
(
pageSize
,
10
)+
"\"><a href=\"javascript:"
+
funcName
+
"("
+
pageNo
+
",10,'"
+
funcParam
+
"');\">10</a></li>"
);
sb
.
append
(
"<li class=\""
+
getSelected
(
pageSize
,
25
)+
"\"><a href=\"javascript:"
+
funcName
+
"("
+
pageNo
+
",25,'"
+
funcParam
+
"');\">25</a></li>"
);
sb
.
append
(
"<li class=\""
+
getSelected
(
pageSize
,
50
)+
"\"><a href=\"javascript:"
+
funcName
+
"("
+
pageNo
+
",50,'"
+
funcParam
+
"');\">50</a></li>"
);
sb
.
append
(
"<li class=\""
+
getSelected
(
pageSize
,
100
)+
"\"><a href=\"javascript:"
+
funcName
+
"("
+
pageNo
+
",100,'"
+
funcParam
+
"');\">100</a></li>"
);
sb
.
append
(
"<li class=\""
+
getSelected
(
pageSize
,
1000
)+
"\"><a href=\"javascript:"
+
funcName
+
"("
+
pageNo
+
",1000,'"
+
funcParam
+
"');\">1000</a></li>"
);
sb
.
append
(
"<li class=\""
+
getSelected
(
pageSize
,
100000000
)+
"\"><a href=\"javascript:"
+
funcName
+
"("
+
pageNo
+
",100000000,'"
+
funcParam
+
"');\">全部</a></li>"
);
sb
.
append
(
"</ul>"
);
sb
.
append
(
"</span> 条记录</span>"
);
sb
.
append
(
"</div>"
);
// sb.append("<p>每页 <select onChange=\""+funcName+"("+pageNo+",this.value,'"+funcParam+"');\"" +"style=\"display:display !important;\" class=\"form-control m-b input-sm\">" +
// "<option value=\"10\" "+getSelected(pageSize,10)+ ">10</option>" +
// "<option value=\"25\" "+getSelected(pageSize,25)+ ">25</option>" +
// "<option value=\"50\" "+getSelected(pageSize,50)+ ">50</option>" +
// "<option value=\"100\" "+getSelected(pageSize,100)+ ">100</option>" +
// "</select> 条记录,显示 " +startIndex+ " 到 "+ endIndex +" 条,共 "+count+" 条</p>");
// sb.append("</div>");
// sb.append("</div>");
sb
.
append
(
"<div class=\"pull-right pagination-roll\">"
);
sb
.
append
(
"<ul class=\"pagination pagination-outline\">"
);
if
(
pageNo
==
first
)
{
// 如果是首页
sb
.
append
(
"<li class=\"paginate_button previous disabled\"><a href=\"javascript:\"><i class=\"fa fa-angle-double-left\"></i></a></li>\n"
);
sb
.
append
(
"<li class=\"paginate_button previous disabled\"><a href=\"javascript:\"><i class=\"fa fa-angle-left\"></i></a></li>\n"
);
}
else
{
sb
.
append
(
"<li class=\"paginate_button previous\"><a href=\"javascript:\" onclick=\""
+
funcName
+
"("
+
first
+
","
+
pageSize
+
",'"
+
funcParam
+
"');\"><i class=\"fa fa-angle-double-left\"></i></a></li>\n"
);
sb
.
append
(
"<li class=\"paginate_button previous\"><a href=\"javascript:\" onclick=\""
+
funcName
+
"("
+
prev
+
","
+
pageSize
+
",'"
+
funcParam
+
"');\"><i class=\"fa fa-angle-left\"></i></a></li>\n"
);
}
int
begin
=
pageNo
-
(
length
/
2
);
if
(
begin
<
first
)
{
begin
=
first
;
}
int
end
=
begin
+
length
-
1
;
if
(
end
>=
last
)
{
end
=
last
;
begin
=
end
-
length
+
1
;
if
(
begin
<
first
)
{
begin
=
first
;
}
}
if
(
begin
>
first
)
{
int
i
=
0
;
for
(
i
=
first
;
i
<
first
+
slider
&&
i
<
begin
;
i
++)
{
sb
.
append
(
"<li class=\"paginate_button \"><a href=\"javascript:\" onclick=\""
+
funcName
+
"("
+
i
+
","
+
pageSize
+
",'"
+
funcParam
+
"');\">"
+
(
i
+
1
-
first
)
+
"</a></li>\n"
);
}
if
(
i
<
begin
)
{
sb
.
append
(
"<li class=\"paginate_button disabled\"><a href=\"javascript:\">...</a></li>\n"
);
}
}
for
(
int
i
=
begin
;
i
<=
end
;
i
++)
{
if
(
i
==
pageNo
)
{
sb
.
append
(
"<li class=\"paginate_button active\"><a href=\"javascript:\">"
+
(
i
+
1
-
first
)
+
"</a></li>\n"
);
}
else
{
sb
.
append
(
"<li class=\"paginate_button \"><a href=\"javascript:\" onclick=\""
+
funcName
+
"("
+
i
+
","
+
pageSize
+
",'"
+
funcParam
+
"');\">"
+
(
i
+
1
-
first
)
+
"</a></li>\n"
);
}
}
if
(
last
-
end
>
slider
)
{
sb
.
append
(
"<li class=\"paginate_button disabled\"><a href=\"javascript:\">...</a></li>\n"
);
end
=
last
-
slider
;
}
for
(
int
i
=
end
+
1
;
i
<=
last
;
i
++)
{
sb
.
append
(
"<li class=\"paginate_button \"><a href=\"javascript:\" onclick=\""
+
funcName
+
"("
+
i
+
","
+
pageSize
+
",'"
+
funcParam
+
"');\">"
+
(
i
+
1
-
first
)
+
"</a></li>\n"
);
}
if
(
pageNo
==
last
)
{
sb
.
append
(
"<li class=\"paginate_button next disabled\"><a href=\"javascript:\"><i class=\"fa fa-angle-right\"></i></a></li>\n"
);
sb
.
append
(
"<li class=\"paginate_button next disabled\"><a href=\"javascript:\"><i class=\"fa fa-angle-double-right\"></i></a></li>\n"
);
}
else
{
sb
.
append
(
"<li class=\"paginate_button next\"><a href=\"javascript:\" onclick=\""
+
funcName
+
"("
+
next
+
","
+
pageSize
+
",'"
+
funcParam
+
"');\">"
+
"<i class=\"fa fa-angle-right\"></i></a></li>\n"
);
sb
.
append
(
"<li class=\"paginate_button next\"><a href=\"javascript:\" onclick=\""
+
funcName
+
"("
+
last
+
","
+
pageSize
+
",'"
+
funcParam
+
"');\">"
+
"<i class=\"fa fa-angle-double-right\"></i></a></li>\n"
);
}
sb
.
append
(
"</ul>"
);
sb
.
append
(
"</div>"
);
sb
.
append
(
"</div>"
);
// sb.insert(0,"<ul>\n").append("</ul>\n");
// sb.append("<div style=\"clear:both;\"></div>");
// sb.insert(0,"<div class=\"page\">\n").append("</div>\n");
return
sb
.
toString
();
}
/**
* 默认输出当前分页标签
* <div class="page">${page}</div>
*/
public
String
getPageHtml
()
{
StringBuilder
sb
=
new
StringBuilder
();
if
(
pageNo
==
first
)
{
// 如果是首页
sb
.
append
(
"<li class=\"disabled\"><a href=\"javascript:\">« 上一页</a></li>\n"
);
}
else
{
sb
.
append
(
"<li><a href=\"javascript:\" onclick=\""
+
funcName
+
"("
+
prev
+
","
+
pageSize
+
",'"
+
funcParam
+
"');\">« 上一页</a></li>\n"
);
}
int
begin
=
pageNo
-
(
length
/
2
);
if
(
begin
<
first
)
{
begin
=
first
;
}
int
end
=
begin
+
length
-
1
;
if
(
end
>=
last
)
{
end
=
last
;
begin
=
end
-
length
+
1
;
if
(
begin
<
first
)
{
begin
=
first
;
}
}
if
(
begin
>
first
)
{
int
i
=
0
;
for
(
i
=
first
;
i
<
first
+
slider
&&
i
<
begin
;
i
++)
{
sb
.
append
(
"<li><a href=\"javascript:\" onclick=\""
+
funcName
+
"("
+
i
+
","
+
pageSize
+
",'"
+
funcParam
+
"');\">"
+
(
i
+
1
-
first
)
+
"</a></li>\n"
);
}
if
(
i
<
begin
)
{
sb
.
append
(
"<li class=\"disabled\"><a href=\"javascript:\">...</a></li>\n"
);
}
}
for
(
int
i
=
begin
;
i
<=
end
;
i
++)
{
if
(
i
==
pageNo
)
{
sb
.
append
(
"<li class=\"active\"><a href=\"javascript:\">"
+
(
i
+
1
-
first
)
+
"</a></li>\n"
);
}
else
{
sb
.
append
(
"<li><a href=\"javascript:\" onclick=\""
+
funcName
+
"("
+
i
+
","
+
pageSize
+
",'"
+
funcParam
+
"');\">"
+
(
i
+
1
-
first
)
+
"</a></li>\n"
);
}
}
if
(
last
-
end
>
slider
)
{
sb
.
append
(
"<li class=\"disabled\"><a href=\"javascript:\">...</a></li>\n"
);
end
=
last
-
slider
;
}
for
(
int
i
=
end
+
1
;
i
<=
last
;
i
++)
{
sb
.
append
(
"<li><a href=\"javascript:\" onclick=\""
+
funcName
+
"("
+
i
+
","
+
pageSize
+
",'"
+
funcParam
+
"');\">"
+
(
i
+
1
-
first
)
+
"</a></li>\n"
);
}
if
(
pageNo
==
last
)
{
sb
.
append
(
"<li class=\"disabled\"><a href=\"javascript:\">下一页 »</a></li>\n"
);
}
else
{
sb
.
append
(
"<li><a href=\"javascript:\" onclick=\""
+
funcName
+
"("
+
next
+
","
+
pageSize
+
",'"
+
funcParam
+
"');\">"
+
"下一页 »</a></li>\n"
);
}
sb
.
append
(
"<li class=\"disabled controls\"><a href=\"javascript:\">当前 "
);
sb
.
append
(
"<input type=\"text\" value=\""
+
pageNo
+
"\" onkeypress=\"var e=window.event||event;var c=e.keyCode||e.which;if(c==13)"
);
sb
.
append
(
funcName
+
"(this.value,"
+
pageSize
+
",'"
+
funcParam
+
"');\" onclick=\"this.select();\"/> / "
);
sb
.
append
(
"<input type=\"text\" value=\""
+
pageSize
+
"\" onkeypress=\"var e=window.event||event;var c=e.keyCode||e.which;if(c==13)"
);
sb
.
append
(
funcName
+
"("
+
pageNo
+
",this.value,'"
+
funcParam
+
"');\" onclick=\"this.select();\"/> 条,"
);
sb
.
append
(
"共 "
+
count
+
" 条"
+(
message
!=
null
?
message:
""
)+
"</a></li>\n"
);
sb
.
insert
(
0
,
"<ul>\n"
).
append
(
"</ul>\n"
);
sb
.
append
(
"<div style=\"clear:both;\"></div>"
);
// sb.insert(0,"<div class=\"page\">\n").append("</div>\n");
return
sb
.
toString
();
}
protected
String
getSelected
(
int
pageNo
,
int
selectedPageNo
){
if
(
pageNo
==
selectedPageNo
){
//return "selected";
return
"active"
;
}
else
{
return
""
;
}
}
/**
* 获取分页HTML代码
* @return
*/
//@JsonIgnore
public
String
getHtml
(){
return
toStringPage
();
}
// public static void main(String[] args) {
// Page<String> p = new Page<String>(3, 3);
// System.out.println(p);
// System.out.println("首页:"+p.getFirst());
// System.out.println("尾页:"+p.getLast());
// System.out.println("上页:"+p.getPrev());
// System.out.println("下页:"+p.getNext());
// }
/**
* 获取设置总数
* @return
*/
public
long
getCount
()
{
return
count
;
}
/**
* 设置数据总数
* @param count
*/
public
void
setCount
(
long
count
)
{
this
.
count
=
count
;
if
(
pageSize
>=
count
){
pageNo
=
1
;
}
}
/**
* 获取当前页码
* @return
*/
public
int
getPageNo
()
{
return
pageNo
;
}
/**
* 设置当前页码
* @param pageNo
*/
public
void
setPageNo
(
int
pageNo
)
{
this
.
pageNo
=
pageNo
;
}
/**
* 获取页面大小
* @return
*/
public
int
getPageSize
()
{
return
pageSize
;
}
/**
* 设置页面大小(最大500)
* @param pageSize
*/
public
void
setPageSize
(
int
pageSize
)
{
this
.
pageSize
=
pageSize
<=
0
?
10
:
pageSize
;
// > 500 ? 500 : pageSize;
}
/**
* 首页索引
* @return
*/
@JsonIgnore
public
int
getFirst
()
{
return
first
;
}
/**
* 尾页索引
* @return
*/
@JsonIgnore
public
int
getLast
()
{
return
last
;
}
/**
* 获取页面总数
* @return getLast();
*/
@JsonIgnore
public
int
getTotalPage
()
{
return
getLast
();
}
/**
* 是否为第一页
* @return
*/
@JsonIgnore
public
boolean
isFirstPage
()
{
return
firstPage
;
}
/**
* 是否为最后一页
* @return
*/
@JsonIgnore
public
boolean
isLastPage
()
{
return
lastPage
;
}
/**
* 上一页索引值
* @return
*/
@JsonIgnore
public
int
getPrev
()
{
if
(
isFirstPage
())
{
return
pageNo
;
}
else
{
return
pageNo
-
1
;
}
}
/**
* 下一页索引值
* @return
*/
@JsonIgnore
public
int
getNext
()
{
if
(
isLastPage
())
{
return
pageNo
;
}
else
{
return
pageNo
+
1
;
}
}
/**
* 获取本页数据对象列表
* @return List<T>
*/
public
List
<
T
>
getList
()
{
return
list
;
}
/**
* 设置本页数据对象列表
* @param list
*/
public
Page
<
T
>
setList
(
List
<
T
>
list
)
{
this
.
list
=
list
;
initialize
();
return
this
;
}
/**
* 获取查询排序字符串
* @return
*/
@JsonIgnore
public
String
getOrderBy
()
{
// SQL过滤,防止注入
String
reg
=
"(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|"
+
"(\\b(select|update|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)"
;
Pattern
sqlPattern
=
Pattern
.
compile
(
reg
,
Pattern
.
CASE_INSENSITIVE
);
if
(
sqlPattern
.
matcher
(
orderBy
).
find
())
{
return
""
;
}
return
orderBy
;
}
/**
* 设置查询排序,标准查询有效, 实例: updatedate desc, name asc
*/
public
void
setOrderBy
(
String
orderBy
)
{
this
.
orderBy
=
orderBy
;
}
/**
* 获取点击页码调用的js函数名称
* function ${page.funcName}(pageNo){location="${ctx}/list-${category.id}${urlSuffix}?pageNo="+i;}
* @return
*/
@JsonIgnore
public
String
getFuncName
()
{
return
funcName
;
}
/**
* 设置点击页码调用的js函数名称,默认为page,在一页有多个分页对象时使用。
* @param funcName 默认为page
*/
public
void
setFuncName
(
String
funcName
)
{
this
.
funcName
=
funcName
;
}
/**
* 获取分页函数的附加参数
* @return
*/
@JsonIgnore
public
String
getFuncParam
()
{
return
funcParam
;
}
/**
* 设置分页函数的附加参数
* @return
*/
public
void
setFuncParam
(
String
funcParam
)
{
this
.
funcParam
=
funcParam
;
}
/**
* 设置提示消息,显示在“共n条”之后
* @param message
*/
public
void
setMessage
(
String
message
)
{
this
.
message
=
message
;
}
/**
* 分页是否有效
* @return this.pageSize==-1
*/
@JsonIgnore
public
boolean
isDisabled
()
{
return
this
.
pageSize
==-
1
;
}
/**
* 是否进行总数统计
* @return this.count==-1
*/
@JsonIgnore
public
boolean
isNotCount
()
{
return
this
.
count
==-
1
;
}
/**
* 获取 Hibernate FirstResult
*/
public
int
getFirstResult
(){
int
firstResult
=
(
getPageNo
()
-
1
)
*
getPageSize
();
if
(
firstResult
>=
getCount
())
{
firstResult
=
0
;
}
return
firstResult
;
}
/**
* 获取 Hibernate MaxResults
*/
public
int
getMaxResults
(){
return
getPageSize
();
}
// /**
// * 获取 Spring data JPA 分页对象
// */
// public Pageable getSpringPage(){
// List<Order> orders = new ArrayList<Order>();
// if (orderBy!=null){
// for (String order : StringUtils.split(orderBy, ",")){
// String[] o = StringUtils.split(order, " ");
// if (o.length==1){
// orders.add(new Order(Direction.ASC, o[0]));
// }else if (o.length==2){
// if ("DESC".equals(o[1].toUpperCase())){
// orders.add(new Order(Direction.DESC, o[0]));
// }else{
// orders.add(new Order(Direction.ASC, o[0]));
// }
// }
// }
// }
// return new PageRequest(this.pageNo - 1, this.pageSize, new Sort(orders));
// }
//
// /**
// * 设置 Spring data JPA 分页对象,转换为本系统分页对象
// */
// public void setSpringPage(org.springframework.data.domain.Page<T> page){
// this.pageNo = page.getNumber();
// this.pageSize = page.getSize();
// this.count = page.getTotalElements();
// this.list = page.getContent();
// }
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/TreeDao.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence
;
import
java.util.List
;
/**
* DAO支持类实现
* @author 黄炳桂 516821420@qq.com
* @version 2014-05-16
* @param <T>
*/
public
interface
TreeDao
<
T
extends
TreeEntity
<
T
>>
extends
InterfaceBaseDao
<
T
>
{
/**
* 找到所有子节点
* @param entity
* @return
*/
List
<
T
>
findByParentIdsLike
(
T
entity
);
/**
* 更新所有父节点字段
* @param entity
* @return
*/
int
updateParentIds
(
T
entity
);
}
\ No newline at end of file
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/TreeEntity.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence
;
import
javax.validation.constraints.NotNull
;
import
org.hibernate.validator.constraints.Length
;
import
com.fasterxml.jackson.annotation.JsonBackReference
;
import
com.jeespring.common.utils.Reflections
;
import
com.jeespring.common.utils.StringUtils
;
/**
* 数据Entity类
* * * * @author 黄炳桂 516821420@qq.com
* @version 2014-05-16
*/
public
abstract
class
TreeEntity
<
T
>
extends
AbstractBaseEntity
<
T
>
{
private
static
final
long
serialVersionUID
=
1L
;
protected
T
parent
;
// 父级编号
protected
String
parentIds
;
// 所有父级编号
protected
String
name
;
// 机构名称
protected
Integer
sort
;
// 排序
public
TreeEntity
()
{
super
();
this
.
sort
=
30
;
}
public
TreeEntity
(
String
id
)
{
super
(
id
);
}
/**
* 父对象,只能通过子类实现,父类实现mybatis无法读取
* @return
*/
@JsonBackReference
@NotNull
public
abstract
T
getParent
();
/**
* 父对象,只能通过子类实现,父类实现mybatis无法读取
* @return
*/
public
abstract
void
setParent
(
T
parent
);
@Length
(
min
=
1
,
max
=
2000
)
public
String
getParentIds
()
{
return
parentIds
;
}
public
void
setParentIds
(
String
parentIds
)
{
this
.
parentIds
=
parentIds
;
}
@Length
(
min
=
1
,
max
=
100
)
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
Integer
getSort
()
{
return
sort
;
}
public
void
setSort
(
Integer
sort
)
{
this
.
sort
=
sort
;
}
public
String
getParentId
()
{
String
id
=
null
;
if
(
parent
!=
null
){
id
=
(
String
)
Reflections
.
getFieldValue
(
parent
,
"id"
);
}
return
StringUtils
.
isNotBlank
(
id
)
?
id
:
"0"
;
}
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/annotation/MyBatisDao.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">JeeSpring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.annotation
;
import
java.lang.annotation.Documented
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
import
org.springframework.stereotype.Component
;
/**
* 标识MyBatis的DAO,方便{@link org.mybatis.spring.mapper.MapperScannerConfigurer}的扫描。
* @author 黄炳桂 516821420@qq.com
* @version 2013-8-28
*/
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Target
(
ElementType
.
TYPE
)
@Documented
@Component
public
@interface
MyBatisDao
{
/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any
*/
String
value
()
default
""
;
}
\ No newline at end of file
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/dialect/DB2Dialect.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.dialect
;
import
com.jeespring.common.persistence.dialect.Dialect
;
/**
* DB2的分页数据库方言实现
*
* @author poplar.yfyang
* @version 1.0 2010-10-10 下午12:31
* @since JDK 1.5
*/
public
class
DB2Dialect
implements
Dialect
{
@Override
public
boolean
supportsLimit
()
{
return
true
;
}
private
static
String
getRowNumber
(
String
sql
)
{
StringBuilder
rownumber
=
new
StringBuilder
(
50
)
.
append
(
"rownumber() over("
);
int
orderByIndex
=
sql
.
toLowerCase
().
indexOf
(
"order by"
);
if
(
orderByIndex
>
0
&&
!
hasDistinct
(
sql
))
{
rownumber
.
append
(
sql
.
substring
(
orderByIndex
));
}
rownumber
.
append
(
") as rownumber_,"
);
return
rownumber
.
toString
();
}
private
static
boolean
hasDistinct
(
String
sql
)
{
return
sql
.
toLowerCase
().
contains
(
"select distinct"
);
}
@Override
public
String
getLimitString
(
String
sql
,
int
offset
,
int
limit
)
{
return
getLimitString
(
sql
,
offset
,
Integer
.
toString
(
offset
),
Integer
.
toString
(
limit
));
}
/**
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
* <pre>
* 如mysql
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
* select * from user limit :offset,:limit
* </pre>
*
* @param sql 实际SQL语句
* @param offset 分页开始纪录条数
* @param offsetPlaceholder 分页开始纪录条数-占位符号
* @param limitPlaceholder 分页纪录条数占位符号
* @return 包含占位符的分页sql
*/
public
String
getLimitString
(
String
sql
,
int
offset
,
String
offsetPlaceholder
,
String
limitPlaceholder
)
{
int
startOfSelect
=
sql
.
toLowerCase
().
indexOf
(
"select"
);
StringBuilder
pagingSelect
=
new
StringBuilder
(
sql
.
length
()
+
100
)
.
append
(
sql
.
substring
(
0
,
startOfSelect
))
//add the comment
.
append
(
"select * from ( select "
)
//nest the main query in an outer select
.
append
(
getRowNumber
(
sql
));
//add the rownnumber bit into the outer query select list
if
(
hasDistinct
(
sql
))
{
pagingSelect
.
append
(
" row_.* from ( "
)
//add another (inner) nested select
.
append
(
sql
.
substring
(
startOfSelect
))
//add the main query
.
append
(
" ) as row_"
);
//close off the inner nested select
}
else
{
pagingSelect
.
append
(
sql
.
substring
(
startOfSelect
+
6
));
//add the main query
}
pagingSelect
.
append
(
" ) as temp_ where rownumber_ "
);
//add the restriction to the outer select
if
(
offset
>
0
)
{
// int end = offset + limit;
String
endString
=
offsetPlaceholder
+
"+"
+
limitPlaceholder
;
pagingSelect
.
append
(
"between "
).
append
(
offsetPlaceholder
)
.
append
(
"+1 and "
).
append
(
endString
);
}
else
{
pagingSelect
.
append
(
"<= "
).
append
(
limitPlaceholder
);
}
return
pagingSelect
.
toString
();
}
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/dialect/DerbyDialect.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.dialect
;
import
com.jeespring.common.persistence.dialect.Dialect
;
/**
* @author poplar.yfyang
* @version 1.0 2010-10-10 下午12:31
* @since JDK 1.5
*/
public
class
DerbyDialect
implements
Dialect
{
@Override
public
boolean
supportsLimit
()
{
return
false
;
}
@Override
public
String
getLimitString
(
String
sql
,
int
offset
,
int
limit
)
{
// return getLimitString(sql,offset,Integer.toString(offset),limit,Integer.toString(limit));
throw
new
UnsupportedOperationException
(
"paged queries not supported"
);
}
/**
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
* <pre>
* 如mysql
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
* select * from user limit :offset,:limit
* </pre>
*
* @param sql 实际SQL语句
* @param offset 分页开始纪录条数
* @param offsetPlaceholder 分页开始纪录条数-占位符号
* @param limit 分页每页显示纪录条数
* @param limitPlaceholder 分页纪录条数占位符号
* @return 包含占位符的分页sql
*/
public
String
getLimitString
(
String
sql
,
int
offset
,
String
offsetPlaceholder
,
int
limit
,
String
limitPlaceholder
)
{
throw
new
UnsupportedOperationException
(
"paged queries not supported"
);
}
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/dialect/Dialect.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.dialect
;
/**
* 类似hibernate的Dialect,但只精简出分页部分
*
* @author poplar.yfyang
* @version 1.0 2011-11-18 下午12:31
* @since JDK 1.5
*/
public
interface
Dialect
{
/**
* 数据库本身是否支持分页当前的分页查询方式
* 如果数据库不支持的话,则不进行数据库分页
*
* @return true:支持当前的分页查询方式
*/
boolean
supportsLimit
();
/**
* 将sql转换为分页SQL,分别调用分页sql
*
* @param sql SQL语句
* @param offset 开始条数
* @param limit 每页显示多少纪录条数
* @return 分页查询的sql
*/
String
getLimitString
(
String
sql
,
int
offset
,
int
limit
);
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/dialect/H2Dialect.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.dialect
;
import
com.jeespring.common.persistence.dialect.Dialect
;
/**
* A dialect compatible with the H2 database.
*
* @author JeeSpring
* @version 1.0 2010-10-10 下午12:31
* @since JDK 1.5
*/
public
class
H2Dialect
implements
Dialect
{
@Override
public
boolean
supportsLimit
()
{
return
true
;
}
/**
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
* <pre>
* 如mysql
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
* select * from user limit :offset,:limit
* </pre>
*
* @param sql 实际SQL语句
* @param offset 分页开始纪录条数
* @param offsetPlaceholder 分页开始纪录条数-占位符号
* @param limit 分页每页显示纪录条数
* @param limitPlaceholder 分页纪录条数占位符号
* @return 包含占位符的分页sql
*/
private
String
getLimitString
(
String
sql
,
int
offset
,
String
offsetPlaceholder
,
int
limit
,
String
limitPlaceholder
)
{
return
sql
+
((
offset
>
0
)
?
" limit "
+
limitPlaceholder
+
" offset "
+
offsetPlaceholder
:
" limit "
+
limitPlaceholder
);
}
@Override
public
String
getLimitString
(
String
sql
,
int
offset
,
int
limit
)
{
return
getLimitString
(
sql
,
offset
,
Integer
.
toString
(
offset
),
limit
,
Integer
.
toString
(
limit
));
}
}
\ No newline at end of file
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/dialect/HSQLDialect.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.dialect
;
import
com.jeespring.common.persistence.dialect.Dialect
;
/**
* Dialect for HSQLDB
*
* @author poplar.yfyang
* @version 1.0 2010-10-10 下午12:31
* @since JDK 1.5
*/
public
class
HSQLDialect
implements
Dialect
{
@Override
public
boolean
supportsLimit
()
{
return
true
;
}
@Override
public
String
getLimitString
(
String
sql
,
int
offset
,
int
limit
)
{
return
getLimitString
(
sql
,
offset
,
Integer
.
toString
(
offset
),
Integer
.
toString
(
limit
));
}
/**
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
* <pre>
* 如mysql
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
* select * from user limit :offset,:limit
* </pre>
*
* @param sql 实际SQL语句
* @param offset 分页开始纪录条数
* @param offsetPlaceholder 分页开始纪录条数-占位符号
* @param limitPlaceholder 分页纪录条数占位符号
* @return 包含占位符的分页sql
*/
public
String
getLimitString
(
String
sql
,
int
offset
,
String
offsetPlaceholder
,
String
limitPlaceholder
)
{
boolean
hasOffset
=
offset
>
0
;
return
new
StringBuffer
(
sql
.
length
()
+
10
)
.
append
(
sql
)
.
insert
(
sql
.
toLowerCase
().
indexOf
(
"select"
)
+
6
,
hasOffset
?
" limit "
+
offsetPlaceholder
+
" "
+
limitPlaceholder
:
" top "
+
limitPlaceholder
)
.
toString
();
}
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/dialect/MySQLDialect.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.dialect
;
import
com.jeespring.common.persistence.dialect.Dialect
;
/**
* Mysql方言的实现
*
* @author poplar.yfyang
* @version 1.0 2010-10-10 下午12:31
* @since JDK 1.5
*/
public
class
MySQLDialect
implements
Dialect
{
@Override
public
String
getLimitString
(
String
sql
,
int
offset
,
int
limit
)
{
return
getLimitString
(
sql
,
offset
,
Integer
.
toString
(
offset
),
Integer
.
toString
(
limit
));
}
@Override
public
boolean
supportsLimit
()
{
return
true
;
}
/**
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
* <pre>
* 如mysql
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
* select * from user limit :offset,:limit
* </pre>
*
* @param sql 实际SQL语句
* @param offset 分页开始纪录条数
* @param offsetPlaceholder 分页开始纪录条数-占位符号
* @param limitPlaceholder 分页纪录条数占位符号
* @return 包含占位符的分页sql
*/
public
String
getLimitString
(
String
sql
,
int
offset
,
String
offsetPlaceholder
,
String
limitPlaceholder
)
{
StringBuilder
stringBuilder
=
new
StringBuilder
(
sql
);
stringBuilder
.
append
(
" limit "
);
if
(
offset
>
0
)
{
stringBuilder
.
append
(
offsetPlaceholder
).
append
(
","
).
append
(
limitPlaceholder
);
}
else
{
stringBuilder
.
append
(
limitPlaceholder
);
}
return
stringBuilder
.
toString
();
}
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/dialect/OracleDialect.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.dialect
;
/**
* Oracle的方言实现
* @author poplar.yfyang
* @version 1.0 2010-10-10 下午12:31
* @since JDK 1.5
*/
public
class
OracleDialect
implements
Dialect
{
@Override
public
boolean
supportsLimit
()
{
return
true
;
}
@Override
public
String
getLimitString
(
String
sql
,
int
offset
,
int
limit
)
{
return
getLimitString
(
sql
,
offset
,
Integer
.
toString
(
offset
),
Integer
.
toString
(
limit
));
}
/**
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
* <pre>
* 如mysql
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
* select * from user limit :offset,:limit
* </pre>
*
* @param sql 实际SQL语句
* @param offset 分页开始纪录条数
* @param offsetPlaceholder 分页开始纪录条数-占位符号
* @param limitPlaceholder 分页纪录条数占位符号
* @return 包含占位符的分页sql
*/
public
String
getLimitString
(
String
sql
,
int
offset
,
String
offsetPlaceholder
,
String
limitPlaceholder
)
{
sql
=
sql
.
trim
();
boolean
isForUpdate
=
false
;
if
(
sql
.
toLowerCase
().
endsWith
(
" for update"
))
{
sql
=
sql
.
substring
(
0
,
sql
.
length
()
-
11
);
isForUpdate
=
true
;
}
StringBuilder
pagingSelect
=
new
StringBuilder
(
sql
.
length
()
+
100
);
if
(
offset
>
0
)
{
pagingSelect
.
append
(
"select * from ( select row_.*, rownum rownum_ from ( "
);
}
else
{
pagingSelect
.
append
(
"select * from ( "
);
}
pagingSelect
.
append
(
sql
);
if
(
offset
>
0
)
{
String
endString
=
offsetPlaceholder
+
"+"
+
limitPlaceholder
;
pagingSelect
.
append
(
" ) row_ where rownum <= "
+
endString
+
") where rownum_ > "
).
append
(
offsetPlaceholder
);
}
else
{
pagingSelect
.
append
(
" ) where rownum <= "
+
limitPlaceholder
);
}
if
(
isForUpdate
)
{
pagingSelect
.
append
(
" for update"
);
}
return
pagingSelect
.
toString
();
}
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/dialect/PostgreSQLDialect.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.dialect
;
import
com.jeespring.common.persistence.dialect.Dialect
;
/**
* Postgre Sql的方言实现
* @author poplar.yfyang
* @version 1.0 2010-10-10 下午12:31
* @since JDK 1.5
*/
public
class
PostgreSQLDialect
implements
Dialect
{
@Override
public
boolean
supportsLimit
()
{
return
true
;
}
@Override
public
String
getLimitString
(
String
sql
,
int
offset
,
int
limit
)
{
return
getLimitString
(
sql
,
offset
,
Integer
.
toString
(
offset
),
Integer
.
toString
(
limit
));
}
/**
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
* <pre>
* 如mysql
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
* select * from user limit :offset,:limit
* </pre>
*
* @param sql 实际SQL语句
* @param offset 分页开始纪录条数
* @param offsetPlaceholder 分页开始纪录条数-占位符号
* @param limitPlaceholder 分页纪录条数占位符号
* @return 包含占位符的分页sql
*/
public
String
getLimitString
(
String
sql
,
int
offset
,
String
offsetPlaceholder
,
String
limitPlaceholder
)
{
StringBuilder
pageSql
=
new
StringBuilder
().
append
(
sql
);
pageSql
=
offset
<=
0
?
pageSql
.
append
(
" limit "
).
append
(
limitPlaceholder
)
:
pageSql
.
append
(
" limit "
).
append
(
limitPlaceholder
).
append
(
" offset "
).
append
(
offsetPlaceholder
);
return
pageSql
.
toString
();
}
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/dialect/SQLServer2005Dialect.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.dialect
;
import
org.apache.commons.lang3.StringUtils
;
import
com.jeespring.common.persistence.dialect.Dialect
;
/**
* Sql 2005的方言实现
* @author poplar.yfyang
* @version 1.0 2010-10-10 下午12:31
* @since JDK 1.5
*/
public
class
SQLServer2005Dialect
implements
Dialect
{
@Override
public
boolean
supportsLimit
()
{
return
true
;
}
@Override
public
String
getLimitString
(
String
sql
,
int
offset
,
int
limit
)
{
return
getLimitString
(
sql
,
offset
,
limit
,
Integer
.
toString
(
limit
));
}
/**
* Add a LIMIT clause to the given SQL SELECT
* <p/>
* The LIMIT SQL will look like:
* <p/>
* WITH query AS
* (SELECT TOP 100 percent ROW_NUMBER() OVER (ORDER BY CURRENT_TIMESTAMP) as __row_number__, * from table_name)
* SELECT *
* FROM query
* WHERE __row_number__ BETWEEN :offset and :lastRows
* ORDER BY __row_number__
*
* @param querySqlString The SQL statement to base the limit query off of.
* @param offset Offset of the first row to be returned by the query (zero-based)
* @param limit Maximum number of rows to be returned by the query
* @param limitPlaceholder limitPlaceholder
* @return A new SQL statement with the LIMIT clause applied.
*/
private
String
getLimitString
(
String
querySqlString
,
int
offset
,
int
limit
,
String
limitPlaceholder
)
{
StringBuilder
pagingBuilder
=
new
StringBuilder
();
String
orderby
=
getOrderByPart
(
querySqlString
);
String
distinctStr
=
""
;
String
loweredString
=
querySqlString
.
toLowerCase
();
String
sqlPartString
=
querySqlString
;
if
(
loweredString
.
trim
().
startsWith
(
"select"
))
{
int
index
=
6
;
if
(
loweredString
.
startsWith
(
"select distinct"
))
{
distinctStr
=
"DISTINCT "
;
index
=
15
;
}
sqlPartString
=
sqlPartString
.
substring
(
index
);
}
pagingBuilder
.
append
(
sqlPartString
);
// if no ORDER BY is specified use fake ORDER BY field to avoid errors
if
(
StringUtils
.
isEmpty
(
orderby
))
{
orderby
=
"ORDER BY CURRENT_TIMESTAMP"
;
}
StringBuilder
result
=
new
StringBuilder
();
result
.
append
(
"WITH query AS (SELECT "
)
.
append
(
distinctStr
)
.
append
(
"TOP 100 PERCENT "
)
.
append
(
" ROW_NUMBER() OVER ("
)
.
append
(
orderby
)
.
append
(
") as __row_number__, "
)
.
append
(
pagingBuilder
)
.
append
(
") SELECT * FROM query WHERE __row_number__ BETWEEN "
)
.
append
(
offset
+
1
).
append
(
" AND "
).
append
(
offset
+
limit
)
.
append
(
" ORDER BY __row_number__"
);
return
result
.
toString
();
}
static
String
getOrderByPart
(
String
sql
)
{
String
loweredString
=
sql
.
toLowerCase
();
int
orderByIndex
=
loweredString
.
indexOf
(
"order by"
);
if
(
orderByIndex
!=
-
1
)
{
// if we find a new "order by" then we need to ignore
// the previous one since it was probably used for a subquery
return
sql
.
substring
(
orderByIndex
);
}
else
{
return
""
;
}
}
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/dialect/SQLServerDialect.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.dialect
;
import
com.jeespring.common.persistence.dialect.Dialect
;
/**
* MSSQLServer 数据库实现分页方言
*
* @author poplar.yfyang
* @version 1.0 2010-10-10 下午12:31
* @since JDK 1.5
*/
public
class
SQLServerDialect
implements
Dialect
{
@Override
public
boolean
supportsLimit
()
{
return
true
;
}
static
int
getAfterSelectInsertPoint
(
String
sql
)
{
int
selectIndex
=
sql
.
toLowerCase
().
indexOf
(
"select"
);
final
int
selectDistinctIndex
=
sql
.
toLowerCase
().
indexOf
(
"select distinct"
);
return
selectIndex
+
(
selectDistinctIndex
==
selectIndex
?
15
:
6
);
}
@Override
public
String
getLimitString
(
String
sql
,
int
offset
,
int
limit
)
{
return
getLimit
(
sql
,
offset
,
limit
);
}
/**
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
* <pre>
* 如mysql
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
* select * from user limit :offset,:limit
* </pre>
*
* @param sql 实际SQL语句
* @param offset 分页开始纪录条数
* @param limit 分页每页显示纪录条数
* @return 包含占位符的分页sql
*/
public
String
getLimit
(
String
sql
,
int
offset
,
int
limit
)
{
if
(
offset
>
0
)
{
throw
new
UnsupportedOperationException
(
"sql server has no offset"
);
}
return
new
StringBuffer
(
sql
.
length
()
+
8
)
.
append
(
sql
)
.
insert
(
getAfterSelectInsertPoint
(
sql
),
" top "
+
limit
)
.
toString
();
}
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/dialect/SybaseDialect.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.dialect
;
import
com.jeespring.common.persistence.dialect.Dialect
;
/**
* Sybase数据库分页方言实现。
* 还未实现
*
* @author poplar.yfyang
* @version 1.0 2010-10-10 下午12:31
* @since JDK 1.5
*/
public
class
SybaseDialect
implements
Dialect
{
@Override
public
boolean
supportsLimit
()
{
return
false
;
}
@Override
public
String
getLimitString
(
String
sql
,
int
offset
,
int
limit
)
{
return
null
;
}
/**
* 将sql变成分页sql语句,提供将offset及limit使用占位符号(placeholder)替换.
* <pre>
* 如mysql
* dialect.getLimitString("select * from user", 12, ":offset",0,":limit") 将返回
* select * from user limit :offset,:limit
* </pre>
*
* @param sql 实际SQL语句
* @param offset 分页开始纪录条数
* @param offsetPlaceholder 分页开始纪录条数-占位符号
* @param limit 分页每页显示纪录条数
* @param limitPlaceholder 分页纪录条数占位符号
* @return 包含占位符的分页sql
*/
public
String
getLimitString
(
String
sql
,
int
offset
,
String
offsetPlaceholder
,
int
limit
,
String
limitPlaceholder
)
{
throw
new
UnsupportedOperationException
(
"paged queries not supported"
);
}
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/interceptor/BaseInterceptor.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.interceptor
;
import
com.jeespring.common.config.Global
;
import
com.jeespring.common.persistence.Page
;
import
com.jeespring.common.persistence.dialect.*
;
import
com.jeespring.common.utils.Reflections
;
import
org.apache.ibatis.logging.Log
;
import
org.apache.ibatis.logging.LogFactory
;
import
org.apache.ibatis.plugin.Interceptor
;
import
java.io.Serializable
;
import
java.util.Properties
;
/**
* Mybatis分页拦截器基类
*
* @author poplar.yfyang / HuangBingGui
* @version 2013-8-28
*/
public
abstract
class
BaseInterceptor
implements
Interceptor
,
Serializable
{
private
static
final
long
serialVersionUID
=
1L
;
protected
static
final
String
PAGE
=
"page"
;
protected
static
final
String
DELEGATE
=
"delegate"
;
protected
static
final
String
MAPPED_STATEMENT
=
"mappedStatement"
;
protected
Log
log
=
LogFactory
.
getLog
(
this
.
getClass
());
protected
Dialect
DIALECT
;
/**
* 对参数进行转换和检查
*
* @param parameterObject 参数对象
* @param page 分页对象
* @return 分页对象
* @throws NoSuchFieldException 无法找到参数
*/
@SuppressWarnings
(
"unchecked"
)
protected
static
Page
<
Object
>
convertParameter
(
Object
parameterObject
,
Page
<
Object
>
page
)
{
try
{
if
(
parameterObject
instanceof
Page
)
{
return
(
Page
<
Object
>)
parameterObject
;
}
else
{
return
(
Page
<
Object
>)
Reflections
.
getFieldValue
(
parameterObject
,
PAGE
);
}
}
catch
(
Exception
e
)
{
return
null
;
}
}
/**
* 设置属性,支持自定义方言类和制定数据库的方式
* <code>dialectClass</code>,自定义方言类。可以不配置这项
* <ode>dbms</ode> 数据库类型,插件支持的数据库
* <code>sqlPattern</code> 需要拦截的SQL ID
*
* @param p 属性
*/
protected
void
initProperties
(
Properties
p
)
{
Dialect
dialect
=
null
;
String
dbType
=
Global
.
getJdbcType
();
if
(
"db2"
.
equals
(
dbType
))
{
dialect
=
new
DB2Dialect
();
}
else
if
(
"derby"
.
equals
(
dbType
))
{
dialect
=
new
DerbyDialect
();
}
else
if
(
"h2"
.
equals
(
dbType
))
{
dialect
=
new
H2Dialect
();
}
else
if
(
"hsql"
.
equals
(
dbType
))
{
dialect
=
new
HSQLDialect
();
}
else
if
(
"mysql"
.
equals
(
dbType
))
{
dialect
=
new
MySQLDialect
();
}
else
if
(
"oracle"
.
equals
(
dbType
))
{
dialect
=
new
OracleDialect
();
}
else
if
(
"postgre"
.
equals
(
dbType
))
{
dialect
=
new
PostgreSQLDialect
();
}
else
if
(
"mssql"
.
equals
(
dbType
)
||
"sqlserver"
.
equals
(
dbType
))
{
dialect
=
new
SQLServer2005Dialect
();
}
else
if
(
"sybase"
.
equals
(
dbType
))
{
dialect
=
new
SybaseDialect
();
}
if
(
dialect
==
null
)
{
log
.
error
(
"user the mysql dialect"
);
dialect
=
new
MySQLDialect
();
}
DIALECT
=
dialect
;
}
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/interceptor/PaginationInterceptor.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.interceptor
;
import
java.util.Properties
;
import
org.apache.ibatis.executor.Executor
;
import
org.apache.ibatis.mapping.BoundSql
;
import
org.apache.ibatis.mapping.MappedStatement
;
import
org.apache.ibatis.mapping.SqlSource
;
import
org.apache.ibatis.plugin.Intercepts
;
import
org.apache.ibatis.plugin.Invocation
;
import
org.apache.ibatis.plugin.Plugin
;
import
org.apache.ibatis.plugin.Signature
;
import
org.apache.ibatis.reflection.MetaObject
;
import
org.apache.ibatis.session.ResultHandler
;
import
org.apache.ibatis.session.RowBounds
;
import
com.jeespring.common.persistence.Page
;
import
com.jeespring.common.utils.Reflections
;
import
com.jeespring.common.utils.StringUtils
;
/**
* 数据库分页插件,只拦截查询语句.
* @author poplar.yfyang / HuangBingGui
* @version 2013-8-28
*/
@Intercepts
({
@Signature
(
type
=
Executor
.
class
,
method
=
"query"
,
args
=
{
MappedStatement
.
class
,
Object
.
class
,
RowBounds
.
class
,
ResultHandler
.
class
})})
public
class
PaginationInterceptor
extends
BaseInterceptor
{
private
static
final
long
serialVersionUID
=
1L
;
@Override
public
Object
intercept
(
Invocation
invocation
)
throws
Throwable
{
final
MappedStatement
mappedStatement
=
(
MappedStatement
)
invocation
.
getArgs
()[
0
];
// //拦截需要分页的SQL
//// if (mappedStatement.getId().matches(_SQL_PATTERN)) {
// if (StringUtils.indexOfIgnoreCase(mappedStatement.getId(), _SQL_PATTERN) != -1) {
Object
parameter
=
invocation
.
getArgs
()[
1
];
BoundSql
boundSql
=
mappedStatement
.
getBoundSql
(
parameter
);
Object
parameterObject
=
boundSql
.
getParameterObject
();
//获取分页参数对象
Page
<
Object
>
page
=
null
;
if
(
parameterObject
!=
null
)
{
page
=
convertParameter
(
parameterObject
,
page
);
}
//如果设置了分页对象,则进行分页
if
(
page
!=
null
&&
page
.
getPageSize
()
!=
-
1
)
{
if
(
StringUtils
.
isBlank
(
boundSql
.
getSql
())){
return
null
;
}
String
originalSql
=
boundSql
.
getSql
().
trim
();
//得到总记录数
page
.
setCount
(
SQLHelper
.
getCount
(
originalSql
,
null
,
mappedStatement
,
parameterObject
,
boundSql
,
log
));
//分页查询 本地化对象 修改数据库注意修改实现
String
pageSql
=
SQLHelper
.
generatePageSql
(
originalSql
,
page
,
DIALECT
);
// if (log.isDebugEnabled()) {
// log.debug("PAGE SQL:" + StringUtils.replace(pageSql, "\n", ""));
// }
invocation
.
getArgs
()[
2
]
=
new
RowBounds
(
RowBounds
.
NO_ROW_OFFSET
,
RowBounds
.
NO_ROW_LIMIT
);
BoundSql
newBoundSql
=
new
BoundSql
(
mappedStatement
.
getConfiguration
(),
pageSql
,
boundSql
.
getParameterMappings
(),
boundSql
.
getParameterObject
());
//解决MyBatis 分页foreach 参数失效 start
if
(
Reflections
.
getFieldValue
(
boundSql
,
"metaParameters"
)
!=
null
)
{
MetaObject
mo
=
(
MetaObject
)
Reflections
.
getFieldValue
(
boundSql
,
"metaParameters"
);
Reflections
.
setFieldValue
(
newBoundSql
,
"metaParameters"
,
mo
);
}
//解决MyBatis 分页foreach 参数失效 end
MappedStatement
newMs
=
copyFromMappedStatement
(
mappedStatement
,
new
BoundSqlSqlSource
(
newBoundSql
));
invocation
.
getArgs
()[
0
]
=
newMs
;
}
// }
return
invocation
.
proceed
();
}
@Override
public
Object
plugin
(
Object
target
)
{
return
Plugin
.
wrap
(
target
,
this
);
}
@Override
public
void
setProperties
(
Properties
properties
)
{
super
.
initProperties
(
properties
);
}
private
MappedStatement
copyFromMappedStatement
(
MappedStatement
ms
,
SqlSource
newSqlSource
)
{
MappedStatement
.
Builder
builder
=
new
MappedStatement
.
Builder
(
ms
.
getConfiguration
(),
ms
.
getId
(),
newSqlSource
,
ms
.
getSqlCommandType
());
builder
.
resource
(
ms
.
getResource
());
builder
.
fetchSize
(
ms
.
getFetchSize
());
builder
.
statementType
(
ms
.
getStatementType
());
builder
.
keyGenerator
(
ms
.
getKeyGenerator
());
if
(
ms
.
getKeyProperties
()
!=
null
)
{
for
(
String
keyProperty
:
ms
.
getKeyProperties
())
{
builder
.
keyProperty
(
keyProperty
);
}
}
builder
.
timeout
(
ms
.
getTimeout
());
builder
.
parameterMap
(
ms
.
getParameterMap
());
builder
.
resultMaps
(
ms
.
getResultMaps
());
builder
.
cache
(
ms
.
getCache
());
builder
.
useCache
(
ms
.
isUseCache
());
return
builder
.
build
();
}
public
static
class
BoundSqlSqlSource
implements
SqlSource
{
BoundSql
boundSql
;
public
BoundSqlSqlSource
(
BoundSql
boundSql
)
{
this
.
boundSql
=
boundSql
;
}
@Override
public
BoundSql
getBoundSql
(
Object
parameterObject
)
{
return
boundSql
;
}
}
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/interceptor/PreparePaginationInterceptor.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.interceptor
;
import
java.sql.Connection
;
import
java.util.Properties
;
import
org.apache.ibatis.executor.statement.BaseStatementHandler
;
import
org.apache.ibatis.executor.statement.RoutingStatementHandler
;
import
org.apache.ibatis.executor.statement.StatementHandler
;
import
org.apache.ibatis.mapping.BoundSql
;
import
org.apache.ibatis.mapping.MappedStatement
;
import
org.apache.ibatis.plugin.Intercepts
;
import
org.apache.ibatis.plugin.Invocation
;
import
org.apache.ibatis.plugin.Plugin
;
import
org.apache.ibatis.plugin.Signature
;
import
com.jeespring.common.persistence.Page
;
import
com.jeespring.common.utils.Reflections
;
/**
* Mybatis数据库分页插件,拦截StatementHandler的prepare方法
* @author poplar.yfyang / HuangBingGui
* @version 2013-8-28
*/
@Intercepts
({
@Signature
(
type
=
StatementHandler
.
class
,
method
=
"prepare"
,
args
=
{
Connection
.
class
})
})
public
class
PreparePaginationInterceptor
extends
BaseInterceptor
{
private
static
final
long
serialVersionUID
=
1L
;
public
PreparePaginationInterceptor
()
{
super
();
}
@Override
public
Object
intercept
(
Invocation
ivk
)
throws
Throwable
{
if
(
ivk
.
getTarget
().
getClass
().
isAssignableFrom
(
RoutingStatementHandler
.
class
))
{
final
RoutingStatementHandler
statementHandler
=
(
RoutingStatementHandler
)
ivk
.
getTarget
();
final
BaseStatementHandler
delegate
=
(
BaseStatementHandler
)
Reflections
.
getFieldValue
(
statementHandler
,
DELEGATE
);
final
MappedStatement
mappedStatement
=
(
MappedStatement
)
Reflections
.
getFieldValue
(
delegate
,
MAPPED_STATEMENT
);
// //拦截需要分页的SQL
//// if (mappedStatement.getId().matches(_SQL_PATTERN)) {
// if (StringUtils.indexOfIgnoreCase(mappedStatement.getId(), _SQL_PATTERN) != -1) {
BoundSql
boundSql
=
delegate
.
getBoundSql
();
//分页SQL<select>中parameterType属性对应的实体参数,即Mapper接口中执行分页方法的参数,该参数不得为空
Object
parameterObject
=
boundSql
.
getParameterObject
();
if
(
parameterObject
==
null
)
{
log
.
error
(
"参数未实例化"
);
throw
new
NullPointerException
(
"parameterObject尚未实例化!"
);
}
else
{
final
Connection
connection
=
(
Connection
)
ivk
.
getArgs
()[
0
];
final
String
sql
=
boundSql
.
getSql
();
//记录统计
final
int
count
=
SQLHelper
.
getCount
(
sql
,
connection
,
mappedStatement
,
parameterObject
,
boundSql
,
log
);
Page
<
Object
>
page
=
null
;
page
=
convertParameter
(
parameterObject
,
page
);
page
.
setCount
(
count
);
String
pagingSql
=
SQLHelper
.
generatePageSql
(
sql
,
page
,
DIALECT
);
if
(
log
.
isDebugEnabled
())
{
log
.
debug
(
"PAGE SQL:"
+
pagingSql
);
}
//将分页sql语句反射回BoundSql.
Reflections
.
setFieldValue
(
boundSql
,
"sql"
,
pagingSql
);
}
if
(
boundSql
.
getSql
()
==
null
||
""
.
equals
(
boundSql
.
getSql
())){
return
null
;
}
}
// }
return
ivk
.
proceed
();
}
@Override
public
Object
plugin
(
Object
o
)
{
return
Plugin
.
wrap
(
o
,
this
);
}
@Override
public
void
setProperties
(
Properties
properties
)
{
initProperties
(
properties
);
}
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/interceptor/SQLHelper.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.interceptor
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.SQLException
;
import
java.util.List
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
org.apache.ibatis.executor.ErrorContext
;
import
org.apache.ibatis.executor.ExecutorException
;
import
org.apache.ibatis.logging.Log
;
import
org.apache.ibatis.mapping.BoundSql
;
import
org.apache.ibatis.mapping.MappedStatement
;
import
org.apache.ibatis.mapping.ParameterMapping
;
import
org.apache.ibatis.mapping.ParameterMode
;
import
org.apache.ibatis.reflection.MetaObject
;
import
org.apache.ibatis.reflection.property.PropertyTokenizer
;
import
org.apache.ibatis.scripting.xmltags.ForEachSqlNode
;
import
org.apache.ibatis.session.Configuration
;
import
org.apache.ibatis.type.TypeHandler
;
import
org.apache.ibatis.type.TypeHandlerRegistry
;
import
com.jeespring.common.config.Global
;
import
com.jeespring.common.persistence.Page
;
import
com.jeespring.common.persistence.dialect.Dialect
;
import
com.jeespring.common.utils.Reflections
;
import
com.jeespring.common.utils.StringUtils
;
/**
* SQL工具类
*
* @author poplar.yfyang / HuangBingGui
* @version 2013-8-28
*/
public
class
SQLHelper
{
/**
* 对SQL参数(?)设值,参考org.apache.ibatis.executor.parameter.DefaultParameterHandler
*
* @param ps 表示预编译的 SQL 语句的对象。
* @param mappedStatement MappedStatement
* @param boundSql SQL
* @param parameterObject 参数对象
* @throws SQLException 数据库异常
*/
@SuppressWarnings
(
"unchecked"
)
public
static
void
setParameters
(
PreparedStatement
ps
,
MappedStatement
mappedStatement
,
BoundSql
boundSql
,
Object
parameterObject
)
throws
SQLException
{
ErrorContext
.
instance
().
activity
(
"setting parameters"
).
object
(
mappedStatement
.
getParameterMap
().
getId
());
List
<
ParameterMapping
>
parameterMappings
=
boundSql
.
getParameterMappings
();
if
(
parameterMappings
!=
null
)
{
Configuration
configuration
=
mappedStatement
.
getConfiguration
();
TypeHandlerRegistry
typeHandlerRegistry
=
configuration
.
getTypeHandlerRegistry
();
MetaObject
metaObject
=
parameterObject
==
null
?
null
:
configuration
.
newMetaObject
(
parameterObject
);
for
(
int
i
=
0
;
i
<
parameterMappings
.
size
();
i
++)
{
ParameterMapping
parameterMapping
=
parameterMappings
.
get
(
i
);
if
(
parameterMapping
.
getMode
()
!=
ParameterMode
.
OUT
)
{
Object
value
;
String
propertyName
=
parameterMapping
.
getProperty
();
PropertyTokenizer
prop
=
new
PropertyTokenizer
(
propertyName
);
if
(
parameterObject
==
null
)
{
value
=
null
;
}
else
if
(
typeHandlerRegistry
.
hasTypeHandler
(
parameterObject
.
getClass
()))
{
value
=
parameterObject
;
}
else
if
(
boundSql
.
hasAdditionalParameter
(
propertyName
))
{
value
=
boundSql
.
getAdditionalParameter
(
propertyName
);
}
else
if
(
propertyName
.
startsWith
(
ForEachSqlNode
.
ITEM_PREFIX
)
&&
boundSql
.
hasAdditionalParameter
(
prop
.
getName
()))
{
value
=
boundSql
.
getAdditionalParameter
(
prop
.
getName
());
if
(
value
!=
null
)
{
value
=
configuration
.
newMetaObject
(
value
).
getValue
(
propertyName
.
substring
(
prop
.
getName
().
length
()));
}
}
else
{
value
=
metaObject
==
null
?
null
:
metaObject
.
getValue
(
propertyName
);
}
@SuppressWarnings
(
"rawtypes"
)
TypeHandler
typeHandler
=
parameterMapping
.
getTypeHandler
();
if
(
typeHandler
==
null
)
{
throw
new
ExecutorException
(
"There was no TypeHandler found for parameter "
+
propertyName
+
" of statement "
+
mappedStatement
.
getId
());
}
typeHandler
.
setParameter
(
ps
,
i
+
1
,
value
,
parameterMapping
.
getJdbcType
());
}
}
}
}
/**
* 查询总纪录数
*
* @param sql SQL语句
* @param connection 数据库连接
* @param mappedStatement mapped
* @param parameterObject 参数
* @param boundSql boundSql
* @return 总记录数
* @throws SQLException sql查询错误
*/
public
static
int
getCount
(
final
String
sql
,
final
Connection
connection
,
final
MappedStatement
mappedStatement
,
final
Object
parameterObject
,
final
BoundSql
boundSql
,
Log
log
)
throws
SQLException
{
String
dbName
=
Global
.
getJdbcType
();
final
String
countSql
;
if
(
"oracle"
.
equals
(
dbName
))
{
countSql
=
"select count(1) from ("
+
sql
+
") tmp_count"
;
}
else
{
countSql
=
"select count(1) from ("
+
removeOrders
(
sql
)
+
") tmp_count"
;
}
Connection
conn
=
connection
;
PreparedStatement
ps
=
null
;
ResultSet
rs
=
null
;
try
{
if
(
log
.
isDebugEnabled
())
{
log
.
debug
(
"COUNT SQL: "
+
StringUtils
.
replaceEach
(
countSql
,
new
String
[]{
"\n"
,
"\t"
},
new
String
[]{
" "
,
" "
}));
}
if
(
conn
==
null
)
{
conn
=
mappedStatement
.
getConfiguration
().
getEnvironment
().
getDataSource
().
getConnection
();
}
ps
=
conn
.
prepareStatement
(
countSql
);
BoundSql
countBS
=
new
BoundSql
(
mappedStatement
.
getConfiguration
(),
countSql
,
boundSql
.
getParameterMappings
(),
parameterObject
);
//解决MyBatis 分页foreach 参数失效 start
if
(
Reflections
.
getFieldValue
(
boundSql
,
"metaParameters"
)
!=
null
)
{
MetaObject
mo
=
(
MetaObject
)
Reflections
.
getFieldValue
(
boundSql
,
"metaParameters"
);
Reflections
.
setFieldValue
(
countBS
,
"metaParameters"
,
mo
);
}
//解决MyBatis 分页foreach 参数失效 end
SQLHelper
.
setParameters
(
ps
,
mappedStatement
,
countBS
,
parameterObject
);
rs
=
ps
.
executeQuery
();
int
count
=
0
;
if
(
rs
.
next
())
{
count
=
rs
.
getInt
(
1
);
}
return
count
;
}
finally
{
if
(
rs
!=
null
)
{
rs
.
close
();
}
if
(
ps
!=
null
)
{
ps
.
close
();
}
if
(
conn
!=
null
)
{
conn
.
close
();
}
}
}
/**
* 根据数据库方言,生成特定的分页sql
*
* @param sql Mapper中的Sql语句
* @param page 分页对象
* @param dialect 方言类型
* @return 分页SQL
*/
public
static
String
generatePageSql
(
String
sql
,
Page
<
Object
>
page
,
Dialect
dialect
)
{
if
(
dialect
.
supportsLimit
())
{
return
dialect
.
getLimitString
(
sql
,
page
.
getFirstResult
(),
page
.
getMaxResults
());
}
else
{
return
sql
;
}
}
/**
* 去除qlString的select子句。
*
* @param qlString
* @return
*/
@SuppressWarnings
(
"unused"
)
private
static
String
removeSelect
(
String
qlString
)
{
int
beginPos
=
qlString
.
toLowerCase
().
indexOf
(
"from"
);
return
qlString
.
substring
(
beginPos
);
}
/**
* 去除hql的orderBy子句。
*
* @param qlString
* @return
*/
private
static
String
removeOrders
(
String
qlString
)
{
Pattern
p
=
Pattern
.
compile
(
"order\\s*by[\\w|\\W|\\s|\\S]*"
,
Pattern
.
CASE_INSENSITIVE
);
Matcher
m
=
p
.
matcher
(
qlString
);
StringBuffer
sb
=
new
StringBuffer
();
while
(
m
.
find
())
{
m
.
appendReplacement
(
sb
,
""
);
}
m
.
appendTail
(
sb
);
return
sb
.
toString
();
}
}
JeeSpringCloud/src/main/java/com/jeespring/common/persistence/proxy/PageConfiguration.java
0 → 100644
View file @
d5ba54ba
/**
* Copyright © 2012-2016 <a href="https://github.com/HuangBingGui/jeespring">jeespring</a> All rights reserved.
*/
package
com.jeespring.common.persistence.proxy
;
import
org.apache.ibatis.binding.MapperRegistry
;
import
org.apache.ibatis.session.Configuration
;
import
org.apache.ibatis.session.SqlSession
;
/**
* <p>
* 自定义Mybatis的配置,扩展.
* </p>
*
* @author poplar.yfyang
* @version 1.0 2012-05-13 上午10:06
* @since JDK 1.5
*/
public
class
PageConfiguration
extends
Configuration
{
protected
MapperRegistry
mapperRegistry
=
new
PaginationMapperRegistry
(
this
);
@Override
public
<
T
>
void
addMapper
(
Class
<
T
>
type
)
{
mapperRegistry
.
addMapper
(
type
);
}
@Override
public
<
T
>
T
getMapper
(
Class
<
T
>
type
,
SqlSession
sqlSession
)
{
return
mapperRegistry
.
getMapper
(
type
,
sqlSession
);
}
@Override
public
boolean
hasMapper
(
Class
<?>
type
)
{
return
mapperRegistry
.
hasMapper
(
type
);
}
}
Prev
1
2
3
4
5
6
7
8
9
…
20
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment