客户管理模块(保存查询客户)| CRM客户关系管理系统项目实战二(Struts2+Spring+Hibernate)解析+源代码

引入数据字典,配置一对多的关系
异步加载
(JSON的使用,将list集合封装为json的数据,然后删除部分数据,JSON的数据发送到前端,前端页面获取对应的数据,遍历json的数据)
数据字典(下拉列表)
添加事务、客户查询出来并分页处理
创建分页订单PageBean

一、客户管理:准备工作

1、创建表

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_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
  `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
  PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

在这里插入图片描述

2、创建实体和映射

创建Customer实体

package com.itzheng.crm.domain;
/*
 * 客户管理的实体
 */
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;
	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 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_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 Customer() {
		// TODO Auto-generated constructor stub
	}
	public Customer(Long cust_id, String cust_name, String cust_source, String cust_industry, String cust_level,
			String cust_phone, String cust_mobile) {
		super();
		this.cust_id = cust_id;
		this.cust_name = cust_name;
		this.cust_source = cust_source;
		this.cust_industry = cust_industry;
		this.cust_level = cust_level;
		this.cust_phone = cust_phone;
		this.cust_mobile = cust_mobile;
	}
	@Override
	public String toString() {
		return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_source=" + cust_source
				+ ", cust_industry=" + cust_industry + ", cust_level=" + cust_level + ", cust_phone=" + cust_phone
				+ ", cust_mobile=" + cust_mobile + "]";
	}
}

创建实体映射在domian下创建Customer.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.itzheng.crm.domain.Customer" table="cst_customer">
		<!-- 建立类中的属性与表中的主键对应 -->
		<id name="cust_id" column="cust_id">
			<!-- 主键生成策略 -->
			<generator class="native" />
		</id>
		<!-- 建立类中的普通的属性和表的字段的对应映射 -->
		<property name="cust_name" column="cust_name" />
		<property name="cust_source" column="cust_source"/>
		<property name="cust_industry" column="cust_industry" />
		<property name="cust_level" column="cust_level" />
		<property name="cust_phone" column="cust_phone" />
		<property name="cust_mobile" column="cust_mobile" />
	</class>
</hibernate-mapping>

3、创建Action

package com.itzheng.crm.web.action;
import com.itzheng.crm.domain.Customer;
import com.itzheng.crm.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/*
 * 客户管理的Action的类
 */
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
	// 模型驱动使用的对象
	private Customer customer = new Customer();
	@Override
	public Customer getModel() {
		// TODO Auto-generated method stub
		return customer;
	}
	//注入Service
	private CustomerService customerService;
	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
}

4、创建Service

CustomerService

package com.itzheng.crm.service;
/*
 * 客户管理Service的接口
 */
public interface CustomerService {
}

CustomerServiceImpl

package com.itzheng.crm.service.impl;
import com.itzheng.crm.dao.CustomerDao;
import com.itzheng.crm.service.CustomerService;
/*
 * 客户管理Service的接口的实现类
 */
public class CustomerServiceImpl implements CustomerService {
	//注入客户的DAO
	private CustomerDao customerDao;
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}
}

5、创建DAO

CustomerDao

package com.itzheng.crm.dao;
/*
 * 客户管理的DAO的接口
 */
public interface CustomerDao {
}

创建CustomerDaoImpl 继承HibernateDaoSupport

package com.itzheng.crm.dao.impl;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itzheng.crm.dao.CustomerDao;
/*
 * 客户管理的DAO的实现类
 */
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
}

6、配置Action、Service、Dao

在applicationContext.xml当中

	<!-- 配置Action -->
	<bean id="customerAction" class="com.itzheng.crm.web.action.CustomerAction" scope="prototype">
		<property name="customerService" ref="customerService"/>
	</bean>
	
	<!-- 配置Service -->
	<bean id="customerService" class="com.itzheng.crm.service.impl.CustomerServiceImpl">
		<property name="customerDao" ref="customerDao"/>
	</bean>
	
	<!-- 配置DAO -->
	<bean id="customerDao" class="com.itzheng.crm.dao.impl.CustomerDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"/>
	</bean>

二、跳转到添加页面

1、修改左侧菜单的页面(menu.jsp)

在这里插入图片描述

2、编写Action当中的saveUI的方法

在CustomerAction当中在这里插入图片描述

3、配置Action的跳转

在struts.xml当中配置
在这里插入图片描述

跳转成功
在这里插入图片描述

三、引入数据字典

1、什么是数据字典

数据字典用来规范某些地方具体值和数据

2、创建数据字典表

SQL语句

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;

数据表
在这里插入图片描述

3、客户表和字典表的关系分析

(1)分析关系(字典表的当中每一条数据都可以对应多个不同的客户)并且客户表当中数据有好几个都来自字典表的同一条数据(将客户每一个数据与字典表建立外键关系)

在这里插入图片描述

(2)创建客户与字典表的外键

在这里插入图片描述

4、创建数据字典的实体和映射

创建实体

package com.itzheng.crm.domain;
/*
 * 数据字典的实体
 * 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;
 */
public class BaseDict {
	private String dict_id;
	private String dict_type_code;
	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;
	}
	public BaseDict() {
		// TODO Auto-generated constructor stub
	}
	public BaseDict(String dict_id, String dict_type_code, String dict_type_name, String dict_item_name,
			String dict_item_code, Integer dict_sort, String dict_enable, String dict_memo) {
		super();
		this.dict_id = dict_id;
		this.dict_type_code = dict_type_code;
		this.dict_type_name = dict_type_name;
		this.dict_item_name = dict_item_name;
		this.dict_item_code = dict_item_code;
		this.dict_sort = dict_sort;
		this.dict_enable = dict_enable;
		this.dict_memo = dict_memo;
	}
	@Override
	public String toString() {
		return "BaseDict [dict_id=" + dict_id + ", dict_type_code=" + dict_type_code + ", dict_type_name="
				+ dict_type_name + ", dict_item_name=" + dict_item_name + ", dict_item_code=" + dict_item_code
				+ ", dict_sort=" + dict_sort + ", dict_enable=" + dict_enable + ", dict_memo=" + dict_memo + "]";
	}
}

创建映射
BaseDict.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.itzheng.crm.domain.BaseDict" 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>

5、创建字典和客户的关系映射

(1)字典和客户是一对多的关系,如果查询字典数据的时候,不需要去查询客户的数据的,所以在字典这一端可以不用配置客户相关内容。————查询客户的时候显示字典的信息所以需要在客户这一端来配置字典相关内容
(2)客户和字典表是属于多对一:需要在多的一方放的是一的一方的对象,修改客户实体类Customer
package com.itzheng.crm.domain;
/*
 * 客户管理的实体
 */
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 BaseDict baseDictSource;
	private BaseDict baseDictIndustry;
	private BaseDict baseDictLevel;
	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 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 BaseDict getBaseDictSource() {
		return baseDictSource;
	}
	public void setBaseDictSource(BaseDict baseDictSource) {
		this.baseDictSource = baseDictSource;
	}
	public BaseDict getBaseDictIndustry() {
		return baseDictIndustry;
	}
	public void setBaseDictIndustry(BaseDict baseDictIndustry) {
		this.baseDictIndustry = baseDictIndustry;
	}
	public BaseDict getBaseDictLevel() {
		return baseDictLevel;
	}
	public void setBaseDictLevel(BaseDict baseDictLevel) {
		this.baseDictLevel = baseDictLevel;
	}
	public Customer() {
		// TODO Auto-generated constructor stub
	}
	public Customer(Long cust_id, String cust_name, String cust_phone, String cust_mobile, BaseDict baseDictSource,
			BaseDict baseDictIndustry, BaseDict baseDictLevel) {
		super();
		this.cust_id = cust_id;
		this.cust_name = cust_name;
		this.cust_phone = cust_phone;
		this.cust_mobile = cust_mobile;
		this.baseDictSource = baseDictSource;
		this.baseDictIndustry = baseDictIndustry;
		this.baseDictLevel = baseDictLevel;
	}
	@Override
	public String toString() {
		return "Customer [cust_id=" + cust_id + ", cust_name=" + cust_name + ", cust_phone=" + cust_phone
				+ ", cust_mobile=" + cust_mobile + ", baseDictSource=" + baseDictSource + ", baseDictIndustry="
				+ baseDictIndustry + ", baseDictLevel=" + baseDictLevel + "]";
	}

}
  • 修改的地方
    在这里插入图片描述
(3)修改客户的映射
  • Customer.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.itzheng.crm.domain.Customer" table="cst_customer">
		<!-- 建立类中的属性与表中的主键对应 -->
		<id name="cust_id" column="cust_id">
			<!-- 主键生成策略 -->
			<generator class="native" />
		</id>
		<!-- 建立类中的普通的属性和表的字段的对应映射 -->
		<property name="cust_name" column="cust_name" />
		<property name="cust_phone" column="cust_phone" />
		<property name="cust_mobile" column="cust_mobile" />
		<!-- 配置客户与字典的多对一的映射 -->
		<many-to-one name="baseDictSource" class="com.itzheng.crm.domain.BaseDict" column="cust_source"></many-to-one>
		<many-to-one name="baseDictIndustry" class="com.itzheng.crm.domain.BaseDict" column="cust_industry"></many-to-one>
		<many-to-one name="baseDictLevel" class="com.itzheng.crm.domain.BaseDict" column="cust_level"></many-to-one>
	</class>
</hibernate-mapping>
  • 更改位置
    在这里插入图片描述

6、将映射文件交给Spring

  • 在applicationContext.xml
    在这里插入图片描述

四、在点击新增客户的时候(添加一个功能)异步加载字典当中的数据

1、创建字典的Action,Service,Dao

(1) Dao和DaoImpl
package com.itzheng.crm.dao;
/*
 * 字典的Dao的接口
 */
public interface BaseDictDao {
}
package com.itzheng.crm.dao.impl;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itzheng.crm.dao.BaseDictDao;
/*
 * 字典的Dao的接口的实现类
 */
public class BaseDictDaoImpl extends HibernateDaoSupport implements BaseDictDao {
}
(2)编写Service
package com.itzheng.crm.service;
/*
 * 字典的业务层的接口
 */
public interface BaseDictService {
}
package com.itzheng.crm.service.impl;
import com.itzheng.crm.dao.BaseDictDao;
import com.itzheng.crm.service.BaseDictService;
/*
 * 字典的业务层的实现类
 */

public class BaseDictServiceImpl implements BaseDictService {
	//注入Dao
	private  BaseDictDao baseDictDao;
	public void setBaseDictDao(BaseDictDao baseDictDao) {
		this.baseDictDao = baseDictDao;
	}
}
(3)编写Action
package com.itzheng.crm.web.action;
import com.itzheng.crm.domain.BaseDict;
import com.itzheng.crm.service.BaseDictService;
/*
 * 字典的Action类
 */
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class BaseDictAction extends ActionSupport implements ModelDriven<BaseDict> {
	//模型驱动使用的对象
	private BaseDict baseDict = new BaseDict();
	@Override
	public BaseDict getModel() {
		// TODO Auto-generated method stub
		return baseDict;
	}
	//注入Service
	private BaseDictService baseDictService;
	public void setBaseDictService(BaseDictService baseDictService) {
		this.baseDictService = baseDictService;
	}
}

2、将上面创建字典的类交给Spring管理,并对应注入的属性

在这里插入图片描述

3、在页面当中使用异步加载:用户来源的数据

(1)引入JQuery的js文件

在这里插入图片描述
在这里插入图片描述

(2)在add.jsp页面上引入JQuery

在这里插入图片描述

(3)在页面加载订单时候进行异步操作(编写异步加载的函数)
  • 修改add.jsp的信息来源
    在这里插入图片描述
  • 编写异步加载的函数
    页面一加载就执行这个函数(异步加载)
    页面加载,就应该异步查询字典的数据
    加载客户的来源
    post请求当中的参数(1、请求的路径,2.参数(Struts2当中自动封装post请求的参数dict_type_code到BaseDict对象当中) 3、返回的数据4、数据的类型)
    在这里插入图片描述
(4)在BaseDictAction当中创建findByTypeCode方法
  • 将list封装为jso格式的数据
    JSONConfig:转JSON的一个配置对象
    JSONArray;将数组和list集合转换为json
    JSONObject:将对象和Map集合转成JSON
  • 引入json的jar
    在这里插入图片描述
  • 编写Action
/*
 * 根据类型名称查询字典的方法:findByTypeCode
 */
	public String findByTypeCode() throws IOException {
		System.out.println("BaseDictAction中的findByTypeCode方法执行了。。。。");
		//调用业务层查询
		List<BaseDict> list = baseDictService.findByTypeCode(baseDict.getDict_type_code());
		//将list转换成json,----jsonlib/fastjson4
		//引入jsonlib的包
		/*
		 * JSONConfig:转JSON的一个配置对象
		 * JSONArray;将数组和list集合转换为json
		 * JSONObject:将对象和Map集合转成JSON
		 */
		JsonConfig jsonconfig = new JsonConfig();
		jsonconfig.setExcludes(new String[] {"dict_sort","dict_enable","dict_memo"});//(将list当中的这些内容删除)
		JSONArray jsonArray = JSONArray.fromObject(list,jsonconfig);//将list集合转换为JSON	
		System.out.println(jsonArray.toString());	
		//将JSON打印到页面上
		ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
		ServletActionContext.getResponse().getWriter().println(jsonArray.toString());
		return NONE;
	}
(5)BaseDictServiceImpl调用Dao
	@Override
	public List<BaseDict> findByTypeCode(String dict_type_code) {
		// TODO Auto-generated method stub
		return baseDictDao.findByTypeCode(dict_type_code);
	}
(6)Dao当中
package com.itzheng.crm.dao.impl;
import java.util.List;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itzheng.crm.dao.BaseDictDao;
import com.itzheng.crm.domain.BaseDict;
public class BaseDictDaoImpl extends HibernateDaoSupport implements BaseDictDao {
	@Override
	//根据类型编码去查询字典数据
	public List<BaseDict> findByTypeCode(String dict_type_code) {
		// TODO Auto-generated method stub
		List<BaseDict> find = (List<BaseDict>) this.getHibernateTemplate().find("from BaseDict where dict_type_code = ?", dict_type_code);
		return find;
	}
}

4、在页面当中使用异步加载:客户级别以及

(1)修改add.jsp的表单所属行业

在这里插入图片描述

(2)修改add.jsp上的异步加载函数

在这里插入图片描述

(3)测试,将页面上的三个列表从字典当中查到数据填满

在这里插入图片描述

五、将表单当中数据存入到数据库当中

1、修改add.jsp

  • 修改提交路径
    在这里插入图片描述
  • 修改表单项的名称:将数据封装到Customer对象当中
    在这里插入图片描述

2、编写Action

在这里插入图片描述

3、编写CustomerServiceImpl

package com.itzheng.crm.service.impl;
import com.itzheng.crm.dao.CustomerDao;
import com.itzheng.crm.domain.Customer;
import com.itzheng.crm.service.CustomerService;
/*
 * 客户管理Service的接口的实现类
 */
public class CustomerServiceImpl implements CustomerService {
	//注入客户的DAO
	private CustomerDao customerDao;
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}
	//业务层保存客户的方法
	@Override
	public void save(Customer customer) {
		// TODO Auto-generated method stub
		customerDao.save(customer);
	}
}

4、CustomerDaoImpl

package com.itzheng.crm.dao.impl;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import com.itzheng.crm.dao.CustomerDao;
import com.itzheng.crm.domain.Customer;
/*
 * 客户管理的DAO的实现类
 */
public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
	@Override
	public void save(Customer customer) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(customer);
	}
}

5、添加事务

  • 在CustomerDaoImpl :在save对应的类上添加事务这个事务作为范围在每一个方法的开始和结束
    在这里插入图片描述
  • 测试
    在这里插入图片描述
    在这里插入图片描述

六、将客户查询出来并分页处理

1、修改menu,jsp

在这里插入图片描述

2、编写Action当中的findAll的方法

(1)创建一个工具类PageBean
package com.itzheng.crm.domain;
import java.util.List;
public class PageBean<T> {
	private Integer currPage;// 当前页数
	private Integer pageSize;// 每页显示的记录数
	private Integer totalCount;// 总记录数
	private Integer totalPage;// 总页数
	private List<T> list;// 每页查询到数据的集合
	public Integer getCurrPage() {
		return currPage;
	}
	public void setCurrPage(Integer currPage) {
		this.currPage = currPage;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public Integer getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(Integer totalCount) {
		this.totalCount = totalCount;
	}
	public Integer getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}
	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
}
(2)修改CustomerAction
  • 设置set方法接收页面传过来的分页数据当前页数以及每页显示的记录数
    在这里插入图片描述
  • 创建findAll方法查询所有的客户数据
	/*
	 * 分页查询客户的方法
	 */
	public String findAll() {
		// 接收参数:分页的参数:
		// 最好使用DetachedCriteria对象(条件查询--带分页)
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Customer.class);
		// 调用业务层查询:
		PageBean<Customer> pageBean = customerService.findByPage(detachedCriteria, currPage, pageSize);	
		ActionContext.getContext().getValueStack().push(pageBean);//放入到值栈当中
		return "findAll";
	}
(3) 在CustomerServiceImpl业务层分页查询客户的方法
@Override
	// 业务层分页查询客户的方法(页码数以及对应的集合)
	public PageBean<Customer> findByPage(DetachedCriteria detachedCriteria, Integer currPage, Integer pageSize) {
		// TODO Auto-generated method stub
		PageBean<Customer> pageBean = new PageBean<Customer>();
		// 封装当前的页数
		pageBean.setCurrPage(currPage);
		// 封装每页显示的记录
		pageBean.setPageSize(pageSize);
		// 封装总记录数
		// 调用Dao
		Integer totalCount = customerDao.findCount(detachedCriteria);
		pageBean.setTotalCount(totalCount);//这个是总数据条数
		// 封装总页数;
		Double tc = totalCount.doubleValue();
		Double num = Math.ceil(tc/pageSize);// 向上取整(总记录数除以每一页数据的数量等于总页数)
		pageBean.setTotalPage(num.intValue());		
		//封装每一页显示的数据的集合
		Integer begin = (currPage - 1) * pageSize;//计算要从几开始查询数据库当中数据		
		List<Customer> list = customerDao.findByPage(detachedCriteria,begin,pageSize);		
		pageBean.setList(list);
		return pageBean;
	}
(4)在Dao的实现类当中当中创建统计总数的方法以及对应集合的方法
// DAO 当中带条件的统计个数的方法
	@Override
	public Integer findCount(DetachedCriteria detachedCriteria) {
		// TODO Auto-generated method stub
		// select count(*) from xxx where 条件;
		DetachedCriteria setProjection = detachedCriteria.setProjection(Projections.rowCount());// 设置条件获取个数
		List<Long> list = (List<Long>) this.getHibernateTemplate().findByCriteria(setProjection);
		if (list.size() > 0) {
			return list.get(0).intValue();
		}
		return null;
	}
	@Override
	// DAO 当中分页查询客户的方法
	public List<Customer> findByPage(DetachedCriteria detachedCriteria, Integer begin, Integer pageSize) {
		// TODO Auto-generated method stub
		detachedCriteria.setProjection(null);// 情况detachedCriteria当中的查询方法
		return (List<Customer>) this.getHibernateTemplate().findByCriteria(detachedCriteria, begin, pageSize);
	}

3、页面跳转

(1)在struts.xml当中配置

在这里插入图片描述

(2)注意延迟加载的问题,在Customer.hbm.xml当中的对应映射属性是默认是延迟加载(baseDictSource,baseDictIndustry,baseDictLevel),所有直接访问的无法查询对应的数据的
  • 需要在web.xml当中配置
    在这里插入图片描述
(3)在list.jsp页面上处理分页的信息(pageBean)
  • 修改表单当中的数据以及对应faction的函数
    在这里插入图片描述
  • 页面获取pageBean的属性以及集合
    在这里插入图片描述
  • 设置分页以及触发还是提交对应的表单的页数(currPage)以及一页要显示的数据个数(pageSize)返回pageBean
    在这里插入图片描述
(4)测试

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_44757034/article/details/107462348