联系人管理-查询列表| CRM客户关系管理系统项目实战四(Struts2+Spring+Hibernate)解析+源代码

一、联系人准备工作

1、创建表

CREATE TABLE `cst_linkman` (
  `lkm_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '联系人编号(主键)',
  `lkm_name` varchar(16) DEFAULT NULL COMMENT '联系人姓名',
  `lkm_cust_id` bigint(32) NOT NULL COMMENT '客户id',
  `lkm_gender` char(1) DEFAULT NULL COMMENT '联系人性别',
  `lkm_phone` varchar(16) DEFAULT NULL COMMENT '联系人办公电话',
  `lkm_mobile` varchar(16) DEFAULT NULL COMMENT '联系人手机',
  `lkm_email` varchar(64) DEFAULT NULL COMMENT '联系人邮箱',
  `lkm_qq` varchar(16) DEFAULT NULL COMMENT '联系人qq',
  `lkm_position` varchar(16) DEFAULT NULL COMMENT '联系人职位',
  `lkm_memo` varchar(512) DEFAULT NULL COMMENT '联系人备注',
  PRIMARY KEY (`lkm_id`),
  KEY `FK_cst_linkman_lkm_cust_id` (`lkm_cust_id`),
  CONSTRAINT `FK_cst_linkman_lkm_cust_id` FOREIGN KEY (`lkm_cust_id`) REFERENCES `cst_customer` (`cust_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

联系人管理-查询列表| CRM客户关系管理系统项目实战四(Struts2+Spring+Hibernate)解析+源代码_第1张图片

2、创建实体和映射

(1)创建联系人的实体
package com.itzheng.crm.domain;

/*
 * 联系人的实体
 * CREATE TABLE `cst_linkman` (
  `lkm_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '联系人编号(主键)',
  `lkm_name` VARCHAR(16) DEFAULT NULL COMMENT '联系人姓名',
  `lkm_cust_id` BIGINT(32) NOT NULL COMMENT '客户id',
  `lkm_gender` CHAR(1) DEFAULT NULL COMMENT '联系人性别',
  `lkm_phone` VARCHAR(16) DEFAULT NULL COMMENT '联系人办公电话',
  `lkm_mobile` VARCHAR(16) DEFAULT NULL COMMENT '联系人手机',
  `lkm_email` VARCHAR(64) DEFAULT NULL COMMENT '联系人邮箱',
  `lkm_qq` VARCHAR(16) DEFAULT NULL COMMENT '联系人qq',
  `lkm_position` VARCHAR(16) DEFAULT NULL COMMENT '联系人职位',
  `lkm_memo` VARCHAR(512) DEFAULT NULL COMMENT '联系人备注',
  PRIMARY KEY (`lkm_id`),
  KEY `FK_cst_linkman_lkm_cust_id` (`lkm_cust_id`),
  CONSTRAINT `FK_cst_linkman_lkm_cust_id` FOREIGN KEY (`lkm_cust_id`) REFERENCES `cst_customer` (`cust_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=INNODB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
 */
public class LinkMan {
	private Long lkm_id;
	private String lkm_name;
	// 外键要编程映射
	private String lkm_gender;
	private String lkm_phone;
	private String lkm_mobile;
	private String lkm_email;
	private String lkm_qq;
	private String lkm_position;
	private String lkm_memo;
	// 客户是一联系人是多
	// 联系人这一方放置客户的对象
	private Customer customer;
	public Long getLkm_id() {
		return lkm_id;
	}
	public void setLkm_id(Long lkm_id) {
		this.lkm_id = lkm_id;
	}
	public String getLkm_name() {
		return lkm_name;
	}
	public void setLkm_name(String lkm_name) {
		this.lkm_name = lkm_name;
	}
	public String getLkm_gender() {
		return lkm_gender;
	}
	public void setLkm_gender(String lkm_gender) {
		this.lkm_gender = lkm_gender;
	}
	public String getLkm_phone() {
		return lkm_phone;
	}
	public void setLkm_phone(String lkm_phone) {
		this.lkm_phone = lkm_phone;
	}
	public String getLkm_mobile() {
		return lkm_mobile;
	}
	public void setLkm_mobile(String lkm_mobile) {
		this.lkm_mobile = lkm_mobile;
	}
	public String getLkm_email() {
		return lkm_email;
	}
	public void setLkm_email(String lkm_email) {
		this.lkm_email = lkm_email;
	}
	public String getLkm_qq() {
		return lkm_qq;
	}
	public void setLkm_qq(String lkm_qq) {
		this.lkm_qq = lkm_qq;
	}
	public String getLkm_position() {
		return lkm_position;
	}
	public void setLkm_position(String lkm_position) {
		this.lkm_position = lkm_position;
	}
	public String getLkm_memo() {
		return lkm_memo;
	}
	public void setLkm_memo(String lkm_memo) {
		this.lkm_memo = lkm_memo;
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	public LinkMan() {
		// TODO Auto-generated constructor stub
	}
	public LinkMan(Long lkm_id, String lkm_name, String lkm_gender, String lkm_phone, String lkm_mobile,
			String lkm_email, String lkm_qq, String lkm_position, String lkm_memo, Customer customer) {
		super();
		this.lkm_id = lkm_id;
		this.lkm_name = lkm_name;
		this.lkm_gender = lkm_gender;
		this.lkm_phone = lkm_phone;
		this.lkm_mobile = lkm_mobile;
		this.lkm_email = lkm_email;
		this.lkm_qq = lkm_qq;
		this.lkm_position = lkm_position;
		this.lkm_memo = lkm_memo;
		this.customer = customer;
	}
	@Override
	public String toString() {
		return "LinkMan [lkm_id=" + lkm_id + ", lkm_name=" + lkm_name + ", lkm_gender=" + lkm_gender + ", lkm_phone="
				+ lkm_phone + ", lkm_mobile=" + lkm_mobile + ", lkm_email=" + lkm_email + ", lkm_qq=" + lkm_qq
				+ ", lkm_position=" + lkm_position + ", lkm_memo=" + lkm_memo + ", customer=" + customer + "]";
	}
}
(2)创建联系人的映射
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<!-- 建立类与表的映射 -->
	<class name="com.itzheng.crm.domain.LinkMan" table="cst_linkman">
		<!-- 建立类中的属性与表中的主键对应 -->
		<id name="lkm_id" column="lkm_id">
			<!-- 主键生成策略 -->
			<generator class="native" />
		</id>
		<!-- 建立类中的普通的属性和表的字段的对应映射 -->
		<property name="lkm_name" column="lkm_name" />
		<property name="lkm_gender" column="lkm_gender" />
		<property name="lkm_phone" column="lkm_phone" />
		<property name="lkm_mobile" column="lkm_mobile" />
		<property name="lkm_email" column="lkm_email" />
		<property name="lkm_qq" column="lkm_qq" />
		<property name="lkm_position" column="lkm_position" />
		<property name="lkm_memo" column="lkm_memo" />		
		<!-- 配置客户与字典的多对一的映射 -->
		<many-to-one name="customer" class="com.itzheng.crm.domain.Customer" column="lkm_cust_id"></many-to-one>
	</class>
</hibernate-mapping>
(3)修改客户的实体

一个客户可以有多个联系人,在客户当中添加联系人的集合
联系人管理-查询列表| CRM客户关系管理系统项目实战四(Struts2+Spring+Hibernate)解析+源代码_第2张图片

(4)修改客户的映射

联系人管理-查询列表| CRM客户关系管理系统项目实战四(Struts2+Spring+Hibernate)解析+源代码_第3张图片

(5)在appliactionContext.xml当中添加LinkMan.hbm.xml映射文件

联系人管理-查询列表| CRM客户关系管理系统项目实战四(Struts2+Spring+Hibernate)解析+源代码_第4张图片

3、创建一些相关的类

(1)创建联系人Dao接口以及实现类
package com.itzheng.crm.dao;
/*
 * 联系人的DAO接口
 */
public interface LinkManDao {	
}

继承HibernateDaoSupport

package com.itzheng.crm.dao.impl;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itzheng.crm.dao.LinkManDao;
/*
 * 联系人的DAO的实现类
 */
public class LinkManDaoImpl extends HibernateDaoSupport implements LinkManDao {
}
(2)创建联系人Service以及对应的实现类
package com.itzheng.crm.service;
public interface LinkManService {
}

注入Dao

package com.itzheng.crm.service.impl;
import com.itzheng.crm.dao.LinkManDao;
import com.itzheng.crm.service.LinkManService;
public class LinkManServiceImpl implements LinkManService {
	private LinkManDao linkManDao;
	public LinkManDao setLinkManDao() {
		return linkManDao;
	}
}
(3)创建LinkManAction
package com.itzheng.crm.web.action;
import com.itzheng.crm.domain.LinkMan;
import com.itzheng.crm.service.LinkManService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/*
 * 联系人的Action的类
 */
public class LinkManAction extends ActionSupport implements ModelDriven<LinkMan> {
	// 模型驱动使用的
	private LinkMan linkMan = new LinkMan();
	@Override
	public LinkMan getModel() {
		// TODO Auto-generated method stub
		return linkMan;
	}
	// 输入Service
	private LinkManService linkManService;
	public void setLinkManService(LinkManService linkManService) {
		this.linkManService = linkManService;
	}
}

4、完成相关配置

在applicationContext.xml当中配置联系人相关的类

联系人管理-查询列表| CRM客户关系管理系统项目实战四(Struts2+Spring+Hibernate)解析+源代码_第5张图片

二、查询联系人的列表

1、修改menu.jsp的链接

在这里插入图片描述

2、编写LinkManAction,设置分页参数并设置对应findAll方法

设置分页参数
编写findAll方法

package com.itzheng.crm.web.action;
import org.hibernate.criterion.DetachedCriteria;
import com.itzheng.crm.domain.LinkMan;
import com.itzheng.crm.domain.PageBean;
import com.itzheng.crm.service.LinkManService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/*
 * 联系人的Action的类
 */
public class LinkManAction extends ActionSupport implements ModelDriven<LinkMan> {
	// 模型驱动使用的
	private LinkMan linkMan = new LinkMan();
	@Override
	public LinkMan getModel() {
		// TODO Auto-generated method stub
		return linkMan;
	}
	// 输入Service
	private LinkManService linkManService;
	public void setLinkManService(LinkManService linkManService) {
		this.linkManService = linkManService;
	}
	// 分页参数
	// 使用set方法的属性驱动的方式来接收数据
	private Integer currPage = 1;
	public void setCurrPage(Integer currPage) {
		if (currPage == null) {
			currPage = 1;
		}
		this.currPage = currPage;
	}
	// 使用set方法接受每页显示的记录数
	private Integer pageSize = 3;
	public void setPageSize(Integer pageSize) {
		if (pageSize == null) {
			pageSize = 3;
		}
		this.pageSize = pageSize;
	}
	/*
	 * 查询联系人列表的Action
	 */
	public String findAll() {
		// 创建离线条件查询
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(LinkMan.class);
		// 设置条件
		// 调用业务层
		PageBean<LinkMan> pageBean = linkManService.findAll(detachedCriteria, currPage, pageSize);
		ActionContext.getContext().getValueStack().push(pageBean);
		return "findAll";
	}
}

3、编写Service

在LinkManServiceImpl 编写findAll的方法

package com.itzheng.crm.service.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import com.itzheng.crm.dao.LinkManDao;
import com.itzheng.crm.domain.LinkMan;
import com.itzheng.crm.domain.PageBean;
import com.itzheng.crm.service.LinkManService;
public class LinkManServiceImpl implements LinkManService {
	private LinkManDao linkManDao;
	public void setLinkManDao(LinkManDao linkManDao) {
		this.linkManDao = linkManDao;
	}
	@Override
	//业务层来分页查询联系人的方法
	public PageBean<LinkMan> findAll(DetachedCriteria detachedCriteria, Integer currPage, Integer pageSize) {		
		PageBean<LinkMan> pageBean = new PageBean<LinkMan>();		
		//设置当前页数
		pageBean.setCurrPage(currPage);		
		//设置每页显示的记录数
		pageBean.setPageSize(pageSize);		
		//设置总记录数
		Integer totalCount = linkManDao.findCount(detachedCriteria);		
		//设置总页数
		pageBean.setTotalCount(totalCount);
		double tc = totalCount;
		Double num = Math.ceil(tc / pageSize);
		pageBean.setTotalPage(num.intValue());	
		//以及每页显示的数据的集合
		Integer begin = (currPage - 1) * pageSize;	
		List<LinkMan> list = linkManDao.findByPage(detachedCriteria,begin,pageSize);
		pageBean.setList(list);	
		return pageBean;
	}
}

4、编写Dao

LinkManDaoImpl

package com.itzheng.crm.dao.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itzheng.crm.dao.LinkManDao;
import com.itzheng.crm.domain.LinkMan;
/*
 * 联系人的DAO的实现类
 */
public class LinkManDaoImpl extends HibernateDaoSupport implements LinkManDao {
	@Override
	// DAO 当中统计个数的方法
	public Integer findCount(DetachedCriteria detachedCriteria) {
		// TODO Auto-generated method stub
		detachedCriteria.setProjection(Projections.rowCount());// 带条件的查询
		List<Long> list = (List<Long>) this.getHibernateTemplate().findByCriteria(detachedCriteria);
		if (list.size() > 0) {
			return list.get(0).intValue();
		}
		return null;
	}
	@Override
	// DAO 的分页查询
	public List<LinkMan> findByPage(DetachedCriteria detachedCriteria, Integer begin, Integer pageSize) {
		// TODO Auto-generated method stub
		// 情况count语句
		detachedCriteria.setProjection(null);
		// 等到list集合:带条件的查询
		List<LinkMan> list = (List<LinkMan>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin,
				pageSize);
		return list;
	}
}

5、在页面当中回显数据

(1)在struts.xml当中配置联系人的配置

联系人管理-查询列表| CRM客户关系管理系统项目实战四(Struts2+Spring+Hibernate)解析+源代码_第6张图片

(2)在linkman的list.jsp当中遍历list集合

联系人管理-查询列表| CRM客户关系管理系统项目实战四(Struts2+Spring+Hibernate)解析+源代码_第7张图片

(3)测试

测试成功
联系人管理-查询列表| CRM客户关系管理系统项目实战四(Struts2+Spring+Hibernate)解析+源代码_第8张图片

(4)完善下面的分页功能

在linkman的list.jsp
联系人管理-查询列表| CRM客户关系管理系统项目实战四(Struts2+Spring+Hibernate)解析+源代码_第9张图片

(5)测试成功

联系人管理-查询列表| CRM客户关系管理系统项目实战四(Struts2+Spring+Hibernate)解析+源代码_第10张图片
联系人管理-查询列表| CRM客户关系管理系统项目实战四(Struts2+Spring+Hibernate)解析+源代码_第11张图片

你可能感兴趣的:(项目实战+源代码)