• 客户关系管理


    package cn.edu.nsu.cstm.servlet;


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


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


    import cn.edu.nsu.cstm.domain.customer;
    import cn.edu.nsu.cstm.service.customerService;
    import cn.itcast.commons.CommonUtils;
    import cn.itcast.servlet.BaseServlet;


    public class customerServlet extends BaseServlet {
    customerService customerService=new customerService();


    public String add(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    customer c=CommonUtils.toBean(request.getParameterMap(), customer.class);//得到封装好的用户
    c.setCid(CommonUtils.uuid());//将用户的id用uuid随机生成的数表似
    customerService.add(c);//通过service添加用户
    request.setAttribute("msg", c.getCname()+"用户注册成功");//保持信息到msg中
    return "f:/msg.jsp";//f代表转发,若是r代表的就是重定向了
    }
    public String findall(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //调用service得到用户的对象
    //保存到request域中
    //转发到list.jsp中
    request.setAttribute("cstmList", customerService.findall());
    return "f:/list.jsp";
    }
    public String preEdit(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //获取cid
    //使用cid得到service的customer对象
    //转发显示到表单中
    String cid =request.getParameter("cid");
    customer cstm=customerService.load(cid);
    request.setAttribute("cstm", cstm);

    return "f:/edit.jsp";
    }
    public String edit(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //封装表单对象到customer中
    //调用service方法完成修改
    //保存信息到request域中
    //转发到msg.jsp中
    customer c=CommonUtils.toBean(request.getParameterMap(), customer.class);
    customerService.edit(c);
    request.setAttribute("msg", c.getCname()+"用户修改成功");

    return "f:/msg.jsp";
    }
    public String delete(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //封装表单对象到customer中
    //调用service方法完成修改
    //保存信息到request域中
    //转发到msg.jsp中
    customer c=CommonUtils.toBean(request.getParameterMap(), customer.class);
    String idString=c.getCname();
    customerService.delete(c);
    request.setAttribute("msg", idString+"用户删除成功");

    return "f:/msg.jsp";
    }
    public String query(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    //封装表单数据到customer对象之中,它有四个属性(cname, gender,cellphone,email)
    //调用service方法
    //保存在request域中
    //转发到list.jsp中
    customer criteria=CommonUtils.toBean(request.getParameterMap(), customer.class);
    List<customer> cstmList=customerService.query(criteria);
    request.setAttribute("cstmList", cstmList);
    return "f:/list.jsp";
    }








    }









    package cn.edu.nsu.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.edu.nsu.cstm.domain.customer;
    import cn.itcast.jdbc.TxQueryRunner;


    public class customerDao {
    private QueryRunner qr = new TxQueryRunner();// 依赖此对象对数据库的使用进行操作
    // 添加客户


    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 (Exception e) {
    throw new RuntimeException();//抛出异常
    }
    }


    public List<customer> findall(){
    try {
    String sql="select* from  t_customer";
    return qr.query(sql, new BeanListHandler<customer>(customer.class));
    } catch (Exception e) {
    throw new RuntimeException(e);
    }
    }


    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 (Exception e) {
    throw new RuntimeException();
    }
    }
    //编辑客户
    public void edit(customer c) {
    try {
    String sql="update t_customer set cname=?,gender=?,birthday=?,cellphone=?,email=?,description=? where cid=?";
    Object[] parmas={c.getCname(), c.getGender(),
    c.getBirthday(), c.getCellphone(), c.getEmail(),
    c.getDescription(),c.getCid()};

    qr.update(sql, parmas);
    } catch (SQLException e) {
    throw new RuntimeException(e);
    }
    }


    public void delete(customer c) {
    String sql="delete from t_customer where cid=?";
    try {
    qr.update(sql, c.getCid());
    } catch (SQLException e) {
    throw new RuntimeException(e);
    }

    }


    public List<customer> quer(customer criteria) {
    //给出一个sql的前半句话
    StringBuilder sql=new StringBuilder("select *from  t_customer where 1=1");
    //判断条件在追加子句
    List<Object> param=new ArrayList<Object>();
    //创建一个Arraylist用来转载参数
    String cname=criteria.getCname();
    if(cname!=null&&!cname.trim().isEmpty()){
    sql.append(" and cname=?");
    param.add(cname);
    }
    String gender=criteria.getGender();
    if(gender!=null&&!gender.trim().isEmpty()){
    sql.append(" and gender=?");
    param.add(gender);
    }
    String cellphone=criteria.getCellphone();
    if(cellphone!=null&&!cellphone.trim().isEmpty()){
    sql.append(" and cellphone=?");
    param.add(cellphone);
    }
    String email=criteria.getEmail();
    if(email!=null&&!email.trim().isEmpty()){
    sql.append(" and email=?");
    param.add(email);
    }
    //给出参数
    try {
    return qr.query(sql.toString(), new BeanListHandler<customer>(customer.class),param.toArray());
    } catch (SQLException e) {
    throw new RuntimeException(e);
    }
    }


    }







    package cn.edu.nsu.cstm.service;


    import java.util.List;


    import cn.edu.nsu.cstm.dao.customerDao;
    import cn.edu.nsu.cstm.domain.customer;


    public class customerService {
    private customerDao customerDao=new customerDao();//依赖此对象得到对象的信息
    public void add(customer c){
    customerDao.add(c);
    }
    public List<customer> findall(){
    return customerDao.findall();
    }
    public customer load(String cid) {

    return customerDao.load(cid);
    }
    public void edit(customer c) {
    customerDao.edit(c);

    }
    public void delete(customer c) {
    customerDao.delete(c);

    }
    public List<customer> query(customer criteria) {


    return customerDao.quer(criteria);
    }
    }

  • 相关阅读:
    移动端开发常见的坑
    javascript的原始类型(primitive type)之间的关系。
    微信小程序学习:开发注意点
    canvas学习(一):线条,图像变换和状态保存
    css模仿微信弹出菜单
    html5 canvas绘制环形进度条,环形渐变色仪表图
    vue学习笔记(三):vue-cli脚手架搭建
    nodejs基础学习
    css3美化radio样式
    基于angular+bower+glup的webapp
  • 原文地址:https://www.cnblogs.com/csnd/p/16675745.html
Copyright © 2020-2023  润新知