一、多条件查询
public List<Commodity> getbookList(String cId, String cCategory, String cName,
String minprice, String maxprice) {
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
String sql = "select * from commodity where 1=1";
ArrayList<Object> list = new ArrayList<>();
if (!"".equals(cId)&&cId!=null) {
sql+=" and cId = ?";
list.add(cId);
}
if (!"".equals(cCategory)&&cCategory!=null) {
sql+=" and cCategory = ?";
list.add(cCategory);
}
if (!"".equals(cName)&&cName!=null) {
sql += " and cName like ?";//不能在这写% % >> '1002'%
list.add("%"+cName+"%");
}
if (!"".equals(minprice)&&minprice!=null) {
sql+=" and cPrice > ?";
list.add(minprice);
}
if (!"".equals(maxprice)&&maxprice!=null) {
sql+=" and cPrice < ?";
list.add(maxprice);
}
List<Commodity> query = new ArrayList<Commodity>();
try {
if (!list.isEmpty()) {
query = qr.query(sql, list.toArray() , new BeanListHandler<Commodity>(Commodity.class));
}else {
query = qr.query(sql, new BeanListHandler<Commodity>(Commodity.class));
}
} catch (SQLException e) {
e.printStackTrace();
}
return query;
}
注意事项:
1、模糊查找标识% %要写在参数集合里面
2、用list存放参数,query执行的时候参数写list.toArray()
3、用价格区间查找满足条件的记录,price的类型应为double或者int,不然比较的是字符串的长度
4、在sql后面加个where 1=1,无条件查询也可以通用
5,、sql拼串的时候,记得 and 前后有空格
二、完成全选/全不选
1、添加ckAll的单击事件
2、调用执行方法checkAll()
function checkAll() {
var flag = document.getElementById("ckAll");
var items = document.getElementsByName("items");
for(var i=0;items!=null && i<items.length;i++){
var item = items[i];
item.checked = flag.checked;
}
}
将要实现全选功能的checbox绑定onclick事件即可
多选>>删除
jsp>>javascript把选中的多个id拼起来传入servlet
function delBooks() {
var items = document.getElementsByName("items");
var ids = "";
for(var i=0;items!=null && i<items.length;i++){
if (items[i].checked) {
ids+=items[i].value+",";
}
}
location.href="${pageContext.request.contextPath}/servlet/delBookServlet?ids="+ids;
}
java代码
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
String ids = req.getParameter("ids");
service.delBookService(ids);
req.getRequestDispatcher("/servlet/bookListServlet").forward(req, resp);
}
public void delBookService(String ids) {
String[] id = ids.split(",");
for (int i=0;i<id.length;i++) {
daoI.delBookService(id[i]);
}
}
public void delBookService(String id) {
QueryRunner runner = new QueryRunner(C3P0Util.getDataSource());
String sql = "delete from commodity where cId=?";
try {
runner.update(sql, id);
} catch (SQLException e) {
e.printStackTrace();
}
}
三、分页思路
1、实现分页需要哪些参数:
SELECT * FROM book LIMIT (currentPage-1)*pageSize,pageSize;
>>>第一个参数:从哪条开始查 第二个参数:查几条
currentPage; //当前页,可以算出从哪里开始(value1)查找
pageSize;//一页有多少条记录(value2)
count;//计算totalPage,用于前台提示
totalPage;//前台展示2、如何传值
我们展示记录的时候,向、list.jsp页面传入commodity实体对象,加入分页功能后多了四个参数
>>我们封装一个PageBean实体,使他包含commodity和其他分页参数,传入jsp
如果多个页面要实现分页功能,可将private List<Commodity> commodity;中的commodity替换成泛型,具体如下,可通用
public class PageBean {
private int currentPage;//当前页
private int pageSize;//每页显示和条数
private int count;//总记录数
private int totalPage;//共页数
//计算:int totalPage = Math.ceil(count*1.0/pageSize);
private List<Commodity> commodity;
//getter / setter
}
servlet代码,记得赋初值
jsp模块:
<div class="pagination">
<table>
<tr>
<td class="disablepage">
<c:if test="${pgBooks.currentPage >1 }" >
<a href="${pageContext.request.contextPath }/servlet/pageServlet?
currentPage=${pgBooks.currentPage==null?1:pgBooks.currentPage-1}"><<上一页</a>
</c:if>
</td>
<td>第${pgBooks.currentPage }页/共${pgBooks.totalPage }页</td>
<td class="nextPage">
<c:if test="${pgBooks.totalPage > pgBooks.currentPage }">
<a href="${pageContext.request.contextPath }/servlet/pageServlet?
currentPage=${pgBooks.currentPage==null?1:pgBooks.currentPage+1}">下一页>></a>
</c:if>
</td>
</tr>
</table>
</div>
1、如何接收参数,${pgBooks},括号里面用后台相同的名字即可接收,效果和requst.getAttribution("pgBooks")一样
2、给当前页初始值1
3、<c:if>标签判断,如果当前页是第一页隐藏上一页按钮,如果当前页是最后一页隐藏下一页按钮
发现一个bug:
查询分页的时候,如果结果有多页,点下一页出来的不再是条件查询的结果