layui+mybatis分页,模糊查询

时间:2021-10-11
本文章向大家介绍layui+mybatis分页,模糊查询,主要包括layui+mybatis分页,模糊查询使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1. 前端

1.1 table容器:

<table class="layui-hide" id="test"></table>

1.2 数据请求和表格渲染:

layui.use('table', function () {
    var table = layui.table;

    table.render({
        elem: '#test',
        url: '/readbarcode/getlog/',//get请求地址
        cellMinWidth: 80,//自动分配宽度
        cols: [[{
            field: 'ipaddress',
            title: 'IP地址',
            align: "center"
        }, {
            field: 'barcode',
            title: '条码号',
            align: "center"
        }, {
            field: 'materialno',
            title: '物料号',
            align: "center"
        }, {
            field: 'userid',
            title: '用户',
            align: "center"
        }, {
            field: 'scandate',
            title: '扫描日期',
            align: "center",
            sort: true
        }, {
            field: 'operationsource',
            title: '来源',
            align: "center"
        }]],
        page: true //开启分页
    });

    // 多条件查询
    var $ = layui.$, active = {
        reload: function () {
            var ipaddressReload = $('#ipaddressReload');
            var barcodeReload = $('#barcodeReload');
            var materialnoReload = $('#materialnoReload');
            var userReload = $('#userReload');
            var scandateReload = $('#scandateReload');
            var sourceReload = $('#sourceReload');

            //执行重载
            table.reload('test', {
                page: {
                    curr: 1
                    //重新从第 1 页开始
                },
                where: {
                    ipaddress: ipaddressReload.val(),
                    barcode: barcodeReload.val(),
                    materialno: materialnoReload.val(),
                    userid: userReload.val(),
                    scandate: scandateReload.val(),
                    operationsource: sourceReload.val(),
                }
            });
        }
    };

    $('.demoTable .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
});

2. 后端接口

// 获取日志列表
@RequestMapping(value = "/getlog", method = RequestMethod.GET)
@ResponseBody
public JSONObject getLog(@RequestParam("page") int page, @RequestParam("limit") int limit, @RequestParam(value = "ipaddress", required = false) String ipaddress,
        @RequestParam(value = "barcode", required = false) String barcode, @RequestParam(value = "materialno", required = false) String materialno,
        @RequestParam(value = "userid", required = false) String userid, @RequestParam(value = "scandate", required = false) String scandate,
        @RequestParam(value = "operationsource", required = false) String operationsource)
{
    JSONObject jsonObject = new JSONObject();
    JSONArray jsonArray = new JSONArray();

    String strDateFormat = "yyyy-MM-dd HH:mm:ss";
    SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);

    try
    {
        List<Rbc_Querylog> logList = firstReadBarcodeService.getLogList(limit, limit * (page - 1), ipaddress, barcode, materialno, userid, scandate, operationsource);
        for (Rbc_Querylog rbc_Querylog : logList)
        {
            JSONObject obj = new JSONObject();
            obj.put("ipaddress", rbc_Querylog.getIpaddress());
            obj.put("barcode", rbc_Querylog.getBarcode());
            obj.put("materialno", rbc_Querylog.getMaterialno());
            obj.put("userid", rbc_Querylog.getUserid());
            obj.put("scandate", sdf.format(rbc_Querylog.getScandate()));
            obj.put("operationsource", rbc_Querylog.getOperationsource());
            jsonArray.add(obj);
        }
        jsonObject.put("data", jsonArray);

        jsonObject.put("code", 0);
        jsonObject.put("msg", "");
        jsonObject.put("count", firstReadBarcodeService.getLogCount(ipaddress, barcode, materialno, userid, scandate, operationsource));

    } catch (Exception e)
    {
        jsonObject.put("msg", "数据查询出错");
        e.printStackTrace();
    }
    return jsonObject;
}

3. Mapper相关

3.1 方法映射

// 查询读图日志
List<Rbc_Querylog> selectLogList(@Param(value = "pagesize") int pagesize, @Param(value = "currentcount") int currentcount, String ipaddress, String barcode, String materialno, String userid,String scandate, String operationsource);

// 查询读图日志数量
int selectLogCount(String ipaddress, String barcode, String materialno, String userid, String scandate, String operationsource);

3.2 sql语句

<!-- 查询读图日志 -->
	<select id="selectLogList"
		resultType="com.myande.readbarcode.bean.Rbc_Querylog">
		SELECT top ${pagesize} * FROM [ITM].[dbo].[rbc_querylog]
		<where>
			<if test="ipaddress!=null and ipaddress!=''">
				ipaddress = #{ipaddress}
			</if>
			<if test="barcode!=null and barcode!=''">
				and barcode = #{barcode}
			</if>
			<if test="materialno!=null and materialno!=''">
				and materialno = #{materialno}
			</if>
			<if test="userid!=null and userid!=''">
				and userid = #{userid}
			</if>
			<if test="scandate!=null and scandate!=''">
				and DateDiff(dd,scandate,#{scandate})=0
			</if>
			<if test="operationsource!=null and operationsource!=''">
				and operationsource = #{operationsource}
			</if>
			<if test="1==1">
				and id not in (SELECT top ${currentcount} ID FROM
				[ITM].[dbo].[rbc_querylog]
				<where>
					<if test="ipaddress!=null and ipaddress!=''">
						ipaddress = #{ipaddress}
					</if>
					<if test="barcode!=null and barcode!=''">
						and barcode = #{barcode}
					</if>
					<if test="materialno!=null and materialno!=''">
						and materialno = #{materialno}
					</if>
					<if test="userid!=null and userid!=''">
						and userid = #{userid}
					</if>
					<if test="scandate!=null and scandate!=''">
						and DateDiff(dd,scandate,#{scandate})=0
					</if>
					<if test="operationsource!=null and operationsource!=''">
						and operationsource = #{operationsource}
					</if>
				</where>
				order by scandate desc)
			</if>
		</where>
		order by scandate desc
	</select>

	<!-- 查询日志数量 -->
	<select id="selectLogCount" resultType="int">
		SELECT count(*) FROM
		[ITM].[dbo].[rbc_querylog]
		<where>
			<if test="ipaddress!=null and ipaddress!=''">
				ipaddress = #{ipaddress}
			</if>
			<if test="barcode!=null and barcode!=''">
				and barcode = #{barcode}
			</if>
			<if test="materialno!=null and materialno!=''">
				and materialno = #{materialno}
			</if>
			<if test="userid!=null and userid!=''">
				and userid = #{userid}
			</if>
			<if test="scandate!=null and scandate!=''">
				and DateDiff(dd,scandate,#{scandate})=0
			</if>
			<if test="operationsource!=null and operationsource!=''">
				and operationsource = #{operationsource}
			</if>
		</where>
	</select>

原文地址:https://www.cnblogs.com/yddwinter/p/15391825.html