SpringMVC_CRM系统_页面加载数据4

现在我们开始处理页面的数据显示

  1. 先填充这三个下拉框中的数据


    SpringMVC_CRM系统_页面加载数据4_第1张图片
  2. 新建service层
public interface BaseDictService {
    public List queryBaseDictByDictTypeCode(String dictTypeCode) ;
}
@Service
public class BaseDictServiceImpl implements BaseDictService{
    @Autowired
    BaseDictDao baseDictDao;

    public List queryBaseDictByDictTypeCode(String dictTypeCode) {
        return baseDictDao.queryBaseDictByDictTypeCode(dictTypeCode);
    }
}

  1. 在CustomerController中调用service
@Controller
public class CustomerController {
    @Autowired
    BaseDictService baseDictService;
    @RequestMapping("/list")
    public String customer(Model model){
        // 客户来源
        List fromType = this.baseDictService.queryBaseDictByDictTypeCode("002");
        // 所属行业
        List industryType = this.baseDictService.queryBaseDictByDictTypeCode("001");
        // 客户级别
        List levelType = this.baseDictService.queryBaseDictByDictTypeCode("006");

        // 把前端页面需要显示的数据放到模型中
        model.addAttribute("fromType", fromType);
        model.addAttribute("industryType", industryType);
        model.addAttribute("levelType", levelType);


        return "customer";
    }
}

  1. 调用 http://localhost:8080/customer,可以看到页面上的下拉框有数据了

    SpringMVC_CRM系统_页面加载数据4_第2张图片

  2. 接下来我们根据筛选条件来查询客户信息
    分析页面元素,得出要是用的sql语句:

SELECT
    a.cust_id,
    a.cust_name,
    a.cust_user_id,
    a.cust_create_id,
    b.dict_item_name cust_source,
    c.dict_item_name cust_industry,
    d.dict_item_name cust_level,
    a.cust_linkman,
    a.cust_phone,
    a.cust_mobile,
    a.cust_zipcode,
    a.cust_address,
    a.cust_createtime
FROM
    customer a
LEFT JOIN base_dict b ON a.cust_source = b.dict_id
LEFT JOIN base_dict c ON a.cust_industry = c.dict_id
LEFT JOIN base_dict d ON a.cust_level = d.dict_id
WHERE
    a.cust_name LIKE '%马%'
AND a.cust_source = '6'
AND a.cust_industry = '2'
AND a.cust_level = '22'
LIMIT 0, 10
  1. 创建pojo
public class Customer {

    private Long cust_id;
    private String cust_name;
    private Long cust_user_id;
    private Long cust_create_id;
    private String cust_source;
    private String cust_industry;
    private String cust_level;
    private String cust_linkman;
    private String cust_phone;
    private String cust_mobile;
    private String cust_zipcode;
    private String cust_address;
    private Date cust_createtime;
get/set。。。。。。
}

  1. 创建queryVo
public class QueryVo {

    private String custName;
    private String custSource;
    private String custIndustry;
    private String custLevel;

    // 当前页码数
    private Integer page = 1;
    // 数据库从哪一条数据开始查
    private Integer start;
    // 每页显示数据条数
    private Integer rows = 10;
get/set。。。。。。
}
  1. 创建CustomerDao
package com.spring.crm.dao;

import com.spring.crm.pojo.Customer;
import com.spring.crm.vo.QueryVo;

import java.util.List;
@Repository
public interface CustomerDao {

    /**
     * 根据queryVo分页查询数据
     *
     * @param queryVo
     * @return
     */
    List queryCustomerByQueryVo(QueryVo queryVo);

    /**
     * 根据queryVo查询数据条数
     *
     * @param queryVo
     * @return
     */
    int queryCountByQueryVo(QueryVo queryVo);

}

  1. 同目录下创建CustomerDao.xml



    
        
            
                AND a.cust_name LIKE '%${custName}%'
            
            
                AND a.cust_source = #{custSource}
            
            
                AND a.cust_industry = #{custIndustry}
            
            
                AND a.cust_level = #{custLevel}
            
        
    

    
    

    
    

  1. 创建service
package com.spring.crm.service;

import com.spring.crm.common.Page;
import com.spring.crm.pojo.Customer;
import com.spring.crm.vo.QueryVo;

public interface CustomerService {
    /**
     * 根据条件分页查询客户
     *
     * @param queryVo
     * @return
     */
    Page queryCustomerByQueryVo(QueryVo queryVo);
}
package com.spring.crm.service;

import com.spring.crm.common.Page;
import com.spring.crm.dao.CustomerDao;
import com.spring.crm.pojo.Customer;
import com.spring.crm.vo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    public Page queryCustomerByQueryVo(QueryVo queryVo) {
        // 设置查询条件,从哪一条数据开始查
        queryVo.setStart((queryVo.getPage() - 1) * queryVo.getRows());

        // 查询数据结果集
        List list = this.customerDao.queryCustomerByQueryVo(queryVo);
        // 查询到的数据总条数
        int total = this.customerDao.queryCountByQueryVo(queryVo);

        // 封装返回的page对象
        Page page = new Page(total, queryVo.getPage(), queryVo.getRows(), list);

        return page;
    }
}
  1. 修改Controller
    @RequestMapping("/customer/list")
    public String queryCustomerList(QueryVo queryVo, Model model) {

        try {
            // 解决get请求乱码问题
            if (StringUtils.isNotBlank(queryVo.getCustName())) {
                queryVo.setCustName(new String(queryVo.getCustName().getBytes("ISO-8859-1"), "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 客户来源
        List fromType = this.baseDictService.queryBaseDictByDictTypeCode("002");
        // 所属行业
        List industryType = this.baseDictService.queryBaseDictByDictTypeCode("001");
        // 客户级别
        List levelType = this.baseDictService.queryBaseDictByDictTypeCode("006");

        // 把前端页面需要显示的数据放到模型中
        model.addAttribute("fromType", fromType);
        model.addAttribute("industryType", industryType);
        model.addAttribute("levelType", levelType);

        // 分页查询数据
        Page page = this.customerService.queryCustomerByQueryVo(queryVo);
        // 把分页查询的结果放到模型中
        model.addAttribute("page", page);

        // 数据回显
        model.addAttribute("custName", queryVo.getCustName());
        model.addAttribute("custSource", queryVo.getCustSource());
        model.addAttribute("custIndustry", queryVo.getCustIndustry());
        model.addAttribute("custLevel", queryVo.getCustLevel());

        return "customer";
    }
  1. 访问http://localhost:8080/customer/list,可以看到页面有数据了,而且可以根据筛选条件查询客户信息
    SpringMVC_CRM系统_页面加载数据4_第3张图片

总结

本章记录了如果显示页面数据和查询数据,下一章讲解数据修改。

本系列源码存放在github上 源码

你可能感兴趣的:(SpringMVC_CRM系统_页面加载数据4)