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

1.配置mybatis后台分页插件

springBoot整合myBatis后台分页+bootstrapTable展示_第1张图片

springBoot整合myBatis后台分页+bootstrapTable展示_第2张图片

后台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
        
        FROM
        sys_log
        
            
                and (user_name like '%${params.name}%' or description like '%${params.name}%' or method like
                '%${params.name}%' or ip like '%${params.name}%')
            
        
        ORDER BY create_time desc
    


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 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 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 params = new HashMap();
        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 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');
}











你可能感兴趣的:(springBoot整合myBatis后台分页+bootstrapTable展示)