多条件查询

多条件查询

步骤分析:
    在product_list.jsp页面上添加一个表单,添加一个查询按钮
    提交的路径 findProductByCondition
        1.获取两个条件
        2.调用service完成查询 返回值:list
        3.将list放入request域中,请求转发
    productDao
        基本sql:select * from product where 1=1
        若商品名称不为空 and pname like ...

        若商品名不为空 and pdesc like ...

FindProductByConditionServlet

package com.feizhu.web.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.feizhu.domain.Product;
import com.feizhu.service.ProductService;

/**
 *多条件查询
 */
public class FindProductByConditionServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       
        //设置编码
        request.setCharacterEncoding("utf-8");
        //接受两个参数
        String name= request.getParameter("name");
        String kw= request.getParameter("kw");
        
        //调用service   完成操作  返回值:list
                 List<Product> plist = null;
                try {
                    plist = new ProductService().findProductByCondition(name,kw);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }    
        //将list放入request域中,请求转发
                 request.setAttribute("list", plist);
                 request.getRequestDispatcher("/product_list.jsp").forward(request, response);;
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
 

 

Service:

    /**
     *
     * @param name 商品的名称
     * @param kw 关键词
     * @return list
     * @throws SQLException
     */
    public List<Product> findProductByCondition(String name, String kw) throws SQLException {
    
        return   new ProductDao().findProductByCondition( name, kw);

    }

Dao

/**
     * 多条件查询
     *
     * @param name
     *            商品名词
     * @param kw
     *            关键词
     * @return list
     * @throws SQLException
     */
    public List<Product> findProductByCondition(String name, String kw) throws SQLException {

        QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
        String sql = "select * from product where 1=1 ";

        // 存放参数

        ArrayList<String> params = new ArrayList<>();

        // 判断参数是否为空 拼接sql
        if (name != null && name.trim().length() > 0) {
            sql += ("and pname like ?"); // pname like "%ssss%"
            params.add("%" + name + "%");
        }
        if (kw != null && kw.trim().length() > 0) {
            sql += ("and pdesc like ?"); // pname like "%ssss%"
            params.add("%" + kw + "%");
        }

        return qr.query(sql, new BeanListHandler<>(Product.class), params.toArray());
    }
备注:由于个人原因,本博客暂停更新。如有问题可联系本人,本人提供技术指导、学习方向、学习路线。本人微信wlp1156107728(添加注明来意)   QQ1156107728(添加注明来意)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值