因添加资源时数据太多很难筛选,添加资源管理的搜索功能进行检索
首先需要新增功能(查询所有的一级分类)
能够使搜索栏中的菜单直接下拉出所有的一级分类
ResourcesDao中新增持久层接口接口:
//获取所有的一级分类名称
List<Resources> AllfirstType();
搜索博客管理列表mapper
在ResourcesDao.xml文件中添加:
<!--查询所有的一级分类名称-->
<select id="AllfirstType" resultType="com.zx.blog.entity.Resources">
select firstType from blog.t_resource group by firstType
</select>
业务层接口 在ResourcesService下添加:
//获取所有的一级分类名称
List<Resources> AllfirstType();
接口实现: 在ResourcesServiceImpl类中添加:
//获取所有的一级分类名称
@Override
public List<Resources> AllfirstType() {
return resourcesDao.AllfirstType();
}
搜索博客管理列表控制器
在ResourcesController类中的resources方法中添加
model.addAttribute("resources", resourcesService.AllfirstType());
完整代码如下:
@GetMapping("/resources")
public String resources(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
PageHelper.startPage(pageNum,10);
List<Resources> listresources = resourcesService.listResources();
PageInfo<Resources> pageInfo = new PageInfo<Resources>(listresources);
model.addAttribute("resources", resourcesService.AllfirstType());
model.addAttribute("pageInfo",pageInfo);
return "admin/resources";
}
完成前后端交互
<div class="default text">分类</div>
<div class="menu">
<div th:each="resource : ${resources}" class="item" data-value="1" th:data-value="${resource.firstType}" th:text="${resource.firstType}">一级标签</div>
</div>
运行:
根据一级分类与资源名称进行搜索
ResourcesDao中新增持久层接口接口:
//搜索博客管理列表
List<Resources> searchByresourceNameAndfirstType(Resources Resources);
搜索博客管理列表mapper
在ResourcesDao.xml文件中添加:
<!--搜索博客管理列表-->
<select id="searchByresourceNameAndfirstType" parameterType="com.zx.blog.entity.Resources" resultType="com.zx.blog.entity.Resources">
<bind name="pattern" value="'%' + resourceName + '%'" />
select * from blog.t_resource b
<where>
<if test="firstType != null">
and b.firstType = #{firstType}
</if>
<if test="resourceName != null">
and b.resourceName like #{pattern}
</if>
</where>
</select>
业务层接口 在ResourcesService下添加:
//搜索博客管理列表
List<Resources> getResourcesBySearch(Resources Resources);
接口实现: 在ResourcesServiceImpl类中添加:
//搜索博客管理列表
@Override
public List<Resources> getResourcesBySearch(Resources Resources) {
return resourcesDao.searchByresourceNameAndfirstType(Resources);
}
管理列表控制器
在ResourcesController类中添加
//搜索博客管理列表
@PostMapping("/resources/search")
public String search(Resources resources, Model model,
@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
List<Resources> resourcesBySearch = resourcesService.getResourcesBySearch(resources);
PageHelper.startPage(pageNum, 10);
PageInfo<Resources> pageInfo = new PageInfo<>(resourcesBySearch);
model.addAttribute("pageInfo", pageInfo);
return "admin/resources :: resourcesList";
}
前后端交互
<div class="field">
<button type="button" id="search-btn" class="ui mini teal basic button"><i class="search icon"></i>搜索</button>
</div>
//js
$("#search-btn").click(function () {
$("[name='page']").val(0);
loaddata();
});