CRM第四天:联系人查找和保存功能实现
目录
CRM联系人管理模块代码实现
1.1需求分析
联系人管理中对联系人的操作:
-
- 添加联系人:保存联系人基本信息
- 查询联系人:分页查询,条件查询
- 修改联系人:对联系人的信息进行修改
- 删除联系人:删除联系人
1.2联系人管理的准备工作
1.2.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=1 DEFAULT CHARSET=utf8;
1.2.2创建实体和映射
LinkMan实体:
/**
*
* 项目名称:CRM
* 类名称:LinkMan
* 类描述:联系人实体
* 创建人:yangyangyang
* 创建时间:2018年12月19日 下午4:47:53
* 修改人:yangyangyang
* 修改时间:2018年12月19日 下午4:47:53
* 修改备注:
* @version
*
*/
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;
}
}
LinkMan.hbm.xml映射文件:
<?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.albertyy.crm.entity.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.albertyy.crm.entity.Customer" column="lkm_cust_id">
</many-to-one>
</class>
</hibernate-mapping>
1.2.3修改客户的实体和映射
修改客户的实体:添加联系人的集合属性,并生成set和get方法。
public class Customer {
private Long cust_id;
private String cust_name;
/*private String cust_source;
private String cust_industry;
private String cust_level;*/
private String cust_phone;
private String cust_mobile;
private String cust_image;
//客户和字典的关系是多对一,所以需要在多的一方,放一的一方的对象
private BaseDict baseDictSource;
private BaseDict baseDictIndustry;
private BaseDict baseDictLevel;
//联系人的集合
private Set<LinkMan> linkMans=new HashSet<LinkMan>();
public Set<LinkMan> getLinkMans() {
return linkMans;
}
public void setLinkMans(Set<LinkMan> linkMans) {
this.linkMans = linkMans;
}
修改客户的映射,添加联系人的集合属性。
<!-- 客户关联联系人的集合 -->
<set name="linkMans">
<key column="lkm_cust_id"></key>
<one-to-many class="com.albertyy.crm.entity.LinkMan"/>
</set>
1.2.4将映射添加到配置文件中
<!-- 引入映射文件 -->
<property name="mappingResources">
<list>
<value>com/albertyy/crm/entity/User.hbm.xml</value>
<value>com/albertyy/crm/entity/Customer.hbm.xml</value>
<value>com/albertyy/crm/entity/BaseDict.hbm.xml</value>
<value>com/albertyy/crm/entity/LinkMan.hbm.xml</value>
</list>
</property>
1.2.5创建相关的类
1.2.6配置相关的类
<!-- ======配置联系人的相关类======= -->
<!-- 配置Action -->
<bean id="linkManAction" class="com.albertyy.crm.web.action.LinkManAction" scope="prototype">
<property name="linkManService" ref="linkManService"/>
</bean>
<!-- 配置Service -->
<bean id="linkManService" class="com.albertyy.crm.serviceImpl.LinkManServiceImpl">
<property name="linkManDao" ref="linkManDao"/>
</bean>
<!-- 配置DAO -->
<bean id="linkManDao" class="com.albertyy.crm.daoImpl.LinkManDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
在struts2中配置Action:
<!-- 联系人管理Action -->
<action name="linkMan_*" class="linkManAction" method="{1}">
</action>
1.3CRM联系人管理:显示联系人列表
1.3.1修改left.jsp路径
<h3>联系人管理</h3>
<ul>
<li><a href="${pageContext.request.contextPath }/linkman/add.jsp">新增联系人</a></li>
<li><a
href="${pageContext.request.contextPath }/linkMan_findAll.action">联系人列表</a>
</li>
</ul>
1.3.2编写Action
package com.albertyy.crm.web.action;
import org.hibernate.criterion.DetachedCriteria;
import com.albertyy.crm.entity.LinkMan;
import com.albertyy.crm.service.LinkManService;
import com.albertyy.crm.utils.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
*
* 项目名称:CRM
* 类名称:LinkManAction
* 类描述: 联系人管理Action
* 创建人:yangyangyang
* 创建时间:2018年12月19日 下午5:29:26
* 修改人:yangyangyang
* 修改时间:2018年12月19日 下午5:29:26
* 修改备注:
* @version
*
*/
public class LinkManAction extends ActionSupport implements ModelDriven<LinkMan> {
//模型驱动对象
LinkMan linkMan=new LinkMan();
@Override
public LinkMan getModel() {
return linkMan;
}
//注入Service
private LinkManService linkManService;
public void setLinkManService(LinkManService linkManService) {
this.linkManService = linkManService;
}
//接收当前页数和每页显示记录数
private Integer page=1;
private Integer pageSize=5;
public void setPage(Integer page) {
if(page==null){
page=1;
}
this.page = page;
}
public void setPageSize(Integer pageSize) {
if(pageSize==null){
pageSize=5;
}
this.pageSize = pageSize;
}
//查询所有联系人的方法
public String findAll(){
DetachedCriteria detachedCriteria=DetachedCriteria.forClass(LinkMan.class);
//设置条件
//调用业务层查询
PageBean<LinkMan> pageBean=linkManService.findByPage(detachedCriteria,page,pageSize);
ActionContext.getContext().getValueStack().push(pageBean);
return "findAll";
}
}
1.3.3编写Service
//注入Dao
private LinkManDao linkManDao;
public void setLinkManDao(LinkManDao linkManDao) {
this.linkManDao = linkManDao;
}
//业务层分页查询联系人
@Override
public PageBean<LinkMan> findByPage(DetachedCriteria detachedCriteria, Integer page, Integer pageSize) {
PageBean<LinkMan> pageBean=new PageBean<LinkMan>();
//设置当前页数
pageBean.setPage(page);
//设置每页显示记录数
pageBean.setPageSize(pageSize);
//设置总记录数
int totalCount=linkManDao.findByCount(detachedCriteria);
pageBean.setTotalCount(totalCount);
//设置总页数
double tc=totalCount;
Double num=Math.ceil(tc/pageSize);
pageBean.setTotalPage(num.intValue());
//每页显示数据的集合
Integer begin=(page-1)*pageSize;
List<LinkMan> list=linkManDao.findByPage(detachedCriteria,begin,pageSize);
pageBean.setList(list);
return pageBean;
}
1.3.4编写Dao
//Dao层统计联系人总记录数
@Override
public int findByCount(DetachedCriteria detachedCriteria) {
detachedCriteria.setProjection(Projections.rowCount());
List<Long> list=(List<Long>) this.getHibernateTemplate().findByCriteria(detachedCriteria);
if(list.size()>0){
return list.get(0).intValue();
}
return 0;
}
//Dao层分页查询联系人
@Override
public List<LinkMan> findByPage(DetachedCriteria detachedCriteria, Integer begin, Integer pageSize) {
detachedCriteria.setProjection(null);
return (List<LinkMan>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin, pageSize);
}
1.3.5配置页面路径
<!-- 联系人管理Action -->
<action name="linkMan_*" class="linkManAction" method="{1}">
<result name="findAll">/linkman/list.jsp</result>
</action>
1.3.6页面显示数据
<table width="100%" border="0" cellpadding="0" cellspacing="0"
class="list_table">
<tr>
<th width="100">联系人名称</th>
<th width="100">性别</th>
<th width="100">办公电话</th>
<th width="100">手机</th>
<th width="100">客户姓名</th>
<th width="100">email</th>
<th width="100">QQ</th>
<th width="100">职位</th>
<th width="100">备注</th>
<th width="180">操作</th>
</tr>
<s:iterator value="list">
<tr class="tr">
<td><s:property value="lkm_name" /></td>
<td><s:if test="lkm_gender==1">男</s:if><s:elseif test="lkm_gender==2">女</s:elseif></td>
<td><s:property value="lkm_phone" /></td>
<td><s:property value="lkm_mobile" /></td>
<td><s:property value="customer.cust_name" /></td>
<td><s:property value="lkm_email" /></td>
<td><s:property value="lkm_qq" /></td>
<td><s:property value="lkm_position" /></td>
<td><s:property value="lkm_memo" /></td>
<td><input type="button" name="button"
class="btn btn82 btn_add" id="modifyBtn"
onclick="toUpdate('<s:property value="lkm_id"/>')" value="修改">
<input type="button" name="button" class="btn btn82 btn_del"
onclick="toDelete('<s:property value="lkm_id"/>')" value="删除"></td>
</tr>
</s:iterator>
</table>
<div class="page mt10">
<div class="pagination">
<ul>
<li class="disabled"><span> 共<s:property
value="totalCount" />条记录, 共 <s:property value="totalPage" />页
</span></li>
<li><span> 每页显示记录数: <select name="pageSize"
onchange="to_page()">
<option value="5" <s:if test="pageSize==5">selected</s:if>>5条</option>
<option value="10" <s:if test="pageSize==10">selected</s:if>>10条</option>
<option value="20" <s:if test="pageSize==20">selected</s:if>>20条</option>
</select>
</span></li>
<s:if test="page!=1">
<li><a href="javascript:to_page(1)">首页</a></li>
<li><a
href="javascript:to_page(<s:property value="page-1" />)">上一页</a></li>
</s:if>
<s:iterator var="i" begin="1" end="totalPage">
<s:if test="#i==page">
<li class="active"><span><s:property value="#i" />
</span></li>
</s:if>
<s:else>
<li><a
href="javascript:to_page(<s:property value="#i" />)"><s:property
value="#i" /></a></li>
</s:else>
</s:iterator>
<s:if test="page!=totalPage">
<li><a
href="javascript:to_page(<s:property value="page+1" />)">下一页</a></li>
<li><a
href="javascript:to_page(<s:property value="totalPage" />)">末页</a></li>
</s:if>
<li>到第 <input class="input-text lh25" type="text"
id="page" name="page" size="2" height="10px" />页 <input
class="ext_btn" type="button" value="GO" onclick="to_page()" />
</li>
</ul>
</div>
</div>
1.4CRM联系人管理:保存联系人
1.4.1修改left.jsp链接
<h3>联系人管理</h3>
<ul>
<li><a href="${pageContext.request.contextPath }/linkMan_saveUI.action">新增联系人</a></li>
<li><a
href="${pageContext.request.contextPath }/linkMan_findAll.action">联系人列表</a>
</li>
</ul>
1.4.2编写Action中的saveUI的方法
//跳转到添加页面的方法
public String saveUI(){
//查询客户
List<Customer> list=customerService.findAll();
ActionContext.getContext().getValueStack().set("list", list);
return "saveUI";
}
1.4.3编写Service
//查询所有客户
@Override
public List<Customer> findAll() {
return customerDao.findAll();
}
1.4.4编写Dao
//查询所有客户
@Override
public List<Customer> findAll() {
return (List<Customer>) this.getHibernateTemplate().find("from Customer");
}
1.4.5配置修改跳转
<!-- 联系人管理Action -->
<action name="linkMan_*" class="linkManAction" method="{1}">
<result name="findAll">/linkman/list.jsp</result>
<result name="saveUI">/linkman/add.jsp</result>
<result name="saveSuccess">/linkman/list.jsp</result>
</action>
1.4.6编写页面
<div class="box_border">
<div class="box_top">
<b class="pl15">添加联系人</b>
</div>
<div class="box_center">
<form
action="${pageContext.request.contextPath }/linkMan_save.action"
method="post" class="jqtransform">
<table class="form_table pt15 pb15" width="100%" border="0"
cellpadding="0" cellspacing="0">
<tr>
<td class="td_right">所属客户:</td>
<td class=""><span class="fl">
<div class="select_border">
<div class="select_containers ">
<select name="customer.cust_id" id="cust_source"
class="select">
<option value="">请选择</option>
<s:iterator value="list">
<option value='<s:property value="cust_id"/>'><s:property
value="cust_name" /></option>
</s:iterator>
</select>
</div>
</div>
</span></td>
</tr>
<tr>
<td class="td_right">联系人名称:</td>
<td class=""><input type="text" name="lkm_name"
class="input-text lh30" required="required"></td>
</tr>
<tr>
<td class="td_right">联系人性别:</td>
<td class=""><input type="radio" name="lkm_gender" value="1">
男 <input type="radio" name="lkm_gender" value="2">女</td>
</tr>
<tr>
<td class="td_right">联系人电话:</td>
<td class=""><input type="number" name="lkm_phone"
class="input-text lh30" ></td>
</tr>
<tr>
<td class="td_right">联系人手机:</td>
<td class=""><input type="number" name="lkm_mobile"
class="input-text lh30" ></td>
</tr>
<tr>
<td class="td_right">联系人邮箱:</td>
<td class=""><input type="email" name="lkm_email"
class="input-text lh30" ></td>
</tr>
<tr>
<td class="td_right">联系人QQ:</td>
<td class=""><input type="number" name="lkm_qq"
class="input-text lh30" ></td>
</tr>
<tr>
<td class="td_right">联系人职位:</td>
<td class=""><input type="text" name="lkm_position"
class="input-text lh30" ></td>
</tr>
<tr>
<td class="td_right">联系人备注:</td>
<td class=""><textarea name="lkm_memo" id="" cols="30" rows="10"
class="textarea" style="width: 362px; height: 93px;"></textarea>
</td>
</tr>
<tr>
<td class="td_right"> </td>
<td class=""><input type="submit" name="button"
class="btn btn82 btn_save2" value="保存"> <input
type="reset" name="button" class="btn btn82 btn_res"
value="重置"></td>
</tr>
</table>
</form>
</div>
</div>
1.4.7编写action
//保存联系人的方法
public String save(){
//调用业务层
linkManService.save(linkMan);
//跳转到查询页面
findAll();
return "saveSuccess";
}
1.4.8编写service
//业务层保存联系人方法
@Override
public void save(LinkMan linkMan) {
linkManDao.save(linkMan);
}
1.4.9编写Dao
//dao层保存联系人
@Override
public void save(LinkMan linkMan) {
this.getHibernateTemplate().save(linkMan);
}
1.4.10配置页面跳转:
<!-- 联系人管理Action -->
<action name="linkMan_*" class="linkManAction" method="{1}">
<result name="findAll">/linkman/list.jsp</result>
<result name="saveUI">/linkman/add.jsp</result>
<result name="saveSuccess">/linkman/list.jsp</result>
</action>