《学生教务系统》之标签管理模块实现
一、标签管理实现前提
注意:在实现城市功能之前我们可以参考一下:
(1)《学生教务系统》立项需求说明书
(2)《学生教务系统》之城市管理板块实现
(1)标签管理数据库设计(t_mark)
注:其中学生表,班级表里面均有标签,我们建表之前最好在学生表,班级表之前建学生表
--标签表 create table t_mark( m_id int primary key auto_increment, m_name varchar(20) not null, m_time date, m_type varchar(20) )
(2)标签功能示意图
(3)标签功能实现效果图
二、增删/查询所有功能实现
(1)实体类(bean类)
public class Mark implements Serializable { private Integer mid; private String mname; private Date mtime; private String mtype;
(2)dao接口
public interface MarkDao { /** * 添加标签 * @param mark */ public void insertMark(Mark mark); /** * 删除标签 * @param id */ public void deleteMark(Integer id); /** * 展示所有标签 * @return */ public List<Mark> selectMark(); }
(3)Mybatis实现Dao接口
<mapper namespace="com.tjcu.dao.MarkDao"> <!--标签的的动态SQL语句--> <resultMap id="SelectMark" type="mark"> <id property="mid" column="m_id"></id> <result property="mname" column="m_name"></result> <result property="mtime" column="m_time"></result> <result property="mtype" column="m_type"></result> </resultMap> <!--添加标签--> <insert id="insertMark"> insert into t_mark values (#{mid}, #{mname}, #{mtime}, #{mtype}) </insert> <!--删除标签--> <delete id="deleteMark"> delete from t_mark where m_id = #{id} </delete> <!--查询所有--> <select id="selectMark" resultMap="SelectMark"> select m_id, m_name, m_time, m_type from t_mark </select> </mapper>
(4)Service接口和Service的实现类
Service接口
public interface MarkService { /** * 添加标签 * @param mark */ public void insertMark(Mark mark); /** * 删除标签 * @param id */ public void deleteMark(Integer id); /** * 展示所有标签 * @return */ public List<Mark> selectMark(); }
Service的实现类
public class MarkServiceImpl implements MarkService { @Override public void insertMark(Mark mark) { MarkDao mapper = (MarkDao) MybatisUtil.getMapper(MarkDao.class); mapper.insertMark(mark); MybatisUtil.commit(); } @Override public void deleteMark(Integer id) { MarkDao mapper = (MarkDao) MybatisUtil.getMapper(MarkDao.class); mapper.deleteMark(id); MybatisUtil.commit(); } @Override public List<Mark> selectMark() { MarkDao mapper = (MarkDao) MybatisUtil.getMapper(MarkDao.class); List<Mark> marks = mapper.selectMark(); MybatisUtil.close(); return marks; } }
(5)Struts2实现Controller层
public class MarkAction extends ActionSupport { private Mark mark; private List<Mark> marks; public String addMark(){ MarkService markService = new MarkServiceImpl(); mark.setMid(null); mark.setMtime(new Date()); System.out.println(mark); markService.insertMark(mark); return SUCCESS; } public String deleteMark(){ MarkService markService = new MarkServiceImpl(); markService.deleteMark(mark.getMid()); return SUCCESS; } public String selectMark(){ MarkService markService = new MarkServiceImpl(); marks = markService.selectMark(); for (Mark mark : marks) { System.out.println(mark); } return SUCCESS; } public Mark getMark() { return mark; } public void setMark(Mark mark) { this.mark = mark; } public List<Mark> getMarks() { return marks; } public void setMarks(List<Mark> marks) { this.marks = marks; } }
(6)测试类
public class MarkTest { @Test public void insert(){ MarkService markService = new MarkServiceImpl(); Mark mark = new Mark(null,"懒散",new Date(),"学生"); markService.insertMark(mark); } @Test public void delete(){ MarkService markService = new MarkServiceImpl(); markService.deleteMark(3); } @Test public void select(){ MarkService markService = new MarkServiceImpl(); List<Mark> marks = markService.selectMark(); for (Mark mark : marks) { System.out.println(mark); } }
(7)Struts核心配置
<!--标签--> <package name="mark" extends="struts-default" namespace="/mark"> <action name="addMark" method="addMark" class="com.tjcu.action.MarkAction"> <result name="success" type="redirectAction">selectMark</result> </action> <action name="deleteMark" method="deleteMark" class="com.tjcu.action.MarkAction"> <result name="success" type="redirectAction">selectMark</result> </action> <action name="selectMark" method="selectMark" class="com.tjcu.action.MarkAction"> <result name="success" type="dispatcher">/mark/showMark.jsp</result> </action> </package>
(8)JSP页面
展示页面
<%@ page import="java.util.Date" %> <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isELIgnored="false" %> <html> <head> <title>add</title> <meta name="keywords" content="keyword1,keyword2,keyword3"> <meta name="description" content="标签的添加"> <meta name="content-type" content="text/html; charset=utf-8"> <meta charset="UTF-8"> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/statics/css/bootstrap.min.css"> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-sm-12"> <form action="${pageContext.request.contextPath}/mark/addMark" method="get"> <div class="form-group"> <label>标签名称:</label> <input type="text" class="form-control" name="mark.mname"/> </div> <div class="form-group"> <label>标签类型:</label> <select name="mark.mtype" class="form-control"> <option value="班级" >班级</option> <option value="学生">学生</option> </select> </div> <input type="submit" class="btn btn-danger btn-block" value="提交"/> <input type="button" class="btn btn-info btn-block" value="返回上级" onclick="history.go(-1);"/> </form> </div> </div> </div> </body> </html>
添加页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" isELIgnored="false" %> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <html lang="en"> <head> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <link rel="stylesheet" type="text/css" href="../statics/css/bootstrap.min.css"> <title>标签列表</title> <script type="text/javascript" src="${pageContext.request.contextPath}/statics/js/jquery-1.8.3.min.js"></script> <script> $(function () { //修改class的属性,变色 使用过滤选择器实现了 $("span[value='班级']").prop("class","label label-info"); }) </script> </head> <body> <div class="container-fluid"> <div class="row"> <div class="col-sm-12"> <input type="button" value="添加标签" class="btn btn-sm btn-info" onclick="location.href='addMark.jsp'"/> </div> </div> <div class="row" style="margin-top: 10px; "> <div class="col-sm-12"> <table class="table table-hover table-bordered table-striped"> <tr> <th>编号</th> <th>名称</th> <th>创建时间</th> <th>标签类型</th> <th>操作</th> </tr> <c:forEach var="mark" items="${requestScope.marks}" varStatus="m"> <tr> <td>${m.count}</td> <td>${mark.mname}</td> <td><fmt:formatDate value="${mark.mtime}" pattern="yyyy年mm月dd日" ></fmt:formatDate></td> <td> <span class="label label-warning" value="${mark.mtype}">${mark.mtype}</span> </td> <td><a href="${pageContext.request.contextPath}/mark/deleteMark?mark.mid=${mark.mid}" class="btn btn-danger btn-sm">删除</a></td> </tr> </c:forEach> </table> </div> </div> </div> </body> </html>