SpringMVC_CRM系統(tǒng)_頁面加載數(shù)據(jù)4

現(xiàn)在我們開始處理頁面的數(shù)據(jù)顯示

  1. 先填充這三個下拉框中的數(shù)據(jù)


  2. 新建service層
public interface BaseDictService {
    public List<BaseDict> queryBaseDictByDictTypeCode(String dictTypeCode) ;
}
@Service
public class BaseDictServiceImpl implements BaseDictService{
    @Autowired
    BaseDictDao baseDictDao;

    public List<BaseDict> queryBaseDictByDictTypeCode(String dictTypeCode) {
        return baseDictDao.queryBaseDictByDictTypeCode(dictTypeCode);
    }
}

  1. 在CustomerController中調(diào)用service
@Controller
public class CustomerController {
    @Autowired
    BaseDictService baseDictService;
    @RequestMapping("/list")
    public String customer(Model model){
        // 客戶來源
        List<BaseDict> fromType = this.baseDictService.queryBaseDictByDictTypeCode("002");
        // 所屬行業(yè)
        List<BaseDict> industryType = this.baseDictService.queryBaseDictByDictTypeCode("001");
        // 客戶級別
        List<BaseDict> levelType = this.baseDictService.queryBaseDictByDictTypeCode("006");

        // 把前端頁面需要顯示的數(shù)據(jù)放到模型中
        model.addAttribute("fromType", fromType);
        model.addAttribute("industryType", industryType);
        model.addAttribute("levelType", levelType);


        return "customer";
    }
}

  1. 調(diào)用 http://localhost:8080/customer,可以看到頁面上的下拉框有數(shù)據(jù)了

  2. 接下來我們根據(jù)篩選條件來查詢客戶信息
    分析頁面元素,得出要是用的sql語句:

SELECT
    a.cust_id,
    a.cust_name,
    a.cust_user_id,
    a.cust_create_id,
    b.dict_item_name cust_source,
    c.dict_item_name cust_industry,
    d.dict_item_name cust_level,
    a.cust_linkman,
    a.cust_phone,
    a.cust_mobile,
    a.cust_zipcode,
    a.cust_address,
    a.cust_createtime
FROM
    customer a
LEFT JOIN base_dict b ON a.cust_source = b.dict_id
LEFT JOIN base_dict c ON a.cust_industry = c.dict_id
LEFT JOIN base_dict d ON a.cust_level = d.dict_id
WHERE
    a.cust_name LIKE '%馬%'
AND a.cust_source = '6'
AND a.cust_industry = '2'
AND a.cust_level = '22'
LIMIT 0, 10
  1. 創(chuàng)建pojo
public class Customer {

    private Long cust_id;
    private String cust_name;
    private Long cust_user_id;
    private Long cust_create_id;
    private String cust_source;
    private String cust_industry;
    private String cust_level;
    private String cust_linkman;
    private String cust_phone;
    private String cust_mobile;
    private String cust_zipcode;
    private String cust_address;
    private Date cust_createtime;
get/set。整吆。决侈。仑最。刷钢。爽丹。
}

  1. 創(chuàng)建queryVo
public class QueryVo {

    private String custName;
    private String custSource;
    private String custIndustry;
    private String custLevel;

    // 當(dāng)前頁碼數(shù)
    private Integer page = 1;
    // 數(shù)據(jù)庫從哪一條數(shù)據(jù)開始查
    private Integer start;
    // 每頁顯示數(shù)據(jù)條數(shù)
    private Integer rows = 10;
get/set卤橄。绿满。。窟扑。喇颁。漏健。
}
  1. 創(chuàng)建CustomerDao
package com.spring.crm.dao;

import com.spring.crm.pojo.Customer;
import com.spring.crm.vo.QueryVo;

import java.util.List;
@Repository
public interface CustomerDao {

    /**
     * 根據(jù)queryVo分頁查詢數(shù)據(jù)
     *
     * @param queryVo
     * @return
     */
    List<Customer> queryCustomerByQueryVo(QueryVo queryVo);

    /**
     * 根據(jù)queryVo查詢數(shù)據(jù)條數(shù)
     *
     * @param queryVo
     * @return
     */
    int queryCountByQueryVo(QueryVo queryVo);

}

  1. 同目錄下創(chuàng)建CustomerDao.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.spring.crm.dao.CustomerDao">
    <sql id="customerQueryVo">
        <where>
            <if test="custName != null and custName != ''">
                AND a.cust_name LIKE '%${custName}%'
            </if>
            <if test="custSource != null and custSource != ''">
                AND a.cust_source = #{custSource}
            </if>
            <if test="custIndustry != null and custIndustry != ''">
                AND a.cust_industry = #{custIndustry}
            </if>
            <if test="custLevel != null and custLevel != ''">
                AND a.cust_level = #{custLevel}
            </if>
        </where>
    </sql>

    <!-- 根據(jù)queryVo分頁查詢數(shù)據(jù) -->
    <select id="queryCustomerByQueryVo" parameterType="com.spring.crm.vo.QueryVo"
            resultType="Customer">
        SELECT
        a.cust_id,
        a.cust_name,
        a.cust_user_id,
        a.cust_create_id,
        b.dict_item_name cust_source,
        c.dict_item_name cust_industry,
        d.dict_item_name cust_level,
        a.cust_linkman,
        a.cust_phone,
        a.cust_mobile,
        a.cust_zipcode,
        a.cust_address,
        a.cust_createtime
        FROM
        customer a
        LEFT JOIN base_dict b ON a.cust_source = b.dict_id
        LEFT JOIN base_dict c ON a.cust_industry = c.dict_id
        LEFT JOIN base_dict d ON a.cust_level = d.dict_id
        <include refid="customerQueryVo" />
        <if test="start != null">
            LIMIT #{start}, #{rows}
        </if>
    </select>

    <!-- 根據(jù)queryVo查詢數(shù)據(jù)條數(shù) -->
    <select id="queryCountByQueryVo" parameterType="com.spring.crm.vo.QueryVo"
            resultType="int">
        SELECT count(1) FROM customer a
        <include refid="customerQueryVo" />
    </select>
</mapper>
  1. 創(chuàng)建service
package com.spring.crm.service;

import com.spring.crm.common.Page;
import com.spring.crm.pojo.Customer;
import com.spring.crm.vo.QueryVo;

public interface CustomerService {
    /**
     * 根據(jù)條件分頁查詢客戶
     *
     * @param queryVo
     * @return
     */
    Page<Customer> queryCustomerByQueryVo(QueryVo queryVo);
}
package com.spring.crm.service;

import com.spring.crm.common.Page;
import com.spring.crm.dao.CustomerDao;
import com.spring.crm.pojo.Customer;
import com.spring.crm.vo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    private CustomerDao customerDao;

    public Page<Customer> queryCustomerByQueryVo(QueryVo queryVo) {
        // 設(shè)置查詢條件,從哪一條數(shù)據(jù)開始查
        queryVo.setStart((queryVo.getPage() - 1) * queryVo.getRows());

        // 查詢數(shù)據(jù)結(jié)果集
        List<Customer> list = this.customerDao.queryCustomerByQueryVo(queryVo);
        // 查詢到的數(shù)據(jù)總條數(shù)
        int total = this.customerDao.queryCountByQueryVo(queryVo);

        // 封裝返回的page對象
        Page<Customer> page = new Page<Customer>(total, queryVo.getPage(), queryVo.getRows(), list);

        return page;
    }
}
  1. 修改Controller
    @RequestMapping("/customer/list")
    public String queryCustomerList(QueryVo queryVo, Model model) {

        try {
            // 解決get請求亂碼問題
            if (StringUtils.isNotBlank(queryVo.getCustName())) {
                queryVo.setCustName(new String(queryVo.getCustName().getBytes("ISO-8859-1"), "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 客戶來源
        List<BaseDict> fromType = this.baseDictService.queryBaseDictByDictTypeCode("002");
        // 所屬行業(yè)
        List<BaseDict> industryType = this.baseDictService.queryBaseDictByDictTypeCode("001");
        // 客戶級別
        List<BaseDict> levelType = this.baseDictService.queryBaseDictByDictTypeCode("006");

        // 把前端頁面需要顯示的數(shù)據(jù)放到模型中
        model.addAttribute("fromType", fromType);
        model.addAttribute("industryType", industryType);
        model.addAttribute("levelType", levelType);

        // 分頁查詢數(shù)據(jù)
        Page<Customer> page = this.customerService.queryCustomerByQueryVo(queryVo);
        // 把分頁查詢的結(jié)果放到模型中
        model.addAttribute("page", page);

        // 數(shù)據(jù)回顯
        model.addAttribute("custName", queryVo.getCustName());
        model.addAttribute("custSource", queryVo.getCustSource());
        model.addAttribute("custIndustry", queryVo.getCustIndustry());
        model.addAttribute("custLevel", queryVo.getCustLevel());

        return "customer";
    }
  1. 訪問http://localhost:8080/customer/list,可以看到頁面有數(shù)據(jù)了橘霎,而且可以根據(jù)篩選條件查詢客戶信息

總結(jié)

本章記錄了如果顯示頁面數(shù)據(jù)和查詢數(shù)據(jù)蔫浆,下一章講解數(shù)據(jù)修改。

本系列源碼存放在github上 源碼

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末姐叁,一起剝皮案震驚了整個濱河市瓦盛,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌外潜,老刑警劉巖原环,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異处窥,居然都是意外死亡嘱吗,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進店門滔驾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來柜与,“玉大人,你說我怎么就攤上這事嵌灰∨埃” “怎么了?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵沽瞭,是天一觀的道長迁匠。 經(jīng)常有香客問我,道長驹溃,這世上最難降的妖魔是什么城丧? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮豌鹤,結(jié)果婚禮上亡哄,老公的妹妹穿的比我還像新娘。我一直安慰自己布疙,他們只是感情好蚊惯,可當(dāng)我...
    茶點故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著灵临,像睡著了一般截型。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上儒溉,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天宦焦,我揣著相機與錄音,去河邊找鬼。 笑死波闹,一個胖子當(dāng)著我的面吹牛酝豪,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播精堕,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼寓调,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了锄码?” 一聲冷哼從身側(cè)響起夺英,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎滋捶,沒想到半個月后痛悯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡重窟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年载萌,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片巡扇。...
    茶點故事閱讀 40,127評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡扭仁,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出厅翔,到底是詐尸還是另有隱情乖坠,我是刑警寧澤,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布刀闷,位于F島的核電站熊泵,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏甸昏。R本人自食惡果不足惜顽分,卻給世界環(huán)境...
    茶點故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望施蜜。 院中可真熱鬧卒蘸,春花似錦、人聲如沸翻默。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽冰蘑。三九已至和泌,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間祠肥,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留仇箱,地道東北人县恕。 一個月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像剂桥,于是被迫代替她去往敵國和親忠烛。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,066評論 2 355

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理权逗,服務(wù)發(fā)現(xiàn)美尸,斷路器,智...
    卡卡羅2017閱讀 134,667評論 18 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,182評論 25 707
  • iOS網(wǎng)絡(luò)架構(gòu)討論梳理整理中斟薇。师坎。。 其實如果沒有APIManager這一層是沒法使用delegate的堪滨,畢竟多個單...
    yhtang閱讀 5,193評論 1 23
  • 上班第五天,冷冷清清的校園只剩下孤零零的我們幾個年輕教師发笔∶巳苦于食堂沒有飯吃,我們幾個慢慢悠悠地去隔壁小超市買吃的了讨。...
    月下小翁閱讀 583評論 0 0
  • 前言 最近在學(xué)習(xí)廖雪峰老師的 Python3.0 教程, 在安裝 MySQL 的時候遇到了一些問題鸯旁,現(xiàn)把我的整個安...
    Hi川閱讀 402評論 0 0