3. 客户管理
3.1 查询所有客户
3.1.1 查询所有
需求:查询所有客户
步骤:
步骤1:入口
步骤2:编写Controller
步骤3:编写Service 接口、实现类
步骤4:修改 list.jsp展示数据
步骤1:入口
步骤2:编写Controller
package com.czxy.crm.controller; import com.czxy.crm.domain.Customer; import com.czxy.crm.service.CustomerService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import java.util.List; /** * @author 桐叔 * @email liangtong@itcast.cn */ @Controller @RequestMapping("/customer") public class CustomerController { @Resource private CustomerService customerService; @RequestMapping("/findAll") public String findAll( Model model){ List<Customer> allCustomer = customerService.findAll(); model.addAttribute("allCustomer", allCustomer); return "customer/list"; } }
步骤3:编写Service 接口、实现类
- 接口
package com.czxy.crm.service; import com.czxy.crm.domain.Customer; import java.util.List; /** * @author 桐叔 * @email liangtong@itcast.cn */ public interface CustomerService { /** * 查询所有 * @return */ List<Customer> findAll(); }
实现类
package com.czxy.crm.service.impl; import com.czxy.crm.domain.BaseDict; import com.czxy.crm.domain.Customer; import com.czxy.crm.mapper.BaseDictMapper; import com.czxy.crm.mapper.CustomerMapper; import com.czxy.crm.service.CustomerService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.util.List; /** * @author 桐叔 * @email liangtong@itcast.cn */ @Service @Transactional public class CustomerServiceImpl implements CustomerService { @Resource private CustomerMapper customerMapper; @Resource private BaseDictMapper baseDictMapper; @Override public List<Customer> findAll() { // 查询所有 List<Customer> list = customerMapper.selectAll(); // 关联查询 list.forEach(customer -> { // 客户来源 BaseDict custSourceBaseDict = baseDictMapper.selectByPrimaryKey(customer.getCustSource()); customer.setCustSourceBaseDict(custSourceBaseDict); //客户行业 BaseDict custIndustryBaseDict = baseDictMapper.selectByPrimaryKey(customer.getCustIndustry()); customer.setCustIndustryBaseDict(custIndustryBaseDict); //客户级别 BaseDict custLevelBaseDict = baseDictMapper.selectByPrimaryKey(customer.getCustLevel()); customer.setCustLevelBaseDict(custLevelBaseDict); }); return list; } }
步骤4:修改 list.jsp展示数据
<c:forEach items="${allCustomer}" var="customer"> <TR style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none"> <TD>${customer.custName }</TD> <TD>${customer.custLevelBaseDict.dictItemName }</TD> <TD>${customer.custSourceBaseDict.dictItemName }</TD> <TD>${customer.custIndustryBaseDict.dictItemName }</TD> <TD>${customer.custAddress }</TD> <TD>${customer.custPhone }</TD> <TD> <a href="${pageContext.request.contextPath }/customer/editUI.action?custId=${customer.custId}">修改</a> <a href="${pageContext.request.contextPath }/customer/delete.action?custId=${customer.custId}" οnclick="return confirm('您确定要删除【${customer.custName }】吗?')">删除</a> </TD> </TR> </c:forEach>
3.1.2 条件查询
需求:
步骤
步骤1:入口,确定查询表单
步骤2:创建CustomerVo,用于封装查询条件
步骤3:修改controller,获得查询条件
步骤4:修改service,使用查询条件
步骤1:入口,确定查询表单
步骤2:创建CustomerVo,用于封装查询条件
///
package com.czxy.crm.vo; /** * @author 桐叔 * @email liangtong@itcast.cn */ public class CustomerVo { private String custName; public String getCustName() { return custName; } public void setCustName(String custName) { this.custName = custName; } }
步骤3:修改controller,获得查询条件
package com.czxy.crm.controller; import com.czxy.crm.domain.Customer; import com.czxy.crm.service.CustomerService; import com.czxy.crm.vo.CustomerVo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import java.util.List; /** * @author 桐叔 * @email liangtong@itcast.cn */ @Controller @RequestMapping("/customer") public class CustomerController { @Resource private CustomerService customerService; @RequestMapping("/findAll") public String findAll(CustomerVo customerVo, Model model){ List<Customer> allCustomer = customerService.findAll(customerVo); model.addAttribute("allCustomer", allCustomer); return "customer/list"; } }
步骤4:修改service,使用查询条件
- 接口
- 实现类
package com.czxy.crm.service.impl; import com.czxy.crm.domain.BaseDict; import com.czxy.crm.domain.Customer; import com.czxy.crm.mapper.BaseDictMapper; import com.czxy.crm.mapper.CustomerMapper; import com.czxy.crm.service.CustomerService; import com.czxy.crm.vo.CustomerVo; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import tk.mybatis.mapper.entity.Example; import javax.annotation.Resource; import java.util.List; /** * @author 桐叔 * @email liangtong@itcast.cn */ @Service @Transactional public class CustomerServiceImpl implements CustomerService { @Resource private CustomerMapper customerMapper; @Resource private BaseDictMapper baseDictMapper; @Override public List<Customer> findAll(CustomerVo customerVo) { // 1 条件查询 Example example = new Example(Customer.class); Example.Criteria criteria = example.createCriteria(); if(StringUtils.isNotBlank(customerVo.getCustName())) { criteria.andLike("custName", "%"+customerVo.getCustName()+"%"); } // 2 分页查询 // 3 查询所有 List<Customer> list = customerMapper.selectByExample(example); // 4 关联查询 list.forEach(customer -> { // 客户来源 BaseDict custSourceBaseDict = baseDictMapper.selectByPrimaryKey(customer.getCustSource()); customer.setCustSourceBaseDict(custSourceBaseDict); //客户行业 BaseDict custIndustryBaseDict = baseDictMapper.selectByPrimaryKey(customer.getCustIndustry()); customer.setCustIndustryBaseDict(custIndustryBaseDict); //客户级别 BaseDict custLevelBaseDict = baseDictMapper.selectByPrimaryKey(customer.getCustLevel()); customer.setCustLevelBaseDict(custLevelBaseDict); }); // 5 封装分页 return list; } }
3.1.3 分页查询
需求
步骤:
步骤1:修改CustomerVo,获得分页参数
步骤2:修改controller,返回pageInfo
步骤3:修改service,封装PageInfo
步骤4:修改jsp,展示列表数据
步骤5:修改jsp,展示分页条
步骤1:修改CustomerVo,获得分页参数
package com.czxy.crm.vo; /** * @author 桐叔 * @email liangtong@itcast.cn */ public class CustomerVo { private String custName; private Integer pageNum = 1; private Integer pageSize = 2; // getter和setter }
步骤2:修改controller,返回pageInfo
步骤3:修改service,封装PageInfo
接口
实现类
/
package com.czxy.crm.service.impl; import com.czxy.crm.domain.BaseDict; import com.czxy.crm.domain.Customer; import com.czxy.crm.mapper.BaseDictMapper; import com.czxy.crm.mapper.CustomerMapper; import com.czxy.crm.service.CustomerService; import com.czxy.crm.vo.CustomerVo; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import tk.mybatis.mapper.entity.Example; import javax.annotation.Resource; import java.util.List; /** * @author 桐叔 * @email liangtong@itcast.cn */ @Service @Transactional public class CustomerServiceImpl implements CustomerService { @Resource private CustomerMapper customerMapper; @Resource private BaseDictMapper baseDictMapper; @Override public PageInfo<Customer> findAll(CustomerVo customerVo) { // 1 条件查询 Example example = new Example(Customer.class); Example.Criteria criteria = example.createCriteria(); if(StringUtils.isNotBlank(customerVo.getCustName())) { criteria.andLike("custName", "%"+customerVo.getCustName()+"%"); } // 2 分页查询 PageHelper.startPage(customerVo.getPageNum(),customerVo.getPageSize()); // 3 查询所有 List<Customer> list = customerMapper.selectByExample(example); // 4 关联查询 list.forEach(customer -> { // 客户来源 BaseDict custSourceBaseDict = baseDictMapper.selectByPrimaryKey(customer.getCustSource()); customer.setCustSourceBaseDict(custSourceBaseDict); //客户行业 BaseDict custIndustryBaseDict = baseDictMapper.selectByPrimaryKey(customer.getCustIndustry()); customer.setCustIndustryBaseDict(custIndustryBaseDict); //客户级别 BaseDict custLevelBaseDict = baseDictMapper.selectByPrimaryKey(customer.getCustLevel()); customer.setCustLevelBaseDict(custLevelBaseDict); }); // 5 封装分页 return new PageInfo<>(list); } }
步骤4:修改jsp,展示列表数据
步骤5:修改jsp,展示分页条
查询表单
分页条
当前第[<B>${pageInfo.pageNum}</B>]页,共[<B>${pageInfo.total}</B>]条
,每页显示
//
<select οnchange="change(this)"> <option value="1" ${pageInfo.pageSize == 1 ? 'selected' : ''}>1</option> <option value="2" ${pageInfo.pageSize == 2 ? 'selected' : ''}>2</option> <option value="3" ${pageInfo.pageSize == 3 ? 'selected' : ''}>3</option> <option value="5" ${pageInfo.pageSize == 5 ? 'selected' : ''}>5</option> <option value="10" ${pageInfo.pageSize == 10 ? 'selected' : ''}>10</option> </select> 条 <c:if test="${pageInfo.pageNum > 1}"> [<a href="javascript:void(0)" οnclick="page(1)">首页</a>] [<a href="javascript:void(0)" οnclick="page(${pageInfo.pageNum - 1})">上一页</a>] </c:if> <c:forEach begin="1" end="${pageInfo.pages}" var="num"> <b><a href="javascript:void(0)" οnclick="page(${num})">${num}</a></b> </c:forEach> <c:if test="${pageInfo.pageNum < pageInfo.pages}"> [<a href="javascript:void(0)" οnclick="page(${pageInfo.pageNum + 1})">下一页</a>] [<a href="javascript:void(0)" οnclick="page(${pageInfo.pages})">尾页</a>] </c:if> 到 <input type="number" style="width: 35px;" value="${customerVo.pageNum}" id="goId"/> 页 <input type="button" value="Go" οnclick="page(goId.value)"/>
js函数
function change(obj) { // 重置,如果修改了pageSize,从1开始 pageNumId.value = 1 // 修改隐藏字段 pageSize pageSizeId.value = obj.value // 提交表单 customerFormId.submit() } function page(pageNum) { // 修改pageNum的值 pageNumId.value = pageNum // 提交表单 customerFormId.submit() }
3.2 添加客户
3.2.1 需求
3.2.2 显示表单
步骤:
- 步骤1:入口
- 步骤2:修改CustomerController,用于显示 customer/add.jsp 页面
- 查询数据字典:客户来源(typeCode=002)
- 查询数据字典:客户行业(typeCode=001)
- 查询数据字典:客户级别(typeCode=006)
- 步骤3:编写BaseDictService,通过typeCode查询所有数据字典信息。
- 步骤4:修改add.jsp,显示字典相关信息
- 步骤1:入口
- 步骤2:修改CustomerController,用于显示 customer/add.jsp 页面
- 查询数据字典:客户来源(typeCode=002)
- 查询数据字典:客户行业(typeCode=001)
- 查询数据字典:客户级别(typeCode=006)
package com.czxy.crm.controller; import com.czxy.crm.domain.BaseDict; import com.czxy.crm.domain.Customer; import com.czxy.crm.service.BaseDictService; import com.czxy.crm.service.CustomerService; import com.czxy.crm.vo.CustomerVo; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.annotation.Resource; import java.util.List; /** * @author 桐叔 * @email liangtong@itcast.cn */ @Controller @RequestMapping("/customer") public class CustomerController { @Resource private CustomerService customerService; @Resource private BaseDictService baseDictService; /** * 添加页面 * @return */ @RequestMapping("/addUI") public String addUI(Model model) { // 客户行业 (typeCode=001) List<BaseDict> custIndustryBaseDictList = baseDictService.selectAllByTypeCode("001"); model.addAttribute("custIndustryBaseDictList",custIndustryBaseDictList); // 客户来源(typeCode=002) List<BaseDict> custSourceBaseDictList = baseDictService.selectAllByTypeCode("002"); model.addAttribute("custSourceBaseDictList",custSourceBaseDictList); // 客户级别(typeCode=006) List<BaseDict> custLevelBaseDictList = baseDictService.selectAllByTypeCode("006"); model.addAttribute("custLevelBaseDictList",custLevelBaseDictList); return "customer/add"; } }
步骤3:编写BaseDictService,通过typeCode查询所有数据字典信息。
接口
package com.czxy.crm.service; import com.czxy.crm.domain.BaseDict; import java.util.List; /** * @author 桐叔 * @email liangtong@itcast.cn */ public interface BaseDictService { /** * 通过类别代码查询数据字典信息 * @param dictTypeCode * @return */ List<BaseDict> selectAllByTypeCode(String dictTypeCode); }
实现类
///
package com.czxy.crm.service.impl; import com.czxy.crm.domain.BaseDict; import com.czxy.crm.mapper.BaseDictMapper; import com.czxy.crm.service.BaseDictService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import tk.mybatis.mapper.entity.Example; import javax.annotation.Resource; import java.util.List; /** * @author 桐叔 * @email liangtong@itcast.cn */ @Service @Transactional public class BaseDictServiceImpl implements BaseDictService { @Resource private BaseDictMapper baseDictMapper; @Override public List<BaseDict> selectAllByTypeCode(String dictTypeCode) { // 条件 Example example = Example.builder(BaseDict.class).build(); example.createCriteria().andEqualTo("dictTypeCode",dictTypeCode); // 查询 return baseDictMapper.selectByExample(example); } }
步骤4:修改add.jsp,显示字典相关信息
<td>所属行业 :</td> <td> <select name="custIndustry" class=textbox style="WIDTH: 180px"> <option value="">---请选择---</option> <c:forEach items="${custIndustryBaseDictList}" var="industry"> <option value="${industry.dictId}">${industry.dictItemName}</option> </c:forEach> </select> </td> <td>信息来源 :</td> <td> <select name="custSource" class=textbox style="WIDTH: 180px"> <option value="non">---请选择---</option> <c:forEach items="${custSourceBaseDictList}" var="source"> <option value="${source.dictId}">${source.dictItemName}</option> </c:forEach> </select> </td> <td>客户级别:</td> <td> <select name="custLevel" class=textbox style="WIDTH: 180px"> <option value="non">---请选择---</option> <c:forEach items="${custLevelBaseDictList}" var="level"> <option value="${level.dictId}">${level.dictItemName}</option> </c:forEach> </select> </td>
3.2.3 添加
步骤:
步骤1:修改add.jsp页面,确定表单提交路径 /customer/add.action
步骤2:修改CustomerController,添加add方法,并处理成功和失败。
步骤3:修改CustomerService,添加add方法
步骤4:修改add.jsp,显示错误信息
步骤1:修改add.jsp页面,确定表单提交路径 /customer/add.action
步骤2:修改CustomerController,添加add方法,并处理成功和失败。
/
/** * 添加 * @param customer * @return */ @RequestMapping("/add") public String add(Customer customer, Model model) { try { // 添加 boolean result = customerService.add(customer); if(result) { // 成功 return "redirect:/customer/findAll.action"; } else { // 失败 model.addAttribute("error","添加客户失败"); return "customer/add"; } } catch (Exception e) { e.printStackTrace(); model.addAttribute("error",e.getMessage()); return "customer/add"; } }
步骤3:修改CustomerService,添加add方法
接口
/** * 添加客户 * @param customer * @return */ boolean add(Customer customer);
实现类
@Override public boolean add(Customer customer) { int insert = customerMapper.insertSelective(customer); return insert == 1; }
步骤4:修改add.jsp,显示错误信息
<%-- 显示错误信息--%> <span style="color:red">${error}</span>
3.3 修改客户
3.3.1 需求
3.3.2 显示表单,回显数据
步骤:
步骤1:入口 /customer/editUI.action?custId=1
步骤2:修改CustomerController,用于显示 customer/edit.jsp 页面
查询数据字典:客户来源(typeCode=002)
查询数据字典:客户行业(typeCode=001)
查询数据字典:客户级别(typeCode=006)
通过custId查询客户详情
步骤3:编写CustomerService,查询客户详情。
步骤4:修改edit.jsp,显示字典相关信息,回显客户信息。
步骤1:入口 /customer/editUI.action?custId=1
步骤2:修改CustomerController,用于显示 customer/edit.jsp 页面
查询数据字典:客户来源(typeCode=002)
查询数据字典:客户行业(typeCode=001)
查询数据字典:客户级别(typeCode=006)
通过custId查询客户详情
/** * 修改页面 * @return */ @RequestMapping("/editUI") public String editUI(Long custId, Model model) { // 客户行业 (typeCode=001) List<BaseDict> custIndustryBaseDictList = baseDictService.selectAllByTypeCode("001"); model.addAttribute("custIndustryBaseDictList",custIndustryBaseDictList); // 客户来源(typeCode=002) List<BaseDict> custSourceBaseDictList = baseDictService.selectAllByTypeCode("002"); model.addAttribute("custSourceBaseDictList",custSourceBaseDictList); // 客户级别(typeCode=006) List<BaseDict> custLevelBaseDictList = baseDictService.selectAllByTypeCode("006"); model.addAttribute("custLevelBaseDictList",custLevelBaseDictList); // 查询客户项 Customer customer = customerService.selectById(custId); model.addAttribute("customer",customer); return "customer/edit"; }
步骤3:编写CustomerService,查询客户详情。
- 接口
/** * 通过id查询详情 * @param custId * @return */ Customer selectById(Long custId);
实现类
@Override public Customer selectById(Long custId) { return customerMapper.selectByPrimaryKey(custId); }
步骤4:修改edit.jsp,显示字典相关信息,回显客户信息。
<select name="custIndustry" class=textbox style="WIDTH: 180px"> <option value="">---请选择---</option> <c:forEach items="${custIndustryBaseDictList}" var="industry"> <option value="${industry.dictId}" ${customer.custIndustry== industry.dictId?"selected":""}>${industry.dictItemName}</option> </c:forEach> </select> <td>信息来源 :</td> <td> <select name="custSource" class=textbox style="WIDTH: 180px"> <option value="non">---请选择---</option> <c:forEach items="${custSourceBaseDictList}" var="source"> <option value="${source.dictId}" ${customer.custSource== source.dictId?"selected":""}>${source.dictItemName}</option> </c:forEach> </select> </td> <td>客户级别:</td> <td> <select name="custLevel" class=textbox style="WIDTH: 180px"> <option value="non">---请选择---</option> <c:forEach items="${custLevelBaseDictList}" var="level"> <option value="${level.dictId}" ${customer.custLevel==level.dictId?"selected":""}>${level.dictItemName}</option> </c:forEach> </select> </td>
3.3.3 修改
步骤:
步骤1:修改edit.jsp页面,确定表单提交路径 /customer/edit.action
步骤2:修改CustomerController,添加edit方法,并处理成功和失败。
步骤3:修改CustomerService,添加edit方法
步骤4:修改edit.jsp,显示错误信息
步骤1:修改edit.jsp页面,确定表单提交路径 /customer/edit.action
步骤2:修改CustomerController,添加edit方法,并处理成功和失败。
/
/** * 修改 * @param customer * @return */ @RequestMapping("/edit") public String edit(Customer customer, Model model) { try { // 修改 boolean result = customerService.update(customer); if(result) { // 成功 return "redirect:/customer/findAll.action"; } else { // 失败 model.addAttribute("error","修改客户失败"); return "customer/edit"; } } catch (Exception e) { e.printStackTrace(); model.addAttribute("error",e.getMessage()); return "customer/edit"; }
步骤3:修改CustomerService,添加edit方法
- 接口
/** * 修改 * @param customer * @return */ boolean update(Customer customer);
实现类
@Override public boolean update(Customer customer) { int update = customerMapper.updateByPrimaryKeySelective(customer); return update == 1; }
步骤4:修改edit.jsp,显示错误信息
3.4 删除客户
步骤:
步骤1:入口 customer/delete.action?custId=1
步骤2:修改CustomerController,添加delete方法,删除成功重定向到列表页。
步骤3:修改CustomerService,添加delete方法
步骤1:入口 customer/delete.action?custId=1
<a href="${pageContext.request.contextPath }/customer/delete.action?custId=${customer.custId}" οnclick="return confirm('您确定要删除【${customer.custName }】吗?')">删除</a>
步骤2:修改CustomerController,添加delete方法,删除成功重定向到列表页。
/** * 通过id删除客户 * @param custId * @return */ @RequestMapping("/delete") public String delete(Long custId) { // 删除 customerService.deleteById(custId); // 重定向列表页 return "redirect:/customer/findAll.action"; }
步骤3:修改CustomerService,添加delete方法
- 接口
/** * 通过id删除 * @param custId */ void deleteById(Long custId);
实现类
@Override public void deleteById(Long custId) { customerMapper.deleteByPrimaryKey(custId); }