毕设要做一个多条件模糊查询,像这样
可以只选择一个条件,或者全部选择,所以下拉列表特意加了“year”“month”等
jsp代码:
<div class="search">
<form class="form-inline" action="searchArticle" method="post">
<label for="year">年:</label>
<select class="form-control" id="year" name="year">
<option>year</option>
<option>2020</option>
<option>2019</option>
<option>2018</option>
</select>
<label for="month">月:</label>
<select class="form-control" id="month" name="month">
<option>month</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
</select>
<label for="label">分类专栏:</label>
<select class="form-control" id="label" name="label">
<option>选择标签</option>
<option>驴行游记</option>
<option>骑行天下</option>
<option>摄影一族</option>
<option>驴友装备</option>
<option>驴友学堂</option>
<option>驴友资讯</option>
</select>
<input type="text" class="form-control" id="title" placeholder="请输入标题关键字" name="title">
<button type="submit" class="btn" style="background-color: rgba(35,119,249,0.19)">搜索</button>
</form>
</div>
controller接受
// 文章管理-搜索 searchArticle
@RequestMapping(value = "searchArticle")
public String searchArticle(HttpServletRequest request, HttpSession session) {
Integer userid = (Integer) session.getAttribute("userid");
String year = request.getParameter("year");
String month = request.getParameter("month");
String label = request.getParameter("label");
String title = request.getParameter("title");
System.out.println(userid + year + month + title + label);
if (year.equals("year")){
year=null;
}
if (month.equals("month")){
month=null;
}
if (label.equals("选择标签")){
label=null;
}
System.out.println(userid + year + month + title + label);
Map<Object, Object> map = new HashMap<>();
int pageIndex = 1;
if (request.getParameter("pageIndex") != null)
pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
//根据用户id取出数据,10表示pagesize=10,每页10条数据,
List<Article> listArticle = articleService.searchArticle(userid, year, month, label, title, pageIndex, 10);
//将listArticle封装到pageutil类中
PageUtil<Article> pageUtil = new PageUtil<Article>();
pageUtil.setList(listArticle);
//pagenumber表示共多少条数据
Integer pageNumber = articleService.selectArticleNumByLabel(userid,label);
//pagecount表示多少页
int pageCount;
if (pageNumber % 10 == 0) {
pageCount = pageNumber / 10;
} else {
pageCount = (pageNumber / 10) + 1;
}
pageUtil.setPageCount(pageCount);
pageUtil.setPageNumber(pageNumber);
pageUtil.setPageIndex(pageIndex);
request.setAttribute("pageUtil", pageUtil);
session.setAttribute("listArticle", listArticle);
return "article/article_manage_search";
}
我用到了分页,但这里重点将搜索,所以只看重点部分即可。
service部分:
public List<Article> searchArticle(Integer userid, String year, String month, String label, String title, int pageIndex, int pageSize) {
return articleMapper.searchArticle(userid,year,month,label,title, (pageIndex - 1) * pageSize, pageSize);
}
mapper.xml
<!-- 帖子管理-搜索 searchArticle-->
<select id="searchArticle" parameterType="com.mandy.model.Article" resultMap="BaseResultMap">
select fid, title, content, label, time, userid, username, readNum,likes,collectNum,commentNum
from article
where userid = #{userid,jdbcType=INTEGER}
<if test='year!=null and year!=" "'>
AND time like CONCAT('%',#{year},'%')
</if>
<if test='month!=null and month!=" "'>
AND time like CONCAT('%',#{month},'%')
</if>
<if test='label!=null and label!=" "'>
AND label = #{label,jdbcType=VARCHAR}
</if>
<if test='title!=null and title!=" "'>
AND title like CONCAT('%',#{title},'%')
</if>
limit #{pageIndex,jdbcType=INTEGER},#{pageSize,jdbcType=INTEGER}
</select>
记录一下重点的,虽然满足了我现有搜索功能,但是bug肯定有的,先记下再说,搞不好之后还要用上。