layui+SSM的数据表的增删改实例(利用弹框添加、修改)
(编辑:jimmy 日期: 2024/11/14 浏览:3 次 )
本人前端知识相当于小白,初学SSM时,了解了layui前端框架,所以开始研究了数据表的增删改查,由于js、ajax知识不是很好,所以百度了相关ajax操作,用以借鉴。希望能帮助有需要的初学者,不喜勿喷,另外有相关不足,希望大家可以指出,谢谢!
注: 以下前端代码都是利用layui的框架,后台是SSM
前端:
<%-- Created by IntelliJ IDEA. User: SL Date: 2019/2/26 Time: 14:03 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <title>layui在线调试</title> <link rel="stylesheet" href="${ctx}/static/layui/css/layui.css" rel="external nofollow" media="all"> <script src="/UploadFiles/2021-04-02/jquery-3.3.1.js">后台:
package com.sl.controller; import com.sl.model.NewsDetail; import com.sl.service.NewsDetailService; import com.sl.util.DateUtil; import org.codehaus.jackson.map.ObjectMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpSession; import java.io.IOException; import java.time.format.DateTimeFormatter; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @Controller @RequestMapping("/news") public class NewsDetailController { /*注入NewsDetailService*/ /** * */ @Autowired private NewsDetailService newsDetailService; @RequestMapping("/main") @ResponseBody private Map<String, Object> queryAll() { Map<String, Object> map = new HashMap<>(); List<NewsDetail> list = newsDetailService.queryAll(); int count = newsDetailService.queryAllCount(); map.put("data", list); map.put("code", 0); map.put("count", count); return map; } @RequestMapping(value = "/delete", method = RequestMethod.POST, produces = "application/json") @ResponseBody private Map<String, Object> deleteById(@RequestBody HashMap<String, String> map2) { Map<String, Object> map = new HashMap<>(); int id = Integer.parseInt(map2.get("id")); int row = newsDetailService.deleteById(id); if (row == 1) { List<NewsDetail> list = newsDetailService.queryAll(); int count = newsDetailService.queryAllCount(); map.put("data", list); map.put("returnCode", "200"); map.put("count", count); map.put("code", 0); return map; } else { map.put("code", 1); return map; } } @RequestMapping(value = "/mulDelete", method = RequestMethod.POST, produces = "application/json") @ResponseBody public Map<String, Object> mulDelete(@RequestBody HashMap<String, String> map2){ Map<String, Object> map = new HashMap<>(); String ids = map2.get("ids"); String mulid[] = ids.split(","); int row = 0; for(int i=0; i<mulid.length; i++){ row = newsDetailService.mulDelete(mulid[i]); } if(row != 0){ List<NewsDetail> list = newsDetailService.queryAll(); int count = newsDetailService.queryAllCount(); map.put("code", 0); map.put("data", list); map.put("count", count); map.put("returnCode", "200"); return map; } else { map.put("code", -1); return map; } } @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json") @ResponseBody public Map<String, Object> updateById(@RequestBody HashMap<String, String> map2) throws IOException { Map<String, Object> map = new HashMap<>(); int id = Integer.parseInt(map2.get("id")); String title = map2.get("newtitle"); String summary = map2.get("newsummary"); String author = map2.get("newauthor"); /*System.out.println(id+" "+title +" "+summary+" "+author); System.out.println(1); System.out.println(new Date()); System.out.println(DateUtil.getCurrentDateString()); System.out.println(2);*/ NewsDetail newsDetail = new NewsDetail(); newsDetail.setId(id); newsDetail.setTitle(title); newsDetail.setSummary(summary); newsDetail.setAuthor(author); newsDetail.setCreateDate(DateUtil.formatDate(new Date(), "yyyy-MM-dd HH-mm-ss")); /*System.out.println(newsDetail.getCreateDate()); System.out.println(DateUtil.formatDate(new Date(), "yyyy-MM-dd HH-mm-ss"));*/ int row = newsDetailService.updateById(newsDetail); if (row == 1) { List<NewsDetail> list = newsDetailService.queryAll(); int count = newsDetailService.queryAllCount(); map.put("data", list); map.put("count", count); map.put("code", 0); map.put("returnCode", "200"); return map; } else { map.put("code", 1); return map; } } @RequestMapping("/update2") @ResponseBody public Map<String, Object> update2(NewsDetail newsDetail) { Map<String, Object> map = new HashMap<>(); /*newsDetail.setCreateDate(new Date());*/ int row = newsDetailService.updateById(newsDetail); if (row == 1) { List<NewsDetail> list = newsDetailService.queryAll(); int count = newsDetailService.queryAllCount(); map.put("data", list); map.put("count", count); map.put("code", 0); return map; } else { map.put("code", 1); return map; } } @RequestMapping("/add") @ResponseBody public Map<String, Object> add(@RequestBody HashMap<String, String> map2) { Map<String, Object> map = new HashMap<>(); String title = map2.get("newtitle"); String summary = map2.get("newsummary"); String author = map2.get("newauthor"); NewsDetail newsDetail = new NewsDetail(); newsDetail.setTitle(title); newsDetail.setSummary(summary); newsDetail.setAuthor(author); newsDetail.setCreateDate(DateUtil.formatDate(new Date(), "yyyy-MM-dd HH-mm-ss")); boolean row = newsDetailService.addNews(newsDetail); if (row) { List<NewsDetail> list = newsDetailService.queryAll(); int count = newsDetailService.queryAllCount(); map.put("data", list); map.put("count", count); map.put("returnCode", "200"); map.put("code", 0); return map; } else { map.put("code", 1); return map; } } }亲测有效,希望大家提出不足!谢谢!另外后台里面有一个时间转换的类(DateUtil.formatDate()),前端有一个例如url: '${ctx}/news/update/',其中的${cxt}是一个指定的路径,后台写了一个类,这些有需要的,可以看我的博客,谢谢!
以上这篇layui+SSM的数据表的增删改实例(利用弹框添加、修改)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
下一篇:vue实现在线翻译功能