package com.immouo.moyou.core.interceptor;
import java.sql.Connection;
import java.util.Properties;
import org.apache.ibatis.executor.statement.RoutingStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
/**
* @ClassName: MySqlDataInterceptor
* @Description: 分页拦截器
* @author zkd
*
*/
@Intercepts({
@Signature(method =
"prepare", type = StatementHandler.class, args = { Connection.class }) })
public class MySqlDataInterceptor implements Interceptor {
/**
* 拦截后要执行的方法
*/
public Object
intercept(Invocation invocation)
throws Throwable {
RoutingStatementHandler handler = (RoutingStatementHandler) invocation.getTarget();
StatementHandler delegate = (StatementHandler) ReflectUtil.getFieldValue(handler,
"delegate");
BoundSql boundSql = delegate.getBoundSql();
Object obj = boundSql.getParameterObject();
if (obj
instanceof PageBean) {
PageBean page = (PageBean) obj;
String sql = boundSql.getSql();
String pageSql =
this.getPageSql(page, sql);
ReflectUtil.setFieldValue(boundSql,
"sql", pageSql);
}
return invocation.proceed();
}
/**
* 拦截器对应的封装原始对象的方法
*/
public Object
plugin(Object target) {
return Plugin.wrap(target,
this);
}
/**
* 设置注册拦截器时设定的属性
*/
public void setProperties(Properties properties) {
}
/**
* 根据page对象获取对应的分页查询Sql语句,这里只做了两种数据库类型,Mysql和Oracle 其它的数据库都 没有进行分页
*
* @param page
* 分页对象
* @param sql
* 原sql语句
* @return
*/
private String
getPageSql(PageBean page, String sql) {
StringBuffer sqlBuffer =
new StringBuffer(sql);
return getMysqlPageSql(page, sqlBuffer);
}
/**
* 获取Mysql数据库的分页查询语句
*
* @param page
* 分页对象
* @param sqlBuffer
* 包含原sql语句的StringBuffer对象
* @return Mysql数据库分页语句
*/
private String
getMysqlPageSql(PageBean page, StringBuffer sqlBuffer) {
int offset = (page.getPageNumber() -
1) * page.getPageSize();
sqlBuffer.append(
" LIMIT ").append(offset).append(
",").append(page.getPageSize());
return sqlBuffer.toString();
}
package com.immouo.moyou.core.interceptor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.type.Alias;
import org.springframework.util.ObjectUtils;
/**
* @ClassName: PageBean
* @author Administrator
*
*/
@Alias(value =
"PageBean")
public class PageBean {
/** 当前页 */
private int pageNumber =
20;
private int pageSize =
1;
/** 当前页显示记录条数 */
@SuppressWarnings(
"rawtypes")
private List rows;
private int total;
private Map<String, Object> conditions =
new HashMap<>();
public int getPageNumber() {
return pageNumber;
}
public void setPageNumber(
int pageNumber) {
this.pageNumber = pageNumber;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(
int pageSize) {
this.pageSize = pageSize;
}
@SuppressWarnings(
"rawtypes")
public List
getRows() {
return rows;
}
@SuppressWarnings(
"rawtypes")
public void setRows(List rows) {
this.rows = rows;
}
public int getTotal() {
return total;
}
public void setTotal(
int total) {
this.total = total;
}
public Map<String, Object>
getConditions() {
return conditions;
}
public void setConditions(Map<String, Object> conditions) {
this.conditions = conditions;
}
public void addConditions(String key, Object value) {
if (ObjectUtils.isEmpty(conditions)) {
this.conditions =
new HashMap<String, Object>();
}
if (!ObjectUtils.isEmpty(value)) {
this.conditions.put(key, value);
}
}
@Override
public String
toString() {
return "PageBean [pageNumber:" + pageNumber +
", pageSize:" + pageSize +
", rows:" + rows +
", total:" + total
+
", conditions:" + conditions +
"]";
}
}
service调用:
@Override
public List<Newest
> queryCreateCircle(CircleData circleData) {
PageBean pageBean
= new PageBean();
pageBean
.setPageNumber(circleData
.getPageNumber());
pageBean
.setPageSize(circleData
.getPageSize());
Map<String, Object
> map = new HashMap
<>();
map.put(
"userId", circleData
.getUserId());
pageBean
.setConditions(
map);
List<Newest
> result
= newestMapper
.queryCreateCircle(pageBean);
XML:
<select id=
"queryCreateCircle" resultMap=
"BaseResultMap" parameterType=
"PageBean">
SELECT
c
.id,
c
.avatar,
c
.circle_name,
c
.circle_introduction,
c
.member_total,
c
.topic_total
FROM
t_circle c
WHERE
c
.creator_id=
#{conditions.userId} ***<----注意:分页一定要加conditions***
</select>
转载请注明原文地址: https://ju.6miu.com/read-678063.html