springBoot整合myBatis后台分页+bootstrapTable展示

1.配置mybatis后台分页插件



后台sql当包含List语句自动进行后台分页拦截操作

2.PagePlugin编写

package com.javon.boot.util;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

import javax.xml.bind.PropertyException;

import com.javon.boot.vo.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.executor.ExecutorException;
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.mapping.ParameterMapping;
import org.apache.ibatis.mapping.ParameterMode;
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;
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;




/**
 * 
* @ClassName: PagePlugin 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author 
* @date 2015年8月6日 下午8:21:28 
*
 */
@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) })
public class PagePlugin implements Interceptor {

	private static String dialect = "";	//数据库方言
	private static String pageSqlId = ""; //mapper.xml中需要拦截的ID(正则匹配)
	
	public Object intercept(Invocation ivk) throws Throwable {
		// TODO Auto-generated method stub
		if(ivk.getTarget() instanceof RoutingStatementHandler){
			RoutingStatementHandler statementHandler = (RoutingStatementHandler)ivk.getTarget();
			BaseStatementHandler delegate = (BaseStatementHandler) ReflectHelper.getValueByFieldName(statementHandler, "delegate");
			MappedStatement mappedStatement = (MappedStatement) ReflectHelper.getValueByFieldName(delegate, "mappedStatement");
			
			if(mappedStatement.getId().matches(pageSqlId)){ //拦截需要分页的SQL
				BoundSql boundSql = delegate.getBoundSql();
				Object parameterObject = boundSql.getParameterObject();//分页SQL<select>中parameterType属性对应的实体参数,即Mapper接口中执行分页方法的参数,该参数不得为空
				if(parameterObject==null){
					throw new NullPointerException("parameterObject尚未实例化!");
				}else{
					Connection connection = (Connection) ivk.getArgs()[0];
					String sql = boundSql.getSql();
					//String countSql = "select count(0) from (" + sql+ ") as tmp_count"; //记录统计
					String countSql = "select count(0) from (" + sql+ ")  tmp_count"; //记录统计 == oracle 加 as 报错(SQL command not properly ended)
					PreparedStatement countStmt = connection.prepareStatement(countSql);
					BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(),countSql,boundSql.getParameterMappings(),parameterObject);
					setParameters(countStmt,mappedStatement,countBS,parameterObject);
					ResultSet rs = countStmt.executeQuery();
					int count = 0;
					if (rs.next()) {
						count = rs.getInt(1);
					}
					rs.close();
					countStmt.close();
					//System.out.println(count);
					Page page = null;
					if(parameterObject instanceof Page){	//参数就是Page实体
						 page = (Page) parameterObject;
						 //见com.jalan.entity.Page.entityOrField 注释
						page.setTotalResult(count);
					}else{	//参数为某个实体,该实体拥有Page属性
						Field pageField = ReflectHelper.getFieldByFieldName(parameterObject,"page");
						if(pageField!=null){
							page = (Page) ReflectHelper.getValueByFieldName(parameterObject,"page");
							if(page==null)
								page = new Page();
							 //见com.jalan.entity.Page.entityOrField 注释
							page.setTotalResult(count);
							ReflectHelper.setValueByFieldName(parameterObject,"page", page); //通过反射,对实体对象设置分页对象
						}else{
							throw new NoSuchFieldException(parameterObject.getClass().getName()+"不存在 page 属性!");
						}
					}
					String pageSql = generatePageSql(sql,page);
					ReflectHelper.setValueByFieldName(boundSql, "sql", pageSql); //将分页sql语句反射回BoundSql.
				}
			}
		}
		return ivk.proceed();
	}

	
	/**
	 * 对SQL参数(?)设值,参考org.apache.ibatis.executor.parameter.DefaultParameterHandler
	 * @param ps
	 * @param mappedStatement
	 * @param boundSql
	 * @param parameterObject
	 * @throws SQLException
	 */
	private 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);
					}
					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());
				}
			}
		}
	}
	
	/**
	 * 根据数据库方言,生成特定的分页sql
	 * @param sql
	 * @param page
	 * @return
	 */
	private String generatePageSql(String sql,Page page){
		if(page!=null && StringUtils.isNotEmpty(dialect)){
			StringBuffer pageSql = new StringBuffer();
			if("mysql".equals(dialect)){
				pageSql.append(sql);
				pageSql.append(" limit "+page.getCurrentResult()+","+page.getShowCount());
			}else if("oracle".equals(dialect)){
				pageSql.append("select * from (select tmp_tb.*,ROWNUM row_id from (");
				pageSql.append(sql);
				//pageSql.append(") as tmp_tb where ROWNUM<=");
				pageSql.append(") tmp_tb where ROWNUM<=");
				pageSql.append(page.getCurrentResult()+page.getShowCount());
				pageSql.append(") where row_id>");
				pageSql.append(page.getCurrentResult());
			}
			return pageSql.toString();
		}else{
			return sql;
		}
	}
	
	public Object plugin(Object arg0) {
		// TODO Auto-generated method stub
		return Plugin.wrap(arg0, this);
	}

	public void setProperties(Properties p) {
		dialect = p.getProperty("dialect");
		if (StringUtils.isEmpty(dialect)) {
			try {
				throw new PropertyException("dialect property is not found!");
			} catch (PropertyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pageSqlId = p.getProperty("pageSqlId");
		if (StringUtils.isEmpty(pageSqlId)) {
			try {
				throw new PropertyException("pageSqlId property is not found!");
			} catch (PropertyException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}

3.page实体编写,可以自定义改造

package com.javon.boot.vo;

import java.util.HashMap;
import java.util.Map;

public class Page {

    private int showCount; //每页显示记录数
    private int totalPage;        //总页数
    private int totalResult;    //总记录数
    private int currentPage;    //当前页
    private int currentResult;    //当前记录起始索引
    private Map<String, Object> params = new HashMap<String, Object>();// 传入查询参数
    public Map<String, Object> getParams() {
        return params;
    }
    public void setParams(Map<String, Object> params) {
        this.params = params;
    }
    public Page() {
        try {
            this.showCount = 10;
        } catch (Exception e) {
            this.showCount = 20;
        }
    }

    public int getTotalPage() {
        if (totalResult % showCount == 0)
            totalPage = totalResult / showCount;
        else
            totalPage = totalResult / showCount + 1;
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getTotalResult() {
        return totalResult;
    }

    public void setTotalResult(int totalResult) {
        this.totalResult = totalResult;
    }

    public int getCurrentPage() {
        if (currentPage <= 0)
            currentPage = 1;
        if (currentPage > getTotalPage())
            currentPage = getTotalPage();
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getShowCount() {
        return showCount;
    }

    public void setShowCount(int showCount) {

        this.showCount = showCount;
    }

    public int getCurrentResult() {
        currentResult = (getCurrentPage() - 1) * getShowCount();
        if (currentResult < 0)
            currentResult = 0;
        return currentResult;
    }

    public void setCurrentResult(int currentResult) {
        this.currentResult = currentResult;
    }


}

4.mybatis.xml文件使用分页page对象

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">

<mapper namespace="com.javon.boot.dao.SysLogDao">
    <resultMap id="rm" type="SysLog">
        <result property="id" jdbcType="INTEGER" column="id"/>
        <result property="userName" jdbcType="VARCHAR" column="user_name"/>
        <result property="userId" jdbcType="INTEGER" column="user_id"/>
        <result property="description" jdbcType="VARCHAR" column="description"/>
        <result property="method" jdbcType="VARCHAR" column="method"/>
        <result property="params" jdbcType="VARCHAR" column="params"/>
        <result property="ip" jdbcType="VARCHAR" column="ip"/>
        <result property="createTime" jdbcType="TIMESTAMP" column="create_time"/>
    </resultMap>

    <sql id="Base_Column_List">
		id,
		user_name,
		user_id,
		description,
		method,
		params,
		ip,
		create_time
	</sql>
    <!-- 插入 -->
    <insert id="insert">
		INSERT INTO sys_log (
		user_name,
		user_id,
		description,
		method,
		params,
		ip,
		create_time
		) VALUES (
		#{userName},
		#{userId},
		#{description},
		#{method},
		#{params},
		#{ip},
		now()
		)

	</insert>
    <!-- 集合查询 -->
    <select id="queryList" resultMap="rm" parameterType="Page">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        sys_log
        <where>
            <if test="params['name'] !=null and params['name'] !=''">
                and (user_name like '%${params.name}%' or description like '%${params.name}%' or method like
                '%${params.name}%' or ip like '%${params.name}%')
            </if>
        </where>
        ORDER BY create_time desc
    </select>
</mapper>

4.dao层编写
package com.javon.boot.dao;

import com.javon.boot.vo.Page;
import com.javon.boot.vo.SysLog;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * Created by admin on 2018/3/30.
 */
@Mapper
public interface SysLogDao {
    void insert(SysLog sysLog);

    List<SysLog> queryList(Page page);
}

5.service层

package com.javon.boot.service.impl;

import com.javon.boot.dao.SysLogDao;
import com.javon.boot.service.SysLogService;
import com.javon.boot.vo.Page;
import com.javon.boot.vo.SysLog;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by admin on 2018/3/30.
 */
@Service
public class SysLogServiceImpl implements SysLogService {
    @Resource
    private SysLogDao sysLogDao;
    @Override
    public void insert(SysLog sysLog) {
        sysLogDao.insert(sysLog);
    }

    @Override
    public List<SysLog> queryList(Page page) {
        return sysLogDao.queryList(page);
    }
}

5.controller层

package com.javon.boot.controller.sys;

import com.javon.boot.service.SysLogService;
import com.javon.boot.util.PageUtils;
import com.javon.boot.vo.Page;
import com.javon.boot.vo.SysLog;
import com.javon.boot.vo.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2018/3/30.
 */
@RequestMapping("/sys/log")
@Controller
public class SysLogController {
    private static final String SYSUSERPREFIX = "system/log";
    @Resource
    private SysLogService sysLogService;


    @RequiresPermissions("sys:log:log")
    @RequestMapping(value = "", method = RequestMethod.GET)
    String log(Model model) {
        return SYSUSERPREFIX + "/log";
    }


    @RequiresPermissions("sys:log:log")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public PageUtils list(String name, String date, String pageNumber, String pageSize) {
        if (!StringUtils.isNotBlank(pageNumber) & !StringUtils.isNotBlank(pageSize)) {
            pageNumber = "1";
            pageSize = "15";
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("name", name);
       // params.put("deptId", date);
        Page page = new Page();
        page.setParams(params);
        page.setCurrentPage(Integer.valueOf(pageNumber));
        page.setShowCount(Integer.valueOf(pageSize));
        List<SysLog> logList = sysLogService.queryList(page);
        return new PageUtils(logList, page.getTotalResult());//组装成前端所需格式数据
    }

}

6.

PageUtils+js+html
package com.javon.boot.util;

import java.io.Serializable;
import java.util.List;

/**
 * 构造出前端需要分页显示信息
 */
public class PageUtils implements Serializable {
	private static final long serialVersionUID = 1L;
	private int total;
	private List<?> rows;

	public PageUtils(List<?> list, int total) {
		this.rows = list;
		this.total = total;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List<?> getRows() {
		return rows;
	}

	public void setRows(List<?> rows) {
		this.rows = rows;
	}

}

var prefix = "/sys/log"
$(function() {
    load();
});

function load() {
	$('#exampleTable')
		.bootstrapTable(
			{
				method : 'get', // 服务器数据的请求方式 get or post
				url : prefix + "/list", // 服务器数据的加载地址
				iconSize : 'outline',
				toolbar : '#exampleToolbar',
				striped : true, // 设置为true会有隔行变色效果
				dataType : "json", // 服务器返回的数据类型
				singleSelect : false, // 设置为true将禁止多选
                queryParamsType:'',
				pageSize : 10, // 如果设置了分页,每页数据条数
				pageNumber : 1, // 如果设置了分布,首页页码
				sidePagination : "server", // 设置在哪里进行分页,可选值为"client" 或者
                pagination: true,//是否开启分页(*)启动分页,必须设为true
                pageList: [10,20],//可供选择的每页的行数(*)
				queryParams : function(params) {
					return {
						// 说明:传入后台的参数包括offset开始索引,limit步长,sort排序列,order:desc或者,以及所有列的键值
                        pageSize : params.pageSize,
                        pageNumber : params.pageNumber,
						name : $('#searchName').val(),
                        date : $('#createTime').val()
					};
				},
				columns : [
					{
						field : 'userName',
						title : '操作人'
					},
					{
						field : 'description',
						title : '描述'
					},
                    {
                        field : 'method', // 列字段名
                        title : '请求方法' // 列标题
                    },
                    {
                        field : 'params', // 列字段名
                        title : '请求参数' // 列标题
                    },
					{
						field : 'ip',
						title : '操作IP'
					},
                    {
                        field : 'createTime',
                        title : '操作时间',
                        //获取日期列的值进行转换
                        formatter: function (value, row, index) {
                            return changeDateFormat(value)
                        }
                    }
				 ]
			});
}
function reLoad() {
	$('#exampleTable').bootstrapTable('refresh');
}
<!DOCTYPE html>
<html lang="zh_CN" xmlns:th="http://www.thymeleaf.org">
<meta charset="utf-8"/>
<head th:include="include::header"></head>
<body class="gray-bg">
<div class="wrapper wrapper-content ">
	<div class="col-sm-12">
		<div class="ibox">
			<div class="ibox-body">
				<div class="fixed-table-toolbar">

					<div class="columns pull-right">
						<button class="btn btn-success" onclick="reLoad()">查询</button>
					</div>

					<div class="columns pull-right col-md-2 nopadding">
						<input id="searchName" type="text" class="form-control" placeholder=" 内容 "
						/>
					</div>
					<div class="layui-inline columns pull-right col-md-4 nopadding">
						<div class="layui-input-inline">
							<input type="text" class="layui-input form-control" id="createTime" placeholder=" 时间范围 " lay-key="11"/>
						</div>
					</div>
				</div>
				<table id="exampleTable" data-mobile-responsive="true">
				</table>
			</div>
		</div>
	</div>

</div>
<div th:include="include::footer"></div>
<script type="text/javascript" src="/js/appjs/sys/log/log.js"></script>
<script>
    laydate.render({
        elem: '#createTime'
        ,type: 'datetime'
        ,range: true
    });
</script>
</body>

</html>






猜你喜欢

转载自blog.csdn.net/wjw1639942939/article/details/80346685