1. 项目概述
CRM:Customer Relationship Management,客户关系管理系统。
客户关系管理的定义是:企业为提高核心竞争力,利用相应的信息技术以及互联网技术协调企业与顾客间在销售、营销和服务上的交互,从而提升其管理方式,向客户提供创新式的个性化的客户交互和服务的过程。其最终目标是吸引新客户、保留老客户以及将已有客户转为忠实客户,增加市场。
比如:汽车4S店、售楼中心、房产中介、保险行业
本案例主要是完成两个模块:客户管理、联系人管理。
2. 环境搭建
2.1 创建项目
项目名:maven-crm
项目位置:
导入坐标
<packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <!--声明不需要web.xml文件--> <failOnMissingWebXml>false</failOnMissingWebXml> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.4.RELEASE</version> </dependency> <!-- 事务 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.4.RELEASE</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.24</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.7.5</version> </dependency> <!--整合--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.5</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!-- mvc json --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.10.2</version> </dependency> <!--swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> <!--文件上传--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.4</version> </dependency> <!--jsp相关--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--整合日志--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.10</version> </dependency> <!--common工具--> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.11</version> </dependency> </dependencies>
2.1创建webapp目录
2.2 导入jsp页面
2.3 配置类
拷贝配置类
2.3.1 MyBatis 配置
拷贝:MyBatisConfiguration3,修改mapper所在包,并创建对应的包
package com.czxy.crm.config; import com.github.pagehelper.PageHelper; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.springframework.context.annotation.Bean; import tk.mybatis.spring.mapper.MapperScannerConfigurer; import javax.sql.DataSource; import java.util.Properties; /** * @author 桐叔 * @email liangtong@itcast.cn */ public class MyBatisConfiguration3 { /** * 配置session工厂 * @param dataSource * @return * @throws Exception */ @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception{ //1 创建 factoryBean SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); //2 设置数据 // 2.1 数据源 factoryBean.setDataSource(dataSource); // 2.2 驼峰命名 Configuration configuration = new Configuration(); configuration.setMapUnderscoreToCamelCase(true); factoryBean.setConfiguration(configuration); // 2.3 分页插件 Properties props = new Properties(); // 设置方言 props.setProperty("dialect", "mysql"); // 分页的同时进行count查询 props.setProperty("rowBoundsWithCount", "true"); // 分页合理化参数,pageNum<=0 时会查询第一页,pageNum>pages (超过总数时),会查询最后一页 props.setProperty("reasonable", "true"); // PageInterceptor pageInterceptor = new PageInterceptor(); // pageInterceptor.setProperties(props); PageHelper pageHelper = new PageHelper(); pageHelper.setProperties(props); factoryBean.setPlugins(new Interceptor[] {pageHelper}); //3 通过factorybean获得对应 return factoryBean.getObject(); } /** * 映射扫描器 * @return */ @Bean public MapperScannerConfigurer mapperScannerConfigurer(){ //1 创建 MapperScannerConfigurer mapperScanner = new MapperScannerConfigurer(); //2设置包 mapperScanner.setBasePackage("com.czxy.crm.mapper"); return mapperScanner; } }
2.3.2 Spring 配置
拷贝:SpringConfiguration3
修改service所在包,并创建对应的包
创建 db.properties
jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm_crm jdbc.username=root jdbc.password=1234 package com.czxy.crm.config; import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.PropertySource; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; @ComponentScan(basePackages="com.czxy.crm.service") @PropertySource("classpath:db.properties") @EnableTransactionManagement public class SpringConfiguration3 { /** * 获得properties文件中内容,并注入对应变量 */ @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; /** * 配置数据源 * @return */ @Bean public DataSource dataSource(){ DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(driver); druidDataSource.setUrl(url); druidDataSource.setUsername(username); druidDataSource.setPassword(password); return druidDataSource; } /** * 事务管理器 * @param dataSource * @return */ @Bean public DataSourceTransactionManager txManager(DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } }
2.3.3 Spring MVC 配置
拷贝:MvcConfiguration3
修改controller所在包,并创建对应的包
修改视图解析器的前后缀
package com.czxy.crm.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration @ComponentScan(basePackages="com.czxy.crm.controller") @EnableWebMvc public class MvcConfiguration3 implements WebMvcConfigurer { /** * 视图解析器 * @return */ @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp("/WEB-INF/jsp/",".jsp"); } }
2.3.4 Web 配置
拷贝:WebInitializer3
package com.czxy.crm.config; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.DispatcherServlet; import javax.servlet.FilterRegistration; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; public class WebInitializer3 implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { //1 配置spring工厂 AnnotationConfigWebApplicationContext application = new AnnotationConfigWebApplicationContext(); // 注册所有的配置类 application.register(MyBatisConfiguration3.class); application.register(SpringConfiguration3.class); application.register(MvcConfiguration3.class); //2 post中文乱码 FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("encoding", new CharacterEncodingFilter("UTF-8")); encodingFilter.addMappingForUrlPatterns(null, true, "/*"); //3 核心控制器 ServletRegistration.Dynamic mvcServlet = servletContext.addServlet("springmvc", new DispatcherServlet(application)); mvcServlet.addMapping("*.action"); //普通项目 //mvcServlet.addMapping("/"); //RESTFul项目 mvcServlet.setLoadOnStartup(2); //tomcat启动时,执行servlet的初始化方法 } }
2.4 数据库初始初始化
2.4.1 建表语句
/
CREATE DATABASE ssm_crm; USE ssm_crm; /*创建客户表*/ CREATE TABLE cst_customer ( cust_id BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '客户编号(主键)', cust_name VARCHAR(32) NOT NULL COMMENT '客户名称(公司名称)', cust_source VARCHAR(32) DEFAULT NULL COMMENT '客户信息来源(外键字典表)', cust_industry VARCHAR(32) DEFAULT NULL COMMENT '客户所属行业', cust_level VARCHAR(32) DEFAULT NULL COMMENT '客户级别(外键字典表)', cust_address VARCHAR(128) DEFAULT NULL COMMENT '客户联系地址', cust_phone VARCHAR(64) DEFAULT NULL COMMENT '客户联系电话', PRIMARY KEY (cust_id) ) ENGINE=INNODB AUTO_INCREMENT=94 DEFAULT CHARSET=utf8; /*创建联系人表*/ CREATE TABLE cst_linkman ( lkm_id BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT '联系人编号(主键)', lkm_name VARCHAR(16) DEFAULT NULL COMMENT '联系人姓名', lkm_gender VARCHAR(10) 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_position VARCHAR(16) DEFAULT NULL COMMENT '联系人职位', lkm_memo VARCHAR(512) DEFAULT NULL COMMENT '联系人备注', lkm_cust_id BIGINT(32) NOT NULL COMMENT '客户id(外键)', 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; /*创建数据字典表*/ 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; /*为字典表插入数据 */ 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), ('2','001','客户行业','电子商务',NULL,2,'1',NULL), ('3','001','客户行业','医药',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), ('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), ('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), ('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); /*客户表数据*/ INSERT INTO cst_customer VALUES ('1', '传智教育', '6', '1', '23', '北京市昌平区建材城西路金燕龙办公楼一层', '010-66668888'); INSERT INTO cst_customer VALUES ('2', '传智专修学院', '6', '1', '23', '江苏省宿迁市沭阳县北京南路999号', '0527-80961111'); INSERT INTO cst_customer VALUES ('3', '京西集团', '7', '2', '23', '京西玉泉山', '010-65085588'); INSERT INTO cst_customer VALUES ('4', '修正药业', '7', '3', '22', '北京市昌平区北七家镇', '010-68909090'); /*联系人表数据*/ INSERT INTO cst_linkman VALUES ('1', '梁老师', 'male', '0527-82930000', '1388888888', 'zzz@czxy.com', '宣传部负责人', '很负责', '1'); INSERT INTO cst_linkman VALUES ('2', '汪老师', 'female', '0527-8290000', '1377777777', 'lll@czxy.com', '全国统一咨询中心', '很负责', '1'); INSERT INTO cst_linkman VALUES ('3', '高工', 'male', '010-82930000', '1399999999', 'bggg@163.com', '传智专修学院宣传中心', '宣传责任人', '2'); INSERT INTO cst_linkman VALUES ('4', '刘管', 'female', '0527-82935100', '1366666666', 'gg@czxy.com', '管理专员', '管理', '2'); INSERT INTO cst_linkman VALUES ('5', '李总', 'male', '021-89986886', '1355555555', 'lz@zongli.com', '总经理', '企划负责人', '3'); INSERT INTO cst_linkman VALUES ('6', '王董', 'male', '021-80980990', '1333333333', 'wd@zongli.com', '董事长', '企业老大', '3'); INSERT INTO cst_linkman VALUES ('7', '孙老板', 'male', '010-80980990', '1322222222', 'slb@xunta.com', '老板', '一把手', '4'); INSERT INTO cst_linkman VALUES ('8', '陈秘书', 'female', '010-80980990', '1311111111', 'cms@xunta.com', '秘书', '二把手', '4');
2.4.2 字典表
开发过程中,页面中会出现固定值。例如:客户来源、客户所属行业、客户级别 等。
存在的问题?随着项目的不断壮大,此类数据的维护难度,将几何倍的增长。
解决方案:项目开发中,我们一般采用字典表进行处理。
什么是字典表?
用于存放系统基本参数的表。也就是将客户来源等信息抽取到表中。
例如:客户来源、客户所属行业、客户级别 等。
问题:独立的表越多,维护成本也将大大提升。
优化:创建数据字典表base_dict,用于存放此类所有数据。
2.4.3 客户表与字典表的关系
根据图片分析,字典表和客户表之间关系是一对多关系。
2.5 JavaBean
2.5.1 BaseDict
@Table(name="base_dict") public class BaseDict { // 编号 @Id private String dictId; //数据字典类别代码 private String dictTypeCode; //数据字典类别名称 private String dictTypeName; //数据字典项目名称 private String dictItemName; //数据字典项目 private String dictItemCode; //排序字段 private String dictSort; //状态 0 停用 1 启用 private String dictEnable; // 备注 private String dictMemo; //getter和setter }
2.5.2 Customer
@Table(name="cst_customer") public class Customer { // 主键 @Id private Long custId; // 客户名称 private String custName; // 客户来源 private String custSource; // 客户行业 private String custIndustry; // 客户级别 private String custLevel; // 客户地址 private String custAddress; // 客户电话 private String custPhone; // 客户来源数据字典对象(typeCode=002) private BaseDict custSourceBaseDict; // 客户行业数据字典对象(typeCode=001) private BaseDict custIndustryBaseDict; // 客户级别数据字典对象(typeCode=006) private BaseDict custLevelBaseDict; //getter 和 setter }
2.5.3 LinkMan
@Table(name="cst_linkman") public class LinkMan { // 主键 @Id private Long lkmId; // 名称 private String lkmName; // 性别 private String lkmGender; // 电话 private String lkmPhone; // 移动电话 private String lkmMobile; // 邮箱 private String lkmEmail; // 职位 private String lkmPosition; // 备注 private String lkmMemo; // 客户的对象 private Customer customer; @Column(name="lkm_cust_id") private Long custId; //所属客户主键信息(及联系人的外键) //getter 和 setter }
2.6 Mapper
2.6.1 BaseDictMapper
package com.czxy.crm.mapper; import com.czxy.crm.domain.BaseDict; import tk.mybatis.mapper.common.Mapper; /** * @author 桐叔 * @email liangtong@itcast.cn */ public interface BaseDictMapper extends Mapper<BaseDict> { } 2.6.2 CustomerMapper package com.czxy.crm.mapper; import com.czxy.crm.domain.Customer; import tk.mybatis.mapper.common.Mapper; /** * @author 桐叔 * @email liangtong@itcast.cn */ public interface CustomerMapper extends Mapper<Customer> { }
2.6.2 CustomerMapper
package com.czxy.crm.mapper; import com.czxy.crm.domain.Customer; import tk.mybatis.mapper.common.Mapper; /** * @author 桐叔 * @email liangtong@itcast.cn */ public interface CustomerMapper extends Mapper<Customer> { }
2.6.3 LinkManMapper
package com.czxy.crm.mapper; import com.czxy.crm.domain.LinkMan; import tk.mybatis.mapper.common.Mapper; /** * @author 桐叔 * @email liangtong@itcast.cn */ public interface LinkManMapper extends Mapper<LinkMan> { }
//