JSP+Servlet+javabean+mysql实现页面多条件模糊查询(转载)
Posted 佬鬼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSP+Servlet+javabean+mysql实现页面多条件模糊查询(转载)相关的知识,希望对你有一定的参考价值。
需求:
一般列表页上面会有一个查询框,有各种的查询条件组合,一般都采用模糊查询方式 ,以下以自己做的实例来说明一下实现方法:
需要实现的界面原型:要满足条件:
1、单选分类,点GO按扭
2、单独输入标题关键字,点GO按扭
3、选择分类,再输入关键字,点GO按扭
我这里用了MVC分层模式来进行的,所以一步步讲解吧,上源码:
因为我一个class里写了很多不同的业务,所以帖代码只帖当前步
dao层:
1 //当前页显示的新闻信息pageNo 当前页码,pagePerCount是每页多少条数据
2 public List<NewsDetail> getPageNewsList(int pageNo,int pagePerCount,String where);
daoImpl层:
这里方法中加的参数是where,因为我不确定前台可能有几个模糊查询的条件,所以这里只能用个整体的字符串来定义,到时候用StringBuffer来拼就行
这里注意一下字符串拼接加变量的写法
1 public List<NewsDetail> getPageNewsList(int pageNo, int pagePerCount,String where) {
2 List<NewsDetail> newslist =new ArrayList<NewsDetail>();
3 String sql = "select d.id,d.title,d.author,d.summary,d.content,d.picPath,d.createDate,d.modifyDate," +
4 "d.createDate,d.categoryId,c.name as categoryname from news_detail as d,news_category as c" +
5 " where c.id=d.categoryId and d.status=1 "+where+"order by d.createDate desc limit ?,?";
6 Object[] params ={(pageNo-1)*pagePerCount,pagePerCount};
7 if(this.getconnection()){
8 ResultSet rs = this.executeQuery(sql, params);
9 try {
10 while(rs.next()){
11 NewsDetail news = new NewsDetail();
12 int id = rs.getInt("id");
13 String title1 = rs.getString("title");
14 String author =rs.getString("author");
15 int categoryId = rs.getInt("categoryId");
16 String categoryname=rs.getString("categoryname");
17 String summary = rs.getString("summary");
18 String content = rs.getString("content");
19 String picPath =rs.getString("picPath");
20 Timestamp createDate =rs.getTimestamp("createDate");
21 Timestamp modifyDate =rs.getTimestamp("modifyDate");
22 news.setId(id);
23 news.setCategoryId(categoryId);
24 news.setAuthor(author);
25 news.setCategoryname(categoryname);
26 news.setContent(content);
27 news.setSummary(summary);
28 news.setPicPath(picPath);
29 news.setCreateDate(createDate);
30 news.setModifyDate(modifyDate);
31 news.setTitle(title1);
32 newslist.add(news);
33
34 }
35 } catch (SQLException e) {
36 e.printStackTrace();
37 }finally{
38 this.clossconnection();
39 }
40 }
41 return newslist;
42 }
service接口:
1 //当前页显示的新闻信息pageNo 当前页码,pagePerCount是每页多少条数据
2 public List<NewsDetail> getPageNewsList(int pageNo,int pagePerCount,String where);
serviceImpl接口实现类:
因为业务比较简单,所以代码也很简单哈,service只是调一下dao即可
1 public class NewsServiceImpl implements NewsService {
2 private NewsDao newsdao =null;
3 public NewsServiceImpl(){
4 newsdao=new NewsDaoImpl();
5 newscategory1 =new NewsCategoryDaoImpl();
6 }
7 public int getNewsCount(String where) {
8 return newsdao.getNewsCount(where);
9 }
10 }
以下是重点,套页面JSP+Servlet
newsDetailList.jsp:我把样式那些都省略了,只列出整体的页面框架元素
1 <div class="main-content-right">
2 <!--即时新闻-->
3 <div class="main-text-box">
4 <div class="main-text-box-tbg">
5 <div class="main-text-box-bbg">
6 <form name ="searchForm" id="searchForm" action="<%=request.getContextPath() %>/servlet/newsListByLikeServlet" method="post">
7 <div>
8 新闻分类:
9 <select name="categoryId">
10 <option value="0">全部</option><!--这里要注意,到servlet中获取分类的时候,值为0要加判断,为0不需要加查询,默认是全部就是不选择分类的情况-->
11 <c:forEach var="category" items="${categorylist }" varStatus="status">
12 <option value=\'${category.id }\' >${category.name }</option>
13 </c:forEach>
14
15
16
17 </select>
18 新闻标题<input type="text" name="title" id="title" value=\'\'/>
19 <button type="submit" class="page-btn" onclick="javascript:window.location.href=\'<%=request.getContextPath() %>/servlet/newsListByLikeServlet\'">GO</button>
20 <button type="button" onclick="addNews();" class="page-btn">增加</button>
21 <!--隐藏域,当前页码 -->
22 <input type="hidden" id="pageIndex" name="pageIndex" value="1"/>
23
24 <input type="hidden" name="pageSize" value="10"/>
25
26 </div>
27 </form>
28 <table cellpadding="1" cellspacing="1" class="admin-list">
29 <thead >
30 <tr class="admin-list-head">
31 <th align="center">新闻标题</th>
32 <th align="center">新闻类别</th>
33 <th align="center">作者</th>
34 <th align="center">创建时间</th>
35 <th align="center">操作</th>
36 </tr>
37 </thead>
38
39 <tbody>