实现删除客户信息这个功能也是非常简单的,只需要在客户列表显示页面中,对要删除的客户点击"删除"按钮,如下图所示。
这时应该要弹出一个友好提示窗口,提示我们是否真的需要删除该客户,如下图所示。
点击"确定"按钮后方可删除客户,最后还要刷新客户列表页面。如此一来,点击"删除"按钮是不是应该触发下面这样一个函数呢?
接下来,我们就要根据前台发起的请求开发后台逻辑了。
实现dao层开发
首先,在客户信息持久化接口(CustomerMapper.java)中声明一个删除客户的方法。
package com.meimeixia.crm.mapper;
import java.util.List;
import com.meimeixia.crm.pojo.Customer;
import com.meimeixia.crm.pojo.QueryVo;
/**
* 客户信息持久化接口
* @author liayun
*
*/
public interface CustomerMapper {
/**
* 根据查询条件分页查询客户列表
* @param vo
* @return
*/
List<Customer> getCustomerByQueryVo(QueryVo vo);
/**
* 根据查询条件查询总记录数
* @param vo
* @return
*/
Integer getCountByQueryVo(QueryVo vo);
/**
* 根据id查询客户信息
* @param id
* @return
*/
Customer getCustomerById(Integer id);
/**
* 更新客户信息
* @param customer
*/
void updateCustomer(Customer customer);
/**
* 删除客户
* @param id
*/
void deleteCustomer(Integer id);
}
然后,在CustomerMapper.xml文件中编写对应的delete删除语句。
<delete id="deleteCustomer" parameterType="int">
DELETE FROM `customer` WHERE `cust_id` = #{cust_id}
</delete>
实现service层开发
首先,在客户信息业务逻辑接口(CustomerService.java)中声明一个删除客户的方法。
package com.meimeixia.crm.service;
import com.meimeixia.crm.pojo.Customer;
import com.meimeixia.crm.pojo.QueryVo;
import com.meimeixia.crm.utils.Page;
/**
* 客户信息业务逻辑接口
* @author liayun
*
*/
public interface CustomerService {
/**
* 根据查询条件分页查询客户列表
* @param vo
* @return 包装的分页数据
*/
Page<Customer> getCustomerByQueryVo(QueryVo vo);
/**
* 根据id查询客户信息
* @param id
* @return
*/
Customer getCustomerById(Integer id);
/**
* 更新客户信息
* @param customer
*/
void updateCustomer(Customer customer);
/**
* 删除客户
* @param id
*/
void deleteCustomer(Integer id);
}
然后,在以上接口的实现类(BaseDictServiceImpl.java)中去实现以上方法。
package com.meimeixia.crm.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.meimeixia.crm.mapper.CustomerMapper;
import com.meimeixia.crm.pojo.Customer;
import com.meimeixia.crm.pojo.QueryVo;
import com.meimeixia.crm.service.CustomerService;
import com.meimeixia.crm.utils.Page;
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerMapper customerMapper;
@Override
public Page<Customer> getCustomerByQueryVo(QueryVo vo) {
//计算分页查询从哪条记录开始
vo.setStart((vo.getPage() - 1) * vo.getRows());
//查询总记录数
Integer total = customerMapper.getCountByQueryVo(vo);
//查询每页的记录(数据)列表
List<Customer> list = customerMapper.getCustomerByQueryVo(vo);
//包装一个分页数据模型
Page<Customer> page = new Page<Customer>(total, vo.getPage(), vo.getRows(), list);
return page;
}
@Override
public Customer getCustomerById(Integer id) {
return customerMapper.getCustomerById(id);
}
@Override
public void updateCustomer(Customer customer) {
customerMapper.updateCustomer(customer);
}
@Override
public void deleteCustomer(Integer id) {
customerMapper.deleteCustomer(id);
}
}
实现表现层开发
在CustomerController类中编写一个如下delete()方法。
package com.meimeixia.crm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.meimeixia.crm.pojo.BaseDict;
import com.meimeixia.crm.pojo.Customer;
import com.meimeixia.crm.pojo.QueryVo;
import com.meimeixia.crm.service.BaseDictService;
import com.meimeixia.crm.service.CustomerService;
import com.meimeixia.crm.utils.Page;
/**
* 客户信息请求处理的Controller
* @author liayun
*
*/
@Controller
@RequestMapping("customer")
public class CustomerController {
@Autowired
private BaseDictService dictService;
@Autowired
private CustomerService customerService;
@Value("${customer_from_type}")
private String customer_from_type;
@Value("${customer_industry_type}")
private String customer_industry_type;
@Value("${customer_level_type}")
private String customer_level_type;
@RequestMapping("list")
public String list(Model model, QueryVo vo) {
//查询客户来源
List<BaseDict> fromType = dictService.getBaseDictByCode(customer_from_type);//"002"
//查询客户行业
List<BaseDict> industryType = dictService.getBaseDictByCode(customer_industry_type);//"001"
//查询客户级别
List<BaseDict> levelType = dictService.getBaseDictByCode(customer_level_type);//"006"
//设置数据模型并返回
model.addAttribute("fromType", fromType);
model.addAttribute("industryType", industryType);
model.addAttribute("levelType", levelType);
//根据查询条件分页查询用户列表
Page<Customer> page = customerService.getCustomerByQueryVo(vo);
//设置分页数据返回
model.addAttribute("page", page);
//返回查询条件
model.addAttribute("vo", vo);
return "customer";
}
/**
* 根据id查询用户信息,并返回json格式的数据
* @param id
* @return
*/
@RequestMapping("edit")
@ResponseBody
public Customer edit(Integer id) {
Customer customer = customerService.getCustomerById(id);
return customer;
}
@RequestMapping("update")
@ResponseBody
public String update(Customer customer) {
String msg = "1";//1即默认修改客户失败
try {
customerService.updateCustomer(customer);
msg = "0";//0:修改客户成功
} catch (Exception e) {
e.printStackTrace();
}
return msg;
}
@RequestMapping("delete")
@ResponseBody
public String delete(Integer id) {
String msg = "1";//1即默认删除客户失败
try {
customerService.deleteCustomer(id);
msg = "0";//0:修改删除成功
} catch (Exception e) {
e.printStackTrace();
}
return msg;
}
}
到这里,就实现删除客户信息的功能了,是不是很简单呢?这样,一个简单的基于SSM框架的crm小项目就完成了!