本人前端知識相當(dāng)于小白,初學(xué)SSM時(shí),了解了layui前端框架,所以開始研究了數(shù)據(jù)表的增刪改查,由于js、ajax知識不是很好,所以百度了相關(guān)ajax操作,用以借鑒。希望能幫助有需要的初學(xué)者,不喜勿噴,另外有相關(guān)不足,希望大家可以指出,謝謝!
成都創(chuàng)新互聯(lián)公司是一家業(yè)務(wù)范圍包括IDC托管業(yè)務(wù),雅安服務(wù)器托管、主機(jī)租用、主機(jī)托管,四川、重慶、廣東電信服務(wù)器租用,大邑服務(wù)器托管,成都網(wǎng)通服務(wù)器托管,成都服務(wù)器租用,業(yè)務(wù)范圍遍及中國大陸、港澳臺以及歐美等多個(gè)國家及地區(qū)的互聯(lián)網(wǎng)數(shù)據(jù)服務(wù)公司。
注: 以下前端代碼都是利用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" %>layui在線調(diào)試
后臺:
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 MapqueryAll() { Map map = new HashMap<>(); List 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 deleteById(@RequestBody HashMap map2) { Map map = new HashMap<>(); int id = Integer.parseInt(map2.get("id")); int row = newsDetailService.deleteById(id); if (row == 1) { List 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 mulDelete(@RequestBody HashMap map2){ Map map = new HashMap<>(); String ids = map2.get("ids"); String mulid[] = ids.split(","); int row = 0; for(int i=0; i 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 updateById(@RequestBody HashMap map2) throws IOException { Map 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 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 update2(NewsDetail newsDetail) { Map map = new HashMap<>(); /*newsDetail.setCreateDate(new Date());*/ int row = newsDetailService.updateById(newsDetail); if (row == 1) { List 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 add(@RequestBody HashMap map2) { Map 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 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; } } }
親測有效,希望大家提出不足!謝謝!另外后臺里面有一個(gè)時(shí)間轉(zhuǎn)換的類(DateUtil.formatDate()),前端有一個(gè)例如url: '${ctx}/news/update/',其中的${cxt}是一個(gè)指定的路徑,后臺寫了一個(gè)類,這些有需要的,可以看我的博客,謝謝!
以上這篇layui+SSM的數(shù)據(jù)表的增刪改實(shí)例(利用彈框添加、修改)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持創(chuàng)新互聯(lián)。