遇到问题记录
1.查看所有文章 前端layui自动发起请求
Request URL: http://localhost:8080/content/findAll.do?page=1&limit=10
后端 Controller 接收请求,调Service层,Service调Dao层
@Controller
@RequestMapping("content")
public class ContentController {
private static final Logger logger = org.apache.log4j.Logger.getLogger(ContentController.class);
@Autowired
private ContentService contentService;
/*查看所有文章*/
@RequestMapping("/findAll.do")
@ResponseBody
public LayuiData findAll(Integer page,Integer limit){
Limit lt = new Limit(page,limit);
List<Content> all = contentService.findAll(lt);
Integer many = contentService.findMany(page,limit);
logger.info("page============>"+page);
logger.info("limit============>"+limit);
logger.info("many============>"+many);
LayuiData ld = new LayuiData(0,"成功了",many,all);
return ld;
}
}
Dao层 有个小坑 开始没有封装Limit类,传了两个参数给findAll,结果一直报错
"Parameter 'XXX' not found. Available parameters are [arg1, arg0, param1,..."
后查资料参考 得知 传多个参数要加注解 @Param("xxx")
https://blog.csdn.net/xuanbabyliu/article/details/84374872
//public List<Content> findAll(@Param("page") Integer page, @Param("limit") Integer limit);
public List<Content> findAll(Limit lt);
于是封装了一个类,专门接收分页,虽然感觉没必要 不过就当试验练习了
public class Limit {
private Integer page;
private Integer limit;
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getLimit() {
return limit;
}
public void setLimit(Integer limit) {
this.limit = limit;
}
public Limit(Integer page, Integer limit) {
this.page = page;
this.limit = limit;
}
public Limit() {
}
}
2.删除+批量删除(说是删除 实际只是修改了数据库del字段) 前端ajax发起请求
//发送ajax请求
var url = "/content/delByid.do";
var params = {"id" : data.id};
$.ajax({
url:url,
data:JSON.stringify(params),
//data:'{"username":"liangge", "password":"123", "age":18}',或者这样写也可以,不用序列化,直接写的规范一点
type:"post",
success:function (resultData) {
//成功后的回调函数
console.log(resultData);
tableIns.reload();
layer.close(index);
},
dataType:"json",
contentType: 'application/json;charset=UTF-8'
});
因为是单个删除,后端要接受传过来的一个id,不知道有什么简单的方法接收只有一个键值对的json,最后还是用的封装的实体类(开始想用HttpServletRequest的getParameter方法,但是没有接收到,参考文章 https://blog.csdn.net/shuangmulin45/article/details/78083992)
https://blog.csdn.net/weixin_39220472/article/details/80725574
/* 逻辑删除*/
@RequestMapping(path = "/delByid.do")
@ResponseBody
public LayuiData delById(@RequestBody Content content){
Integer id = content.getId();
logger.info("id============>"+id);
Integer rs = contentService.delByid(id);
LayuiData ld = new LayuiData();
if (rs == 1){
ld.setCode(1);
ld.setMsg("成功了");
return ld;
}else {
ld.setCode(0);
ld.setMsg("失败了");
return ld;
}
}
批量删除 layui前端ajax发起请求
//批量删除
var newsId = [];
if(data.length > 0) {
for (var i in data) {
newsId.push(data[i].id);
}
$.ajax({
type:"post",
url:"/content/delByids.do",
contentType:"application/json",
data:JSON.stringify(newsId),
success:function (rs) {
console.log(rs);
tableIns.reload();
layer.close(index);
}
})
因为是个数组 所以后端接收有个小细节 后端接收 接收json数组
查资料参考的 https://blog.csdn.net/u012838207/article/details/80519375
/* 批量逻辑删除*/
@RequestMapping("/delByids.do")
@ResponseBody
public LayuiData delByids(@RequestBody String[] array){
List al = new ArrayList();
LayuiData ld = new LayuiData();
for (String a:array
) {
logger.info("a============>"+a);
Integer i = contentService.delByid(Integer.parseInt(a));
al.add(i);
}
if (al.size() >=1 ){
ld.setCode(1);
ld.setMsg("成功了");
}else{
ld.setCode(0);
ld.setMsg("失败了");
}
return ld;
}