一、查询所有标签
1.Tag
//标签对象
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@ToString
public class Tag {
private String id;
private String name;
private String type;
private Date createtime;
}
2.TagController
@Controller
@RequestMapping("tag")
public class TagController {
@Autowired
private TagService tagService;
//查询所有标签的方法
@RequestMapping("findAll")
public String findAll(Model model){
List<Tag> tags = tagService.findAll();
model.addAttribute("tags",tags);
return "back/tag/index";
}
}
3.TagService
public interface TagService {
List<Tag> findAll();
}
4.TagServiceImpl
@Service
@Transactional
public class TagServiceImpl implements TagService {
@Resource
private TagMapper tagMapper;
@Override
@Transactional(propagation = Propagation.SUPPORTS)
public List<Tag> findAll() {
return tagMapper.findAll();
}
}
5.TagMapper
public interface TagMapper extends BaseMapper<Tag,String>{
}
6.TagMapper.xml
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.TagMapper">
<select id="findAll" resultType="Tag">
select id,name,type,createtime
from t_tag
</select>
</mapper>
7.index.jsp
<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt_rt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html; UTF-8" pageEncoding="UTF-8" isELIgnored="false" %>
<!doctype html>
<html lang="en">
<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">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<title>标签管理</title>
</head>
<body>
<%--标签管理--%>
<div class="container-fluid">
<div class="row">
<div class="col-sm-4 col-sm-offset-4">
<h1 class="text-center">标签管理</h1>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<table class="table table-bordered table-striped table-hover">
<tr>
<th>编号</th>
<th>名称</th>
<th>标签类型</th>
<th>创建时间</th>
<th>操作</th>
</tr>
<c:forEach items="${requestScope.tags}" var="tag">
<tr>
<td>${tag.id}</td>
<td>${tag.name}</td>
<td>${tag.type}</td>
<td><fmt:formatDate value="${tag.createtime}"/></td>
<td>
<a href="" class="btn btn-info">修改</a>
<a href="" class="btn btn-danger">删除</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</div>
</body>
</html>
二、增加功能
1.TagController
@RequestMapping("save")
public String save(Tag tag){
tagService.save(tag);
return "redirect:/tag/findAll";
}
2.TagService
void save(Tag tag);
3.TagServiceImpl
@Override
public void save(Tag tag) {
//设置标签的创建时间
tag.setCreatetime(new Date());
tagMapper.save(tag);
}
4.TagMapper
public interface TagMapper extends BaseMapper<Tag,String>{
}
5.TagMappper.xml
<insert id="save" parameterType="Tag" useGeneratedKeys="true" keyProperty="id">
insert into t_tag values (#{id},#{name},#{type},#{createtime})
</insert>
6.index.jsp
<div class="row">
<div class="col-sm-12">
<form action="${pageContext.request.contextPath}/tag/save" method="post" id="inputForm" class="form-inline">
<div class="form-group">
<label for="name">标签名称:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="请输入标签名称...">
</div>
<div class="form-group">
<label for="type">标签类型:</label>
<select name="type" id="type" style="width: 200px;" class="form-control">
<option value="班级">班级标签</option>
<option value="学生">学生标签</option>
</select>
</div>
<button type="submit" class="btn btn-success">添加标签</button>
</form>
</div>
</div>
<script>
$(function () {
$("#inputForm").submit(function () {
if (!$("#name").val()){
alert("请输入标签名称!");
return false;
}
return true;
});
})
</script>
7.启动项目