后台分类管理功能设计(个人博客)
1、编写dao层
@Mapper
public interface CategoryMapper {
public List<Category> allCategories();
@Delete("delete from category where id = #{id}")
public int deleteCategory(int id);
@Update("update category set status_id = #{statusId},name = #{name},detail = #{detail} where id = #{id}")
public int updateCategory(Category category);
@Insert("insert into category(status_id,name,detail) values(#{statusId},#{name},#{detail})")
public int addCategory(Category category);
public Category getCategoryById(int id);
@Select("select count(id) from category")
int countArt();
}
2、编写绑定配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.pang.dao.CategoryMapper" >
<resultMap id="BaseResultMap" type="category" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="status_id" property="statusId" jdbcType="BIGINT" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="detail" property="detail" jdbcType="VARCHAR" />
<result column="count" property="articleCount" jdbcType="BIGINT" />
</resultMap>
<select id="allCategories" resultMap="BaseResultMap">
select c.id,c.status_id,c.name,c.detail,count(a.id) count
from category c
left join article a
on c.status_id = a.category_id
group by c.id
</select>
<select id="getCategoryById" resultMap="BaseResultMap">
select c.id,c.status_id,c.name,c.detail,count(a.id) count
from category c
left join article a
on c.status_id = a.category_id
where c.id = #{id}
group by c.id
</select>
</mapper>
3、编写service层
@Service
public class CategoryService {
@Autowired
private CategoryMapper categoryMapper;
public List<Category> allCategories(){
return categoryMapper.allCategories();
};
public int deleteCategory(int id){
return categoryMapper.deleteCategory(id);
};
public int updateCategory(Category category){
return categoryMapper.updateCategory(category);
};
public int addCategory(Category category){
return categoryMapper.addCategory(category);
};
public Category getCategoryById(int id){
return categoryMapper.getCategoryById(id);
};
public int countArt(){
return categoryMapper.countArt();
}
}
4、编写controller层
@RequestMapping("/admin/toFen")
public String toAdFen(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,Model model){
PageHelper.startPage(pageNum,4);
List<Category> categories = categoryService.allCategories();
PageInfo<Category> pageInfo = new PageInfo<>(categories);
model.addAttribute("pageInfo",pageInfo);
return "admin/types";
}
@RequestMapping("/admin/toFen/toAddType")
public String toAdTy(Model model){
model.addAttribute("type",new Category());
return "admin/types-input";
}
@RequestMapping("/admin/toUpdateTy/{id}")
public String toUpTy(@PathVariable("id")int id,Model model){
Category categoryById = categoryService.getCategoryById(id);
model.addAttribute("type",categoryById);
System.out.println(categoryById.getStatusId());
return "admin/types-update";
}
@RequestMapping("/admin/uptType")
public String edit(Category category){
categoryService.updateCategory(category);
return "redirect:/admin/toFen";
}
@RequestMapping("/admin/addType")
public String add(Category category){
categoryService.addCategory(category);
return "redirect:/admin/toFen";
}
@RequestMapping("/admin/deleteTy/{id}")
public String edit(@PathVariable("id") int id){
categoryService.deleteCategory(id);
return "redirect:/admin/toFen";
}
5、绑定前端页面
<table class="ui celled table">
<thead>
<tr>
<th>编号</th>
<th>类型</th>
<th>详情</th>
<th>文章数</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr th:each="type:${pageInfo.list}">
<td th:text="${type.statusId}"></td>
<td th:text="${type.name}"></td>
<td th:text="${type.detail}"></td>
<td th:text="${type.articleCount}"></td>
<td>
<a th:href="@{/admin/toUpdateTy/{id}(id=${type.id})}" class="ui mini teal basic button">编辑</a>
<a th:href="@{/admin/deleteTy/{id}(id=${type.id})}" class="ui mini red basic button">删除</a>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<th colspan="6">
<div class="ui mini pagination menu">
<a class="icon item" th:href="@{/admin/toFen(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}" th:unless="${pageInfo.isFirstPage}">上一页</a>
<a class="icon item" th:href="@{/admin/toFen(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}" th:unless="${pageInfo.isLastPage}">下一页</a>
</div>
<a th:href="@{/admin/toFen/toAddType}" class="ui right floated mini teal basic button">新增</a>
</th>
</tr>
</tfoot>
</table>