crm开发(基于ssh)(五)

1 信息查询

(1)多条件组合查询

-拼接hql语句

-使用离线对象

2 添加数据字典表

(1)改造添加客户功能

3 统计分析

(1)调用普通sql实现

(2)结果处理操作

4 使用ssh注解整合

(1)spring注解创建对象、注入属性

(2)hibernate配置映射注解

 联系人信息查询

1 点击 联系人信息查询 超链接的时候,到查询页面

(1)在查询页面中,选择客户,根据客户进行查询

//到联系人添加页面
    public String toSelectPage(){
        //查询所有客户,把传递到页面到下拉列表中
        List<Customer> list = customerService.findAll();
        ServletActionContext.getRequest().setAttribute("list", list);
        return "toSelectPage";
    }

页面中显示

<td>所属客户</td>
                                <td>
                                    <select name="customer.cid">
                                        <option value="0">--请选择--</option>
                                        <c:forEach items="${list }" var="cus">
                                            <option value="${cus.cid }">${cus.custName}</option>
                                        </c:forEach>
                                    </select>
                                
                                </td>

2 在查询页面中,输入值,提交表单到action,查询数据库得到结果

//多条件组合查询-hql语句拼接方式实现
    public List<LinkMan> findCondition(LinkMan linkMan) {
        String hql = "from LinkMan where 1=1";
        List<Object> p = new ArrayList<Object>();
        //判断条件是否为空
        if(linkMan.getLkmName() != null && !"".equals(linkMan.getLkmName())){
            hql += " and lkmName=?";
            p.add(linkMan.getLkmName());
        }
        //判断是否选择客户
        if(linkMan.getCustomer().getCid() != null && linkMan.getCustomer().getCid() > 0){
            //判断客户里面cid值
            hql += " and customer.cid=?";
            p.add(linkMan.getCustomer().getCid());
        }
        
        return (List<LinkMan>) this.getHibernateTemplate().find(hql, p.toArray());
    }

重点语句在于

hql += " and customer.cid=?";

3 使用离线对象实现查询

//多条件组合查询-离线对象方式实现
    public List<LinkMan> findCondition(LinkMan linkMan) {
        //创建离线对象
        DetachedCriteria criteria = DetachedCriteria.forClass(LinkMan.class);
        if(linkMan.getLkmName() != null && !"".equals(linkMan.getLkmName())){
            criteria.add(Restrictions.eq("lkmName", linkMan.getLkmName()));
        }
        if(linkMan.getCustomer() != null && linkMan.getCustomer().getCid() > 0){
            criteria.add(Restrictions.eq("customer.cid", linkMan.getCustomer().getCid()));
        }
        
        return (List<LinkMan>) this.getHibernateTemplate().findByCriteria(criteria);
    }

添加数据字典表

1 什么是数据字典表

(1)存储基础数据

-比如添加客户信息的时候,添加客户级别,这个级别不能随便输入,把客户级别存到数据字典表里面,添加的时候,查询数据字典所有记录显示

(2)码表

2 数据字典表 和 客户表之间关系是一对多关系

(1)数据字典表是一

(2)客户表示 多

-一个级别里面可以被多个客户拥有,一个客户只能是一个级别。

(3)让数据字典表对应一个实体类

-配置数据字典表和客户表关系

改造添加客户功能

1 创建数据字典表实体类,配置映射

public class Dict {
    
    private String did;
    private String dname;
    public String getDid() {
        return did;
    }
    public void setDid(String did) {
        this.did = did;
    }
    public String getDname() {
        return dname;
    }
    public void setDname(String dname) {
        this.dname = dname;
    }
    
}

配置文件中

<class name="cn.itcast.entity.Dict" table="t_dict">
        <id name="did" column="did">
            <!-- 
            native:主键自动增长,类型必须是int类型
            实体类did是string,可以写uuid
            还要一个值,increment
             -->
            <generator class="increment">
            </generator>
        </id>
        <property name="dname" column="dname"></property>
        
</class>

2 配置数据字典和客户映射关系

(1)需求:根据客户查询级别,没有根据级别查询客户的需求

(2)只需要在客户实体类表示所属级别

//在客户实体类表示所属级别
    private Dict dictCustLevel;
    
    public Dict getDictCustLevel() {
        return dictCustLevel;
    }

    public void setDictCustLevel(Dict dictCustLevel) {
        this.dictCustLevel = dictCustLevel;
    }

(3)在映射文件中表示关系

-在客户映射文件中,配置所属级别就可以了

<!-- 所属级别 -->
<many-to-one name="dictCustLevel" class="cn.itcast.entity.Dict" column="custLevel"></many-to-one>

t_customer的建表语句如下所示:

 CREATE TABLE `t_customer` (
  `cid` int(11) NOT NULL AUTO_INCREMENT,
  `custName` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `custSource` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `custPhone` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `custMobile` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `custLevel` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`cid`),
  KEY `FKo6oqtbbjmu6890to85xbpymcd` (`custLevel`),
  CONSTRAINT `FKo6oqtbbjmu6890to85xbpymcd` FOREIGN KEY (`custLevel`) REFERENCES `t_dict` (`did`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |

3 功能分析

(1)到添加客户页面中,在下拉列表中把所有级别显示

(2)点击保存,把数据添加到数据库里面

 -使用模型驱动封装得到值

-列表页面中修改位置

统计分析

 1 系统中可能有复杂查询操作,使用hibernate不能实现,需要调用普通sql语句实现

(1)在dao里面使用hibernate模板,使用hibernate模板调用普通sql

select count(*) as num,custSource from t_customer
group by custSource

(2)根据客户级别统计

只使用客户表,客户级别id值不是名称,但是显示客户级别名称

-让客户表和数据字典表多表查询操作

sql

select c.num,d.dname 
from (select count(*)as num,custLevel from t_customer group by custLevel) c,
t_dict d
where c.custLevel=d.did;

2 统计查询之后,返回多条记录,每条记录里面有两个值,返回list集合,

但是list里面泛型没有实体类封装,让list里面的泛型是map集合

 sqlquery里面有一个方法可以把查询结果进行转换,setResultTransformer()

根据客户级别统计

1 在dao里面调用普通sql语句

//根据客户级别统计
    public List findCountLevel() {
        //获取session对象
        Session session = this.getSessionFactory().getCurrentSession();
        //创建SQLQuery对象
        SQLQuery sqlQuery = session.createSQLQuery("select c.num,d.dname from (select count(*)as num,"
                + "custLevel from t_customer group by custLevel) c,t_dict d where c.custLevel=d.did");
        //得到结果
        //转换成map结果
        sqlQuery.setResultTransformer(Transformers.aliasToBean(HashMap.class));
        List list = sqlQuery.list();
        return list;
    }

返回list集合,list集合中每部分是数组形式。

总结crm

1 struts2上传

(1)表单满足三个要求

-提交方式是post,enctype属性值设置,表单里面要有文件上传项和name属性

(2)在action直接获取上传文件和文件名称

(3)上传逻辑:

-在服务器上创建文件

-把上传文件复制到服务器文件里面

(4)struts2上传文件大小限制2M

2 多对多配置方式

(1)使用hibernate配置多对多,缺陷:第三张表只有两个id值

(2)拆分成两个一对多实现

3 离线对象使用

(1)分页查询

(2)多条件组合查询

-离线对象

-hql实现

-拼接hql语句,使用list集合设置参数值

4 调用底层sql实现

(1)得到sessionFactory对象,得到session对象

(2)得到session里面的方法创建SQLQuery对象

(3)返回结果的结构转换

5 配置实体类之间关系的时候,不需要两端都进行配置,根据具体需求实现

6 BaseDao抽取

(1)创建basedao接口,使用大写字母T,代表任意类型,定义crud方法

(2)创建basedao接口实现类,实现crud操作

-术语,在构造方法里面实现代码

(3)在具体功能的接口继承basedao接口

(4)在具体功能实现类继承basedao实现类

-在接口和实现类里面,把crud方法去掉。

原文地址:https://www.cnblogs.com/liaoxiaolao/p/9959829.html