借用了别人的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,
});
}
这是前端的参数,这样来看应该是传到前端了吧?但是为什么不能按照这个参数搜索呢?