9、客户模块之按条件查询(异步查询客户级别后台代码编写)

1. 使用异步的方式加载客户级别和客户的来源
        * 前端使用JQuery的ajax技术
        * 后端使用fastjson的jar包
            * 导入fastjson的开发jar包fastjson-1.2.8.jar
            * String s = JSON.toJSONString(集合)
            * String s = JSON.toJSONString(对象)
        
        * 如果List集合中存入相同引用的对象
            * fastjson默认的情况下是进行循环检测的,去除掉死循环调用的方式
            * 可以使用JSON.toJSONString(p,SerializerFeature.DisableCircularReferenceDetect) 去除循环检测,但是就会出现死循环的效果
            * 最后可以使用注解:@JSONField(serialize=false)对指定的属性不转换成json

 

/crm/src/com/louis/web/action/DictAction.java

/**
 * 字典控制器
 * @author Administrator
 */
public class DictAction extends ActionSupport implements ModelDriven<Dict>{

    private static final long serialVersionUID = -82264029685635742L;
    
    private Dict dict = new Dict();

    public Dict getModel() {
        return dict;
    }
}

/crm/src/applicationContext.xml

    <!-- 字典 -->
    <bean id="dictAction" class="com.louis.web.action.DictAction" scope="prototype">
        <property name="dictService" ref="dictService"/>
    </bean>
    
    <bean id="dictService" class="com.louis.service.DictServiceImpl">
        <property name="dictDao" ref="dictDao"/>
    </bean>
    
    <bean id="dictDao" class="com.louis.dao.DictDaoImpl">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

 

/crm/src/struts.xml

<!-- 字典模块 -->
        <action name="dict_*" class="dictAction" method="{1}"></action>

/crm/src/com/louis/web/action/DictAction.java

package com.louis.web.action;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.louis.domain.Dict;
import com.louis.service.DictService;
import com.louis.utils.FastJsonUtil;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 字典控制器
 * @author Administrator
 */
public class DictAction extends ActionSupport implements ModelDriven<Dict>{

    private static final long serialVersionUID = -82264029685635742L;
    
    private Dict dict = new Dict();

    public Dict getModel() {
        return dict;
    }

    
    private DictService dictService;
    public void setDictService(DictService dictService) {
        this.dictService = dictService;
    }

    
    /**
     * 通过字段的type_code值,查询客户级别或者客户的来源
     * @return
     */
    public String findByCode(){
        // 调用业务层
        List<Dict> list = dictService.findByCode(dict.getDict_type_code());
        // List<Dict> list = dictService.findByCode("006");
        
        // 使用fastjson,把list转换成json字符串
        String jsonString = FastJsonUtil.toJSONString(list);
        // 把json字符串写到浏览器
        HttpServletResponse response = ServletActionContext.getResponse();
        // 输出
        FastJsonUtil.write_json(response, jsonString);
        
        return NONE;
    }
}

/crm/src/com/louis/service/DictServiceImpl.java

package com.louis.service;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.louis.dao.DictDao;
import com.louis.domain.Dict;

/**
 * 字典
 * @author Administrator
 */
@Transactional
public class DictServiceImpl implements DictService {
    
    private DictDao dictDao;
    public void setDictDao(DictDao dictDao) {
        this.dictDao = dictDao;
    }
    
    /**
     * 通过客户类别编码查询字段
     */
    public List<Dict> findByCode(String dict_type_code) {
        return dictDao.findByCode(dict_type_code);
    }
    

}

/crm/src/com/louis/dao/DictDaoImpl.java

package com.louis.dao;

import java.util.List;

import org.springframework.orm.hibernate5.support.HibernateDaoSupport;

import com.louis.domain.Dict;

/**
 * 持久层
 * @author Administrator
 */
public class DictDaoImpl extends HibernateDaoSupport implements DictDao {
    
    /**
     * 通过客户类别编码查询字段
     */
    public List<Dict> findByCode(String dict_type_code) {
        return (List<Dict>) this.getHibernateTemplate().find("from Dict where dict_type_code = ?", dict_type_code);
    }

}

测试

 

 

posted on 2017-11-01 08:54  Michael2397  阅读(337)  评论(0编辑  收藏  举报

导航