6、客户模块-数据字典与客户的关系
1. 数据字典表的引入 * 数据字典表的作用:规范开发中数据的写法 * 字段表与客户表是一对多的关系 * 修改客户表,添加外键(使用SQLyog进行修改) ![](./图片/01-字典表.bmp) 2. 创建字典表的实体和映射的配置文件 * 编写字典表的JavaBean和映射的配置文件 * 修改Customer的JavaBean,因为是多方,需要把外键字段换成字典对象 * 修改Customer.hbm.xml的配置文件,配置多对一 3. 分页查询所有的客户功能实现
字典与客户一对多的关系
crm_base_dict.sql
/* SQLyog v10.2 MySQL - 5.1.72-community : Database - crm_hibernate ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; /*Table structure for table `base_dict` */ CREATE TABLE `base_dict` ( `dict_id` varchar(32) NOT NULL COMMENT '数据字典id(主键)', `dict_type_code` varchar(10) NOT NULL COMMENT '数据字典类别代码', `dict_type_name` varchar(64) NOT NULL COMMENT '数据字典类别名称', `dict_item_name` varchar(64) NOT NULL COMMENT '数据字典项目名称', `dict_item_code` varchar(10) DEFAULT NULL COMMENT '数据字典项目(可为空)', `dict_sort` int(10) DEFAULT NULL COMMENT '排序字段', `dict_enable` char(1) NOT NULL COMMENT '1:使用 0:停用', `dict_memo` varchar(64) DEFAULT NULL COMMENT '备注', PRIMARY KEY (`dict_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*Data for the table `base_dict` */ LOCK TABLES `base_dict` WRITE; insert into `base_dict`(`dict_id`,`dict_type_code`,`dict_type_name`,`dict_item_name`,`dict_item_code`,`dict_sort`,`dict_enable`,`dict_memo`) values ('1','001','客户行业','教育培训 ',NULL,1,'1',NULL),('10','003','公司性质','民企',NULL,3,'1',NULL),('12','004','年营业额','1-10万',NULL,1,'1',NULL),('13','004','年营业额','10-20万',NULL,2,'1',NULL),('14','004','年营业额','20-50万',NULL,3,'1',NULL),('15','004','年营业额','50-100万',NULL,4,'1',NULL),('16','004','年营业额','100-500万',NULL,5,'1',NULL),('17','004','年营业额','500-1000万',NULL,6,'1',NULL),('18','005','客户状态','基础客户',NULL,1,'1',NULL),('19','005','客户状态','潜在客户',NULL,2,'1',NULL),('2','001','客户行业','电子商务',NULL,2,'1',NULL),('20','005','客户状态','成功客户',NULL,3,'1',NULL),('21','005','客户状态','无效客户',NULL,4,'1',NULL),('22','006','客户级别','普通客户',NULL,1,'1',NULL),('23','006','客户级别','VIP客户',NULL,2,'1',NULL),('24','007','商机状态','意向客户',NULL,1,'1',NULL),('25','007','商机状态','初步沟通',NULL,2,'1',NULL),('26','007','商机状态','深度沟通',NULL,3,'1',NULL),('27','007','商机状态','签订合同',NULL,4,'1',NULL),('3','001','客户行业','对外贸易',NULL,3,'1',NULL),('30','008','商机类型','新业务',NULL,1,'1',NULL),('31','008','商机类型','现有业务',NULL,2,'1',NULL),('32','009','商机来源','电话营销',NULL,1,'1',NULL),('33','009','商机来源','网络营销',NULL,2,'1',NULL),('34','009','商机来源','推广活动',NULL,3,'1',NULL),('4','001','客户行业','酒店旅游',NULL,4,'1',NULL),('5','001','客户行业','房地产',NULL,5,'1',NULL),('6','002','客户信息来源','电话营销',NULL,1,'1',NULL),('7','002','客户信息来源','网络营销',NULL,2,'1',NULL),('8','003','公司性质','合资',NULL,1,'1',NULL),('9','003','公司性质','国企',NULL,2,'1',NULL); UNLOCK TABLES; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
/crm/src/com/louis/domain/Dict.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.louis.domain.Dict" table="base_dict"> <id name="dict_id" column="dict_id"> <!-- 换成字符串生成的策略 --> <generator class="uuid"/> </id> <property name="dict_type_code" column="dict_type_code"/> <property name="dict_type_name" column="dict_type_name"/> <property name="dict_item_name" column="dict_item_name"/> <property name="dict_item_code" column="dict_item_code"/> <property name="dict_sort" column="dict_sort"/> <property name="dict_enable" column="dict_enable"/> <property name="dict_memo" column="dict_memo"/> </class> </hibernate-mapping>
/crm/src/com/louis/domain/Dict.java
package com.louis.domain; /** * 字典表 * @author Administrator */ public class Dict { /** * `dict_id` varchar(32) NOT NULL COMMENT '数据字典id(主键)', `dict_type_code` varchar(10) NOT NULL COMMENT '数据字典类别代码', `dict_type_name` varchar(64) NOT NULL COMMENT '数据字典类别名称', `dict_item_name` varchar(64) NOT NULL COMMENT '数据字典项目名称', `dict_item_code` varchar(10) DEFAULT NULL COMMENT '数据字典项目(可为空)', `dict_sort` int(10) DEFAULT NULL COMMENT '排序字段', `dict_enable` char(1) NOT NULL COMMENT '1:使用 0:停用', `dict_memo` varchar(64) DEFAULT NULL COMMENT '备注', */ private String dict_id; // 数据字典类别代码 01 06 private String dict_type_code; // 类别名称 01所属于行业 06客户级别 private String dict_type_name; // 字典项目名称 private String dict_item_name; private String dict_item_code; // 排序字段 private Integer dict_sort; private String dict_enable; private String dict_memo; public String getDict_id() { return dict_id; } public void setDict_id(String dict_id) { this.dict_id = dict_id; } public String getDict_type_code() { return dict_type_code; } public void setDict_type_code(String dict_type_code) { this.dict_type_code = dict_type_code; } public String getDict_type_name() { return dict_type_name; } public void setDict_type_name(String dict_type_name) { this.dict_type_name = dict_type_name; } public String getDict_item_name() { return dict_item_name; } public void setDict_item_name(String dict_item_name) { this.dict_item_name = dict_item_name; } public String getDict_item_code() { return dict_item_code; } public void setDict_item_code(String dict_item_code) { this.dict_item_code = dict_item_code; } public Integer getDict_sort() { return dict_sort; } public void setDict_sort(Integer dict_sort) { this.dict_sort = dict_sort; } public String getDict_enable() { return dict_enable; } public void setDict_enable(String dict_enable) { this.dict_enable = dict_enable; } public String getDict_memo() { return dict_memo; } public void setDict_memo(String dict_memo) { this.dict_memo = dict_memo; } }
/crm/src/applicationContext.xml
<!-- 引入映射的配置文件 --> <property name="mappingResources"> <list> <value>com/louis/domain/User.hbm.xml</value> <value>com/louis/domain/Customer.hbm.xml</value> <value>com/louis/domain/Dict.hbm.xml</value> </list> </property>
多方維護關係/crm/src/com/louis/domain/Customer.java
package com.louis.domain; public class Customer { /** * `cust_id` bigint(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)', `cust_name` varchar(32) NOT NULL COMMENT '客户名称(公司名称)', `cust_user_id` bigint(32) DEFAULT NULL COMMENT '负责人id', `cust_create_id` bigint(32) DEFAULT NULL COMMENT '创建人id', `cust_source` varchar(32) DEFAULT NULL COMMENT '客户信息来源', `cust_industry` varchar(32) DEFAULT NULL COMMENT '客户所属行业', `cust_level` varchar(32) DEFAULT NULL COMMENT '客户级别', `cust_linkman` varchar(64) DEFAULT NULL COMMENT '联系人', `cust_phone` varchar(64) DEFAULT NULL COMMENT '固定电话', `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话', */ 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 Dict source; // 一客户的行业 多是客户 private Dict industry; // 一客户级别 多是客户 private Dict level; 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 Dict getSource() { return source; } public void setSource(Dict source) { this.source = source; } public Dict getIndustry() { return industry; } public void setIndustry(Dict industry) { this.industry = industry; } public Dict getLevel() { return level; } public void setLevel(Dict level) { this.level = 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; } }
/crm/src/com/louis/domain/Customer.hbm.xml
<!-- <property name="cust_source" column="cust_source"/>
<property name="cust_industry" column="cust_industry"/>
<property name="cust_level" column="cust_level"/> -->
<!-- 配置的多方 name是JavaBean属性名称 class="一方类的全路径" cloumn="sql外键的名称" --> <many-to-one name="source" class="com.louis.domain.Dict" column="cust_source"/> <many-to-one name="industry" class="com.louis.domain.Dict" column="cust_industry"/> <many-to-one name="level" class="com.louis.domain.Dict" column="cust_level"/>
posted on 2017-10-22 11:05 Michael2397 阅读(741) 评论(0) 编辑 收藏 举报