一、搭建CRM项目环境
1、使用system登录,创建CRM数据库用户和CRM客户表
(1)、创建用户
(2)、指定用户的角色
(3)、赋予用户权限
2、创建继承common-parent的maven工程
3、编写web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>crm</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 初始化spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置webservice服务的servlet -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>
4、编写spring配置文件和log4j日志文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<!-- 连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
<property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.37.128:1521:ORCL" />
<property name="user" value="crm_64" />
<property name="password" value="crm_64" />
</bean>
<!-- spring整合JPA -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 指定扫描的包,存放实体类 -->
<property name="packagesToScan" value="com.itheima.crm.domain" />
<!-- 指定持久层提供者为Hibernate -->
<property name="persistenceProvider">
<bean class="org.hibernate.ejb.HibernatePersistence" />
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<!-- 自动建表 -->
<property name="generateDdl" value="true" />
<property name="database" value="ORACLE" />
<property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
<property name="showSql" value="true" />
</bean>
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
</property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- 组件扫描 @Server @Controller @Repository -->
<context:component-scan base-package="com.itheima.crm.service"/>
<!-- 事务注解支持 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 整合 spring data jpa-->
<jpa:repositories base-package="com.itheima.crm.dao" />
</beans>
5、创建domain实体类
package com.itheima.crm.domain;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
* @description:客户信息表
*
*/
@Entity
@Table(name = "T_CUSTOMER")
public class Customer {
@Id
@GeneratedValue()
@Column(name = "C_ID")
private Integer id; // 主键id
@Column(name = "C_USERNAME")
private String username; // 用户名
@Column(name = "C_PASSWORD")
private String password; // 密码
@Column(name = "C_TYPE")
private Integer type; // 类型
@Column(name = "C_BRITHDAY")
@Temporal(TemporalType.DATE)
private Date birthday; // 生日
@Column(name = "C_SEX")
private Integer sex; // 性别
@Column(name = "C_TELEPHONE")
private String telephone; // 手机
@Column(name = "C_COMPANY")
private String company; // 公司
@Column(name = "C_DEPARTMENT")
private String department; // 部门
@Column(name = "C_POSITION")
private String position; // 职位
@Column(name = "C_ADDRESS")
private String address; // 地址
@Column(name = "C_MOBILEPHONE")
private String mobilePhone; // 座机
@Column(name = "C_EMAIL")
private String email; // 邮箱
@Column(name = "C_Fixed_AREA_ID")
private String fixedAreaId; // 定区编码
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getMobilePhone() {
return mobilePhone;
}
public void setMobilePhone(String mobilePhone) {
this.mobilePhone = mobilePhone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFixedAreaId() {
return fixedAreaId;
}
public void setFixedAreaId(String fixedAreaId) {
this.fixedAreaId = fixedAreaId;
}
}
6、添加tomcat服务器,修改端口号,放入crm项目,启动自动建表
7、导入customer表数据
insert into T_CUSTOMER
(C_ID,
C_ADDRESS,
C_BRITHDAY,
C_COMPANY,
C_DEPARTMENT,
C_EMAIL,
C_FIXED_AREA_ID,
C_MOBILEPHONE,
C_PASSWORD,
C_POSITION,
C_SEX,
C_TELEPHONE,
C_TYPE,
C_USERNAME)
values
(1,
'北京市海淀区建材城西路金燕龙办公楼一层',
to_date('01-07-1998', 'dd-mm-yyyy'),
'A公司',
'A部门',
'aaa@163.com',
'dq001',
'01012345671',
'123456',
'经理',
1,
'13112345678',
1,
'张三');
insert into T_CUSTOMER
(C_ID,
C_ADDRESS,
C_BRITHDAY,
C_COMPANY,
C_DEPARTMENT,
C_EMAIL,
C_FIXED_AREA_ID,
C_MOBILEPHONE,
C_PASSWORD,
C_POSITION,
C_SEX,
C_TELEPHONE,
C_TYPE,
C_USERNAME)
values
(2,
'北京市海淀区建材城西路育新花园9号楼111',
to_date('01-07-1998', 'dd-mm-yyyy'),
'B公司',
'B部门',
'bbb@163.com',
'dq002',
'01012345672',
'123456',
'经理',
1,
'13212345678',
1,
'李四');
insert into T_CUSTOMER
(C_ID,
C_ADDRESS,
C_BRITHDAY,
C_COMPANY,
C_DEPARTMENT,
C_EMAIL,
C_FIXED_AREA_ID,
C_MOBILEPHONE,
C_PASSWORD,
C_POSITION,
C_SEX,
C_TELEPHONE,
C_TYPE,
C_USERNAME)
values
(3,
'北京市海淀区中关村海龙大厦1111',
to_date('01-07-1998', 'dd-mm-yyyy'),
'C公司',
'C部门',
'ccc@163.com',
'dq001',
'01012345673',
'123456',
'经理',
1,
'13312345678',
1,
'王五');
insert into T_CUSTOMER
(C_ID,
C_ADDRESS,
C_BRITHDAY,
C_COMPANY,
C_DEPARTMENT,
C_EMAIL,
C_FIXED_AREA_ID,
C_MOBILEPHONE,
C_PASSWORD,
C_POSITION,
C_SEX,
C_TELEPHONE,
C_TYPE,
C_USERNAME)
values
(4,
'北京市海淀区中关村软件园国际软件大厦112',
to_date('01-07-1998', 'dd-mm-yyyy'),
'D公司',
'D部门',
'ddd@163.com',
null,
'01012345674',
'123456',
'经理',
1,
'3412345678',
1,
'赵六');
二、开发服务代码
1、创建服务层接口
package com.itheima.crm.service;
import java.util.List;
import javax.jws.WebService;
import com.itheima.crm.domain.Customer;
/**
* webService服务接口
* @author Administrator
*
*/
@WebService
public interface CustomerService {
/**
* 查询所有客户
* @return
*/
public List<Customer> findAll();
/**
* 查询未关联到定区的客户数据
* @return
*/
public List<Customer> findCustomerNotAssociation();
/**
* 查询已经关联到指定定区的客户数据
* @param fixedAreaId
* @return
*/
public List<Customer> findCustomersHasAssociation(String fixedAreaId);
}
2、编写实现类
package com.itheima.crm.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.itheima.crm.dao.CustomerDao;
import com.itheima.crm.domain.Customer;
import com.itheima.crm.service.CustomerService;
/**
* webService服务实现类
*
* @author Administrator
*
*/
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerDao dao;
/**
* 查询所有客户
*
* @return
*/
public List<Customer> findAll() {
return dao.findAll();
}
/**
* 查询未关联到定区的客户数据
*
* @return
*/
public List<Customer> findCustomerNotAssociation() {
return dao.findByFixedAreaIdIsNull();
}
/**
* 查询已经关联到指定定区的客户数据
*
* @param fixedAreaId
* @return
*/
public List<Customer> findCustomersHasAssociation(String fixedAreaId) {
return dao.findByFixedAreaId(fixedAreaId);
}
}
3、编写Dao层代码
package com.itheima.crm.dao;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import com.itheima.crm.domain.Customer;
public interface CustomerDao extends JpaRepository<Customer, Integer> {
/**
* 查询未关联到定区的客户数据
* @return
*/
public List<Customer> findByFixedAreaIdIsNull();
/**
* 查询已经关联到指定定区的客户数据
*
* @param fixedAreaId
* @return
*/
public List<Customer> findByFixedAreaId(String fixedAreaId);
}
4、在spring配置文件中引入webservice约束
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
5、在spring配置文件中配置注册webservice
<!-- 注册webservice服务 -->
<jaxws:server id="customerService" address="/customer">
<jaxws:serviceBean>
<ref bean="customerServiceImpl"/><!-- 此处bean属性的值是service层实现类的id值 -->
</jaxws:serviceBean>
</jaxws:server>
6、运行项目,测试发布是否正常
三、定区关联客户
1、定区页面点击按钮弹出的关联客户窗口
2、使用wsimport命令生成crm项目的客户端代码,只保留接口和实体类
G:\apache-cxf-3.0.3\apache-cxf-3.0.3\bin>wsimport -s . http://localhost:8081/crm/webservice/customer?wsdl
3、将生成的接口和实体类放入bos-util 项目中
4、在management_web项目中配置webservice客户端代码
<!-- 注册CRM服务的客户端代理对象,用于通过WebService访问CRM项目 -->
<jaxws:client id="crmClient" address="http://localhost:8081/crm/webservice/customer"
serviceClass="com.itheima.crm.service.CustomerService">
</jaxws:client>
5、调整定区关联客户页面关联窗口
(1)、点击关联客户按钮弹出的事件
function doAssociations(){
//获取当前数据表格选中的行
var rows = $("#grid").datagrid("getSelections");
if(rows.length != 1){
//弹出提示
$.messager.alert("提示信息","请选择一个定区来进行操作","warning");
}else{
$('#customerWindow').window('open');
//清空下拉框中的客户数据
$("#noassociationSelect").empty();
$("#associationSelect").empty();
/**
* 发送ajax请求,请求后台系统的Action,在Action中注入CRM代理对象,调用代理对象
* 实现访问CRM服务获取客户信息,在Action中将获取的客户数据转为json并写回当前浏览器
*/
//加载左侧未关联定区的客户
$.post("../../fixedAreaAction_findCustomersNoAssociation.action",function(data){
//解析服务器响应的json数据,展示到下拉框中
for(var i = 0; i < data.length; i++){
var json = data[i];
var id = json.id;
var tel = json.telephone;
var username = json.username + "[ " + tel + " ]";
$("#noassociationSelect").append("<option value='"+id+"'>"+username+"</option>");
}
},'json');
//加载右侧已经关联到当前定区的客户列表
$.post("../../fixedAreaAction_findCustomersHasAssociation.action",{"id":rows[0].id},function(data){
//解析服务器响应的json数据,展示到下拉框中
for(var i = 0; i < data.length; i++){
var json = data[i];
var id = json.id;
var tel = json.telephone;
var username = json.username + "[ " + tel + " ]";
$("#associationSelect").append("<option value='"+id+"'>"+username+"</option>");
}
},'json');
}
}
(2)、编写FixedAreaAction中代码
//注入CRM服务的客户端代理对象
@Autowired
private CustomerService crmProxy;
/**
* 查询未关联到定区的客户数据
*/
@Action(value="fixedAreaAction_findCustomersNoAssociation")
public String findCustomersNoAssociation(){
List<Customer> listNo = crmProxy.findCustomerNotAssociation();
this.list2json(listNo, new String[]{});
return NONE;
}
/**
* 查询已经关联到指定定区的客户数据
* @return
*/
@Action(value="fixedAreaAction_findCustomersHasAssociation")
public String findCustomersHasAssociation(){
List<Customer> listHas = crmProxy.findCustomersHasAssociation(model.getId());
this.list2json(listHas, new String[]{});
return NONE;
}
(3)、页面效果图
6、实现关联界面客户信息左右移动的操作
<!-- 为以上两个按钮绑定点击事件 -->
<script type="text/javascript">
$(function(){
//页面加载完成,为向右按钮绑定点击事件
$("#toRight").click(function(){
$("#associationSelect").append($("#noassociationSelect option:selected"));
});
//页面加载完成,为向左按钮绑定点击事件
$("#toLeft").click(function(){
$("#noassociationSelect").append($("#associationSelect option:selected"));
});
});
</script>
7、完成客户关联的操作
(1)、为窗口中“关联客户”按钮绑定点击事件,提交表单
<tr>
<td colspan="3">
<a id="associationBtn" href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'">关联客户</a>
<!-- 为关联客户按钮绑定点击事件 -->
<script type="text/javascript">
$(function(){
$("#associationBtn").click(function(){
//提交表单之前,需要选中右侧下拉框中所有的option
$("#associationSelect option").attr("selected","selected");
//为隐藏于(存放的是当前选中的定区的id)赋值
var rows = $("#grid").datagrid("getSelections");
$("#customerFixedAreaId").val(rows[0].id);
//提交表单
$("#customerForm").submit();
});
});
</script>
</td>
</tr>
(2)、编写Action中关联客户的代码
/**
* 将客户关联到定区
*/
@Action(value="fixedAreaAction_assignCustomers2FixedArea",results={
@Result(name="success", type="redirect", location="/pages/base/fixed_area.html")
})
public String assignCustomers2FixedArea(){
//获取当前定区id
String fixedId = model.getId();
crmProxy.assingCustomers2FixedArea(fixedId, customerIds);
return SUCCESS;
}
(3)、扩展CRM项目中Service层代码
/**
* 关联客户到指定定区
* @param fixedAreaId
*/
public void assingCustomers2FixedArea(String fixedAreaId, Integer[] customerIds){
//将当前定区关联的所有客户的逻辑外键改为null
dao.setFixedAreaIdIsNull(fixedAreaId);
//重新建立关联,即将当前选中的客户添加到当前的分区中
if(customerIds != null && customerIds.length > 0){
for (Integer id : customerIds) {
dao.assignCustomer2FixedArea(fixedAreaId, id);
}
}
}
(4)、扩展CRM项目中的dao层代码
/**
* 将客户关联的定区为指定id的定区置空
* @param fixedAreaId
*/
@Query("update Customer set fixedAreaId = null where fixedAreaId = ?")
@Modifying
public void setFixedAreaIdIsNull(String fixedAreaId);
/**
* 关联客户到指定的定区
* @param fixedAreaId
* @param customerId
*/
@Query("update Customer set fixedAreaId = ? where id = ?")
@Modifying
public void assignCustomer2FixedArea(String fixedAreaId, Integer customerId);
(5)、重新使用wsimport命令生成客户端的接口代码
注意:服务端代码更新之后,必须重新生成客户端的服务端接口