基于SSM+Layui的图书管理系统(7) 您所在的位置:网站首页 pageinfo转list 基于SSM+Layui的图书管理系统(7)

基于SSM+Layui的图书管理系统(7)

2023-03-08 15:39| 来源: 网络整理| 查看: 265

前言

与前面也类似,注意将添加改成发布公告即可,编辑改成查看详情,已经发布的公告只能查看详情或删除,不能进行修改。

完整版本已开发完毕,并开源,见该篇文章 https://blog.csdn.net/weixin_39615182/article/details/115458248

功能实现

Notice.java中createDate字段,需要加上对日期格式化的注解

@DateTimeFormat(pattern = "yyyy-MM-dd")//前台向后台传日期的接收模型 @JSONField(format="yyyy-MM-dd")//对返回的时间对象用json格式化时间 private Date createDate;

dao层实现 在NoticeMapper中增加几个方法

/** * 查询公告信息 */ List queryNoticeAll(@Param("content") String content); /** * 公告删除 */ void deleteNoticeByIds(List id); /** * 根据id查询详细查询 */ Notice queryNoticeById(Integer id);

对应NoticeMapper.xml,底部新增

select * from notice and content like '%${content}%' select * from notice where id=#{id} delete from notice where id in #{id}

service层 新建NoticeService.java

package com.yx.service; import com.github.pagehelper.PageInfo; import com.yx.entity.Notice; import java.util.List; public interface NoticeService { //查询所有公告 PageInfo queryNoticeAll(String content, int page, int limit); //添加公告 void insertNoticeInfo(Notice notice); //查询根据id Notice queryNoticeById(Integer id); //删除 void deleteNoticeByIds(List ids); }

NoticeServiceImpl.java

package com.yx.service.impl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.yx.dao.NoticeMapper; import com.yx.entity.Notice; import com.yx.service.NoticeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; @Service("noticeService") public class NoticeServiceImpl implements NoticeService { @Autowired private NoticeMapper noticeMapper; @Override public PageInfo queryNoticeAll(String content, int page, int limit) { PageHelper.startPage(page,limit); List list=noticeMapper.queryNoticeAll(content); PageInfo pageInfo=new PageInfo(list); return pageInfo; } @Override public void insertNoticeInfo(Notice notice) { noticeMapper.insert(notice); } @Override public Notice queryNoticeById(Integer id) { return noticeMapper.queryNoticeById(id); } @Override public void deleteNoticeByIds(List ids) { List list=new ArrayList(); for(String id:ids){ list.add(Integer.valueOf(id)); } noticeMapper.deleteNoticeByIds(list); } }

Controller层 NoticeController.java

package com.yx.controller; import com.github.pagehelper.PageInfo; import com.yx.entity.Notice; import com.yx.service.NoticeService; import com.yx.util.DataInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import java.util.Arrays; import java.util.Date; import java.util.List; @Controller public class NoticeController { @Autowired private NoticeService noticeService; @GetMapping("/noticeIndex") public String noticeIndex(){ return "notice/noticeIndex"; } @GetMapping("/noticeIndex2") public String noticeIndex2(){ return "notice/noticeIndex2"; } //发布公告页面跳转 @GetMapping("/addNotice") public String addNotice(){ return "notice/addNotice"; } //详细转 @GetMapping("/queryNotice") public String queryNoticeById(Integer id, Model model){ //根据id查询公告详细信息 Notice notice=noticeService.queryNoticeById(id); model.addAttribute("info",notice); return "notice/queryNotice"; } /** * 查询所有的公告信息 */ @RequestMapping("/noticeAll") @ResponseBody public DataInfo noticeAll(String content, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "15")Integer limit){ PageInfo pageInfo= noticeService.queryNoticeAll(content,page,limit); return DataInfo.ok("成功",pageInfo.getTotal(),pageInfo.getList()); } /** * 公告发布提交方法 */ @ResponseBody @RequestMapping("/addNoticeSubmit") public DataInfo addNoticeSubmit(Notice notice){ //设置进入 notice.setAuthor(1); notice.setCreateDate(new Date()); noticeService.insertNoticeInfo(notice); return DataInfo.ok(); } /** * 根据ids删除公告信息 */ @ResponseBody @RequestMapping("/deleteNoticeByIds") public DataInfo deleteNoticeByIds(String ids){ List list= Arrays.asList(ids.split(",")); noticeService.deleteNoticeByIds(list); return DataInfo.ok(); } }

然后是对应的jsp addNotice.jsp

layui body { background-color: #ffffff; } 公告内容 发布公告 layui.use(['form'], function () { var form = layui.form, layer = layui.layer, $ = layui.$; //监听提交 form.on('submit(saveBtn)', function (data) { var datas=data.field;//form单中的数据信息 //向后台发送数据提交添加 $.ajax({ url:"addNoticeSubmit", type:"POST", data:datas, success:function(result){ if(result.code==0){//如果成功 layer.msg('发布成功',{ icon:6, time:500 },function(){ parent.window.location.reload(); var iframeIndex = parent.layer.getFrameIndex(window.name); parent.layer.close(iframeIndex); }) }else{ layer.msg("发布失败"); } } }) return false; }); });

noticeIndex.jsp

layui 内容关键字: 搜索 发布公告 删除 查询详情 删除 layui.use(['form', 'table'], function () { var $ = layui.jquery, form = layui.form, table = layui.table; table.render({ elem: '#currentTableId', url: 'noticeAll',//查询类型数据 toolbar: '#toolbarDemo', defaultToolbar: ['filter', 'exports', 'print', { title: '提示', layEvent: 'LAYTABLE_TIPS', icon: 'layui-icon-tips' }], cols: [[ {type: "checkbox", width: 50}, {field: 'id', width: 100, title: 'ID', sort: true}, {templet:"{{d.content.substring(0,15)}}....", width: 180, title: '公告内容'}, {templet:"{{layui.util.toDateString(d.createDate,'yyyy-MM-dd HH:mm:ss' )}}", width: 280, title: '发布时间'}, {field: 'author', width: 100, title: '发布人'}, {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"} ]], limits: [10, 15, 20, 25, 50, 100], limit: 15, page: true, skin: 'line', id:'testReload' }); var $ = layui.$, active = { reload: function(){ var content = $('#content').val(); //执行重载 table.reload('testReload', { page: { curr: 1 //重新从第 1 页开始 } ,where: { content: content } }, 'data'); } }; $('.demoTable .layui-btn').on('click', function(){ var type = $(this).data('type'); active[type] ? active[type].call(this) : ''; }); /** * tool操作栏监听事件 */ table.on('tool(currentTableFilter)', function (obj) { var data=obj.data; if (obj.event === 'query') { // 监听添加操作 var index = layer.open({ title: '公告详细', type: 2, shade: 0.2, maxmin:true, shadeClose: true, area: ['60%', '60%'], content: 'queryNotice?id='+data.id, }); $(window).on("resize", function () { layer.full(index); }); } else if (obj.event === 'delete') { // 监听删除操作 layer.confirm('真的删除行么', function (index) { //调用删除功能 deleteInfoByIds(data.id,index); layer.close(index); }); } }); //监听表格复选框选择 table.on('checkbox(currentTableFilter)', function (obj) { console.log(obj) }); /** * 获取选中记录的id信息 */ function getCheackId(data){ var arr=new Array(); for(var i=0;i //向后台发送请求 $.ajax({ url: "deleteNoticeByIds", type: "POST", data: {ids: ids}, success: function (result) { if (result.code == 0) {//如果成功 layer.msg('删除成功', { icon: 6, time: 500 }, function () { parent.window.location.reload(); var iframeIndex = parent.layer.getFrameIndex(window.name); parent.layer.close(iframeIndex); }); } else { layer.msg("删除失败"); } } }) }; /** * toolbar监听事件 */ table.on('toolbar(currentTableFilter)', function (obj) { if (obj.event === 'add') { // 监听添加操作 var index = layer.open({ title: '发布公告', type: 2, shade: 0.2, maxmin:true, shadeClose: true, area: ['60%', '60%'], content: 'addNotice', }); $(window).on("resize", function () { layer.full(index); }); } else if (obj.event === 'delete') { /* 1、提示内容,必须删除大于0条 2、获取要删除记录的id信息 3、提交删除功能 ajax */ //获取选中的记录信息 var checkStatus=table.checkStatus(obj.config.id); var data=checkStatus.data; if(data.length==0){//如果没有选中信息 layer.msg("请选择要删除的记录信息"); }else{ //获取记录信息的id集合 var ids=getCheackId(data); layer.confirm('真的删除行么', function (index) { //调用删除功能 deleteInfoByIds(ids,index); layer.close(index); }); } } }); });

queryNotice.jsp

layui body { background-color: #ffffff; } 公告信息 ${info.content} 发布人 ${info.author} 发布时间

到数据库中加一条数据

运行Tomcat 框架提供的显示/隐藏功能可以,可隐藏ID

所有功能测试通过



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有