本章只有一个功能就是分页展示客户信息,也是本项目最复杂的功能
一、实现效果
二、实现思路
首先,在第一章项目准备的时候就引入了自定义分页标签和两个utils类,其中Page类就是主要的分页类,他有4个属性,当前页,总记录数,每页的记录数,List对象集合,我们根据这个Page所需要的属性来一步步往下写,且最后添加到model中的也是这个Page对象。
第二点,在页面中可以看到,有4个查询条件,所以我们要为这四个查询条件创建一个pojo类,而Page中还有一个List对象集合,即要创建第二个pojo类,客户对象类。所以我们要创建两个pojo类。
第三点,dao中需要的方法,因为Page对象中有总记录数,所以我们需要一个方法获得所有满足条件的对象总数,当然,List对象集合也需要一个方法,来获取所有满足条件的对象
最后,在Service中我们需要把page给设置好,然后传给controller来存入model中。
三、实现
3.1 创建Pojo类
先创建Customer类:
package cn.zhao.crm.pojo;
import java.util.Date;
/**
* 客户信息类
* @author S1
*
*/
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;
public Long getCust_id() {
return cust_id;
}
public void setCust_id(Long cust_id) {
this.cust_id = cust_id;
}
public String getCust_name() {
return cust_name;
}
public void setCust_name(String cust_name) {
this.cust_name = cust_name;
}
public Long getCust_user_id() {
return cust_user_id;
}
public void setCust_user_id(Long cust_user_id) {
this.cust_user_id = cust_user_id;
}
public Long getCust_create_id() {
return cust_create_id;
}
public void setCust_create_id(Long cust_create_id) {
this.cust_create_id = cust_create_id;
}
public String getCust_source() {
return cust_source;
}
public void setCust_source(String cust_source) {
this.cust_source = cust_source;
}
public String getCust_industry() {
return cust_industry;
}
public void setCust_industry(String cust_industry) {
this.cust_industry = cust_industry;
}
public String getCust_level() {
return cust_level;
}
public void setCust_level(String cust_level) {
this.cust_level = cust_level;
}
public String getCust_linkman() {
return cust_linkman;
}
public void setCust_linkman(String cust_linkman) {
this.cust_linkman = cust_linkman;
}
public String getCust_phone() {
return cust_phone;
}
public void setCust_phone(String cust_phone) {
this.cust_phone = cust_phone;
}
public String getCust_mobile() {
return cust_mobile;
}
public void setCust_mobile(String cust_mobile) {
this.cust_mobile = cust_mobile;
}
public String getCust_zipcode() {
return cust_zipcode;
}
public void setCust_zipcode(String cust_zipcode) {
this.cust_zipcode = cust_zipcode;
}
public String getCust_address() {
return cust_address;
}
public void setCust_address(String cust_address) {
this.cust_address = cust_address;
}
public Date getCust_createtime() {
return cust_createtime;
}
public void setCust_createtime(Date cust_createtime) {
this.cust_createtime = cust_createtime;
}
@Override
public String toString() {
return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_user_id=" + cust_user_id
+ ", cust_create_id=" + cust_create_id + ", cust_source=" + cust_source + ", cust_industry="
+ cust_industry + ", cust_level=" + cust_level + ", cust_linkman=" + cust_linkman + ", cust_phone="
+ cust_phone + ", cust_mobile=" + cust_mobile + ", cust_zipcode=" + cust_zipcode + ", cust_address="
+ cust_address + ", cust_createtime=" + cust_createtime + "]";
}
}
再来创建QueryVo查询条件类:
package cn.zhao.crm.pojo;
/**
* 查询条件类
* @author S1
*
*/
public class QueryVo {
private String custName; //客户名称
private String custSource; //客户来源
private String custIndustry; //所属行业
private String custLevel; //客户级别
// 当前页码数
private Integer page;
// 数据库从哪一条数据开始查
private Integer start = 0;
// 每页显示数据条数
private Integer size = 10;
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustSource() {
return custSource;
}
public void setCustSource(String custSource) {
this.custSource = custSource;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public Integer getPage() {
return page;
}
public void setPage(Integer page) {
this.page = page;
}
public Integer getStart() {
return start;
}
public void setStart(Integer start) {
this.start = start;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
}
注意!在查询条件类中不止有4个查询条件,因为要分页,所以还需要查询开始的数目,每页显示的数目,当前页码
3.2 编写Dao
Dao根据前面的分析,需要两个方法,一是获取所有满足条件的对象数量queryCountByQueryVo,二是获取所有满足条件的对象集合selectCustomerByQueryVo
Dao:
package cn.zhao.crm.mapper;
import java.util.List;
import cn.zhao.crm.pojo.Customer;
import cn.zhao.crm.pojo.QueryVo;
public interface CustomerDao {
//根据查询条件查询总条目
public Integer queryCountByQueryVo(QueryVo vo);
//根据查询条件查询分页查询
public List<Customer> selectCustomerByQueryVo(QueryVo vo);
}
Mapper:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.zhao.crm.mapper.CustomerDao">
<!-- //根据查询条件查询总条目
public Integer queryCountByQueryVo(QueryVo vo);
private String custName; //客户名称
private String custSource; //客户来源
private String custIndustry; //所属行业
private String custLevel; //客户级别
-->
<select id="queryCountByQueryVo" parameterType="QueryVo" resultType="Integer">
select count(1) from customer
<where>
<if test="custName!=null and custName!=''">
cust_name like '%${custName}%'
</if>
<if test="custSource!=null and custSource!=''">
and cust_source = #{custSource}
</if>
<if test="custIndustry!=null and custIndustry!=''">
and cust_industry = #{custIndustry}
</if>
<if test="custLevel!=null and custLevel!=''">
and cust_level = #{custLevel}
</if>
</where>
</select>
<!-- //根据查询条件查询分页查询
public Page<Customer> selectCustomerByQueryVo(QueryVo vo); -->
<select id="selectCustomerByQueryVo" parameterType="QueryVo" resultType="Customer">
select * from customer
<where>
<if test="custName!=null and custName!=''">
cust_name like '%${custName}%'
</if>
<if test="custSource!=null and custSource!=''">
and cust_source = #{custSource}
</if>
<if test="custIndustry!=null and custIndustry!=''">
and cust_industry = #{custIndustry}
</if>
<if test="custLevel!=null and custLevel!=''">
and cust_level = #{custLevel}
</if>
</where>
limit #{start},#{size}
</select>
</mapper>
注意!在Mapper中因为传进来的条件可能有可能没有,所以要进行判断!
3.3 编写Service
Service中,需要判断传进的条件,并且调用Dao方法,存入Page对象中,并设置vo对象的一系列属性
Service实现类(接口就不写在博客中了,自己写一个就好了):
package cn.zhao.crm.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zhao.crm.mapper.CustomerDao;
import cn.zhao.crm.pojo.Customer;
import cn.zhao.crm.pojo.QueryVo;
import cn.zhao.crm.utils.Page;
@Service
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerDao customerDao;
@Override
public Page<Customer> queryCustomerByQueryVo(QueryVo vo) {
//创建Page对象
Page<Customer> page = new Page<>();
//设置每页数,注意!
vo.setSize(10);
page.setSize(vo.getSize());
//处理传来的客户名称数据,因为是自己填写的,所以有可能带有空格,把空格去除
if(vo != null) {
//判断当前页是否存在
if(vo.getPage()!=null) {
page.setPage(vo.getPage());
//设置起始页
vo.setStart((vo.getPage()-1)*vo.getSize());
}
if( vo.getCustName()!=null && !vo.getCustName().trim().equals("")) {
vo.setCustName(vo.getCustName().trim());
}
//向page对象保存结果集
page.setRows(customerDao.selectCustomerByQueryVo(vo));
//将count总条目存入page
page.setTotal(customerDao.queryCountByQueryVo(vo));
}
return page;
}
}
注意:page对象的Size属性和vo对象的Size属性都要设置,且Page对象除了page不用特意设置其他都必须设置
3.4 编写controller
只需要把page对象保存到域中即可
package cn.zhao.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 cn.zhao.crm.pojo.BaseDict;
import cn.zhao.crm.pojo.Customer;
import cn.zhao.crm.pojo.QueryVo;
import cn.zhao.crm.service.BaseDictService;
import cn.zhao.crm.service.CustomerService;
import cn.zhao.crm.utils.Page;
/**
* 客户管理
* @author S1
*
*/
@Controller
public class CustomerController {
@Autowired
private BaseDictService baseDictService;
@Autowired
private CustomerService customerService;
@Value("${TypeCode.fromType}")
private String FromType;
@Value("${TypeCode.industryType}")
private String IndustryType;
@Value("${TypeCode.levelType}")
private String LevelType;
@RequestMapping("/customer/list")
public String list(Model model,QueryVo vo) {
//一进入页面即调用方法,为select下拉框添加数据
List<BaseDict> fromType = baseDictService.selectBaseDictByTypeCode(FromType);
List<BaseDict> industryType = baseDictService.selectBaseDictByTypeCode(IndustryType);
List<BaseDict> levelType = baseDictService.selectBaseDictByTypeCode(LevelType);
//添加域中
model.addAttribute("fromType", fromType);
model.addAttribute("industryType", industryType);
model.addAttribute("levelType", levelType);
Page<Customer> page = customerService.queryCustomerByQueryVo(vo);
model.addAttribute("page", page);
//数据回显
model.addAttribute("custName", vo.getCustName());
model.addAttribute("custSource", vo.getCustSource());
model.addAttribute("custIndustry", vo.getCustIndustry());
model.addAttribute("custLevel", vo.getCustLevel());
return "customer";
}
}
这样就完成了分页显示信息