liu_yuanji 2022-09-13 15:17 采纳率: 20%
浏览 24
已结题

Spring+layui查询时无论输入什么都显示整个table

借用了别人的Spring+layui搭建的项目,实现简单的增删改查,目前出现的问题是查询的时候不管输入了什么内容,显示出来的都是所有的信息。去掉xml的话,会直接报异常,说明也是加载了xml的;似乎问题就是xml里的where模块加载失败。但是xml应该写的没什么问题啊?求了解的人帮忙看看,感谢!!
dao层

package com.mudemo.dao;

import com.alibaba.fastjson.JSONObject;
import com.mudemo.model.User;
import org.apache.ibatis.annotations.*;
import java.util.List;

public interface GoodsDao {
    @Select("SELECT * FROM user")
    List<User> getGoodsList();

    @Insert("INSERT INTO user(title,recorder,department,date,detail,file_name) VALUES(#{title}, #{recorder},#{department},#{date},#{detail},#{file_name})")
    int addGood(JSONObject request);

    @Update("UPDATE user SET title=#{title},recorder=#{recorder},department=#{department},date=#{date},detail=#{detail},file_name=#{file_name} WHERE id=#{id}")
    void updateGood(JSONObject request);

    @Delete("DELETE FROM user WHERE id=#{id}")
    void deleteGood(@Param("id") int id);

    //@Select("SELECT * FROM user WHERE detail like '%'#{detail}'%'")
    List<User> searchGoods(JSONObject request);
}


serviceImpl层

package com.mudemo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mudemo.dao.GoodsDao;
import com.mudemo.model.User;
import com.mudemo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "GoodsService")
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsDao goodsDao;

    @Override
    public JSONObject getGoodsList(int pageNum, int pageSize) {
        JSONObject result = new JSONObject();
        try {
            PageHelper.startPage(pageNum, pageSize);
            PageInfo<User> pageInfo = new PageInfo(goodsDao.getGoodsList());

            result.put("code", "0");
            result.put("msg", "操作成功!");
            result.put("data", pageInfo.getList());
            result.put("count", pageInfo.getTotal());
        } catch (Exception e) {
            result.put("code", "500");
            result.put("msg", "查询异常!");
        }
        return result;
    }

    @Override
    public JSONObject addGood(JSONObject request) {
        JSONObject result = new JSONObject();
        
        try {
            goodsDao.addGood(request);
            result.put("code", "0");
            result.put("msg", "操作成功!");
        } catch (Exception e) {
            result.put("code", "500");
            result.put("msg", "新增异常!");
        }
        return result;
    }

    @Override
    public JSONObject updateGood(JSONObject request) {
        JSONObject result = new JSONObject();
        try {
            goodsDao.updateGood(request);
            result.put("code", "0");
            result.put("msg", "操作成功!");
        } catch (Exception e) {
            result.put("code", "500");
            result.put("msg", "修改异常!");
            System.out.println(e.getMessage());
        }
        return result;
    }

    @Override
    public JSONObject deleteGood(int id) {
        JSONObject result = new JSONObject();
        try {
            goodsDao.deleteGood(id);
            result.put("code", "0");
            result.put("msg", "操作成功!");
        } catch (Exception e) {
            result.put("code", "500");
            result.put("msg", "删除异常!");
        }
        return result;
    }

    @Override
    public JSONObject searchGoods(JSONObject request) {
        JSONObject result = new JSONObject();
        try {
            int pageNum = request.getInteger("pageNum");
            int pageSize = request.getInteger("pageSize");
            String detail = request.getString("detail");
            PageHelper.startPage(pageNum, pageSize);
            //System.out.println(detail);
            PageInfo<User> pageInfo = new PageInfo(goodsDao.searchGoods(request));

            result.put("code", "0");
            result.put("msg", "操作成功!");
            result.put("data", pageInfo.getList());
            result.put("count", pageInfo.getTotal());
        } catch (Exception e) {
            result.put("code", "500");
            result.put("msg", "查询异常!");
        }
        return result;
    }
}

controller层

package com.mudemo.controller;

import com.alibaba.fastjson.JSONObject;
import com.mudemo.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @RequestMapping(value = "/goodsList", method = RequestMethod.GET)
    public JSONObject getGoodsList(@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        return goodsService.getGoodsList(pageNum, pageSize);
    }

    @RequestMapping(value = "/updateGood", method = RequestMethod.POST)
    public JSONObject updateGood(@RequestBody JSONObject request) {
        return goodsService.updateGood(request);
    }

    @RequestMapping(value = "/addGood", method = RequestMethod.POST)
    public JSONObject addGood(@RequestBody JSONObject request) {
        return goodsService.addGood(request);
    }

    @RequestMapping(value = "/deleteGood", method = RequestMethod.GET)
    public JSONObject deleteGood(@RequestParam("id") int id) {
        return goodsService.deleteGood(id);
    }

    @RequestMapping(value = "searchGoods",method = RequestMethod.POST)
    public JSONObject searchGood(@RequestBody JSONObject request){
        System.out.println(request);
        return goodsService.searchGoods(request);
    }
    @RequestMapping("/get")
    public Object get(){
        return "index";
    }
}

xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mudemo.dao.GoodsDao">

    <select id="searchGoods" resultType="com.mudemo.model.User">
        SELECT
        *
        FROM
        user
        <where>
            <if test="detail != null">and name like concat('%',
                #{detail},
                '%')
            </if>
            <if test="recorder != null">and recorder = #{recorder}</if>
        </where>

    </select>
</mapper>


index.html(只粘贴相关部分了)

table.render({
            elem: '#goods_table'
            , id: 'goodsReload'
            , even: true //开启隔行背景
            , url: '/goods/goodsList'
            , request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            }
            // , toolbar: '#goods_headerBar'
            , title:
                '问题详情'
            , page:
                true //开启分页
            , limit: 10
            , limits: [1, 5, 10, 20, 50, 100]
            , cols: [
                        [{
                            type: 'checkbox',
                            fixed: 'left'
                        }, {
                            field: 'id',
                            fixed: 'left',
                            width: 80,
                            title: 'ID',
                            sort: true,
                            totalRowText: '合计:'
                        }, {
                            field: 'title',
                            width: 200,
                            title: '标题'
                        }, {
                            field: 'recorder',
                            title: '记录人',
                            hide: 0,
                            width: 100,
                            edit: 'text'
                        }, {
                            field: 'department',
                            width: 150,
                            title: '部门',
                            sort: true
                        },  {
                            field: 'date',
                            title: '日期',
                            width: 150,
                        }, {
                            field: 'detail',
                            width: 300,
                            title: '详情',
                        }, {
                            field: 'fileName',
                            title: '图片展示',
                            width: 200,
                            templet:function(res){
                                return '<img src="ftp://172.30.139.207/"'+res.fileName+'"../static/images"/>'
                            }
                        },{
                        fixed: 'right',
                        title: '操作',
                        toolbar: '#goods_lineBar',
                        width: 160,
                        align: 'center',
                        hide:true,
                    },]
                    ],
        });

// 监听搜索按钮提交事件
        form.on('submit(search)', function (data) {
            var formData = data.field;
            var count = checkForm("search_form");
            if (count !== 0) {
                //数据表格重载
                tableReload('goodsReload', formData, "application/json; charset=utf-8", '/goods/searchGoods', 'post');
            } else {
                parent.layer.msg('请先选择查询条件!', {icon: 2, time: 1500});
            }
            return false;
        });

js(只粘贴查询部分了)


function tableReload(tableId, where, contentType, url, method) {
    layui.table.reload(tableId, {
        where: where,
        contentType: contentType,
        page: {
            curr: 1 //重新从第 1 页开始
        },
        url: url
        , method: method,
        
    });
}

这是前端的参数,这样来看应该是传到前端了吧?但是为什么不能按照这个参数搜索呢?

img

  • 写回答

1条回答 默认 最新

  • 流光Wyatt 2022-09-13 16:32
    关注

    你前端调用的应该是searchGoods,而非goodsList

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月21日
  • 修改了问题 9月13日
  • 创建了问题 9月13日

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器