客户关系管理增删改

servlet:

package cn.itcast.cstm.web.servlet;

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

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

import cn.itcast.commons.CommonUtils;
import cn.itcast.cstm.domain.Customer;
import cn.itcast.cstm.service.CustomerService;
import cn.itcast.servlet.BaseServlet;

/**
 * Web层
 * @author cxf
 *
 */
public class CustomerServlet extends BaseServlet {
    private CustomerService customerService = new CustomerService();
    
    /**
     * 添加客户
     */
    public String add(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*
         * 1. 封装表单数据到Customer对象
         * 2. 补全:cid,使用uuid
         * 3. 使用service方法完成添加工作
         * 4. 向request域中保存成功信息
         * 5. 转发到msg.jsp
         */
        Customer c = CommonUtils.toBean(request.getParameterMap(), Customer.class);
        c.setCid(CommonUtils.uuid());
        customerService.add(c);
        request.setAttribute("msg", "恭喜,添加客户成功!");
        return "f:/msg.jsp";
    }
    
    /**
     * 查询所有
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String findAll(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*
         * 1. 调用service得到所有客户
         * 2. 保存到request域
         * 3. 转发到list.jsp
         */
        request.setAttribute("cstmList", customerService.findAll());
        return "f:/list.jsp";
    }
    
    /**
     * 编辑之前的加载工作
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String preEdit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*
         * 1. 获取cid
         * 2. 使用cid来调用service方法,得到Customer对象
         * 3. 把Customer保存到request域中
         * 4. 转发到edit.jsp显示在表单中
         */
        String cid = request.getParameter("cid");
        Customer cstm = customerService.load(cid);
        request.setAttribute("cstm", cstm);
        return "f:/edit.jsp";
    }
    
    /**
     * 编辑方法
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String edit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*
         * 1. 封装表单数据到Customer对象中
         * 2. 调用service方法完成修改
         * 3. 保存成功信息到request域
         * 4. 转发到msg.jsp显示成功信息
         */
        // 已经封装了cid到Customer对象中
        Customer c = CommonUtils.toBean(request.getParameterMap(), Customer.class);
        customerService.edit(c);
        request.setAttribute("msg", "恭喜,编辑客户成功!");
        return "f:/msg.jsp";
    }
    
    public String query(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*
         * 1. 封装表单数据到Customer对象中,它只有四个属性(cname、gender、cellphone、email)
         *   它就是一个条件
         * 2. 使用Customer调用service方法,得到List<Customer>
         * 3. 保存到request域中
         * 4. 转发到list.jsp
         */
        Customer criteria = CommonUtils.toBean(request.getParameterMap(), Customer.class);
        List<Customer> cstmList = customerService.query(criteria);
        request.setAttribute("cstmList", cstmList);
        return "/list.jsp";
    }
}

DAO:






DAO:

package
cn.itcast.cstm.dao; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import cn.itcast.cstm.domain.Customer; import cn.itcast.jdbc.TxQueryRunner; /** * 持久层 * * @author cxf * */ public class CustomerDao { private QueryRunner qr = new TxQueryRunner(); /** * 添加客户 * * @param c */ public void add(Customer c) { try { String sql = "insert into t_customer values(?,?,?,?,?,?,?)"; Object[] params = { c.getCid(), c.getCname(), c.getGender(), c.getBirthday(), c.getCellphone(), c.getEmail(), c.getDescription()}; qr.update(sql, params); } catch(SQLException e) { throw new RuntimeException(e); } } /** * 查询所有 * @return */ public List<Customer> findAll() { try { String sql = "select * from t_customer"; return qr.query(sql, new BeanListHandler<Customer>(Customer.class)); } catch(SQLException e) { throw new RuntimeException(e); } } /** * 加载客户 * @param cid * @return */ public Customer load(String cid) { try { String sql = "select * from t_customer where cid=?"; return qr.query(sql, new BeanHandler<Customer>(Customer.class), cid); } catch(SQLException e) { throw new RuntimeException(e); } } /** * 编辑客户 * @param c */ public void edit(Customer c) { try { String sql = "update t_customer set cname=?,gender=?,birthday=?," + "cellphone=?,email=?,description=? where cid=?"; Object[] params = {c.getCname(), c.getGender(), c.getBirthday(), c.getCellphone(), c.getEmail(), c.getDescription(), c.getCid()}; qr.update(sql, params); } catch(SQLException e) { throw new RuntimeException(e); } } /** * 多条件组合查询 * @param criteria * @return */ public List<Customer> query(Customer criteria) { try { /* * 1. 给出sql模板 * 2. 给出参数 * 3. 调用query方法,使用结果集处理器:BeanListHandler */ /* * 一、 给出sql模板 * 二、 给出参数! */ /* * 1. 给出一个sql语句前半部 */ StringBuilder sql = new StringBuilder("select * from t_customer where 1=1"); /* * 2. 判断条件,完成向sql中追加where子句 */ /* * 3. 创建一个ArrayList,用来装载参数值 */ List<Object> params = new ArrayList<Object>(); String cname = criteria.getCname(); if(cname != null && !cname.trim().isEmpty()) { sql.append(" and cname like ?"); params.add("%" + cname + "%"); } String gender = criteria.getGender(); if(gender != null && !gender.trim().isEmpty()) { sql.append(" and gender=?"); params.add(gender); } String cellphone = criteria.getCellphone(); if(cellphone != null && !cellphone.trim().isEmpty()) { sql.append(" and cellphone like ?"); params.add("%" + cellphone + "%"); } String email = criteria.getEmail(); if(email != null && !email.trim().isEmpty()) { sql.append(" and email like ?"); params.add("%" + email + "%"); } /* * 三、执行query */ return qr.query(sql.toString(), new BeanListHandler<Customer>(Customer.class), params.toArray()); } catch(SQLException e) { throw new RuntimeException(e); } } }

 

jsp:添加:

 

<form action="<c:url value='/CustomerServlet'/>" method="post">
<!-- 向servlet传递一个名为method的参数,其值表示要调用servlet的哪一个方法 -->
<input type="hidden" name="method" value="add"/>

编辑:

注意要把cid传过去:

<%--
要调用CustomerServlet#edit
要传递cid
 --%>
<form action="<c:url value='/CustomerServlet'/>" method="post">
    <input type="hidden" name="method" value="edit"/>
    <input type="hidden" name="cid" value="${cstm.cid }"/>

list.jsp

<c:forEach items="${requestScope.cstmList}" var="cstm">
    <tr>
        <td>${cstm.cname }</td>
        <td>${cstm.gender }</td>
        <td>${cstm.birthday }</td>
        <td>${cstm.cellphone }</td>
        <td>${cstm.email }</td>
        <td>${cstm.description }</td>
        <td>
            <a href="<c:url value='/CustomerServlet?method=preEdit&cid=${cstm.cid }'/>">编辑</a>
            <a href="<c:url value='/msg.jsp'/>">删除</a>
        </td>
    </tr>
</c:forEach>

msg.jsp

  <h1 style="color:green;" align="center">${msg }</h1>

query.jsp

<h3 align="center">高级搜索</h3>
<form action="<c:url value='/CustomerServlet'/>" method="post">
    <input type="hidden" name="method" value="query"/>

top.jsp

<body style="text-align: center;">
      <h1>客户关系管理系统</h1>
    <a href="<c:url value='/add.jsp'/>">添加客户</a>  | 
    <a href="<c:url value='/CustomerServlet?method=findAll'/>">查询客户</a> | 
    <a href="<c:url value='/query.jsp'/>">高级搜索</a>  
  </body>

 

posted @ 2015-10-27 16:09  暗夜小精灵~~  阅读(366)  评论(0编辑  收藏  举报