ps:昨天將管理員登陸的功能完成了,並完美的解決跳過登陸從而進入管理界面的bug,今天咱們將實現"查詢用戶"功能。java
①在po包中建立Customer類,並編寫相關變量和添加set/get方法。(相關變量:客戶編號,客戶名稱,負責人id,建立人id,客戶信息來源,客戶所屬行業,客戶級別,聯繫人,固定電話,移動電話,郵政編碼,聯繫地址,建立時間,起始行,所取行數) 而後建立BaseDict類(字典類)
sql
②在dao包中建立CustomerDao接口,接口中包含 經過id查詢客戶的方法,客戶列表方法(List集合)和顯示客戶數的方法瀏覽器
③在dao包中建立CustomerDao.xml文件,在文件開頭咱們先編寫映射地址,而後編寫查詢用戶的sql語句,其id爲 "selectCustomerListWhere" 而後編寫查詢客戶列表的sql語句(查詢客戶列表的語句要和字典表結合來查)。以後編寫查詢客戶總數的sql語句。服務器
<mapper namespace="com.yehaijing.core.dao.CustomerDao" >
<!-- 查詢客戶 -->
<sql id="selectCustomerListWhere">
<where>
<if test="cust_name != null" >
cust_name like "%"#{cust_name}"%"
</if>
<if test="cust_source != null" >
and cust_source = #{cust_source}
</if>
<if test="cust_industry != null" >
and cust_industry = #{cust_industry}
</if>
<if test="cust_level != null" >
and cust_level = #{cust_level}
</if>
</where>
</sql>
<!-- 查詢客戶列表 -->
<select id="selectCustomerList" parameterType="customer"
resultType="customer">
SELECT
cust_id,
cust_name,
cust_user_id,
cust_create_id,
b.dict_item_name cust_source,
c.dict_item_name cust_industry,
d.dict_item_name cust_level,
cust_linkman,
cust_phone,
cust_mobile,
cust_createtime
FROM
customer a
LEFT JOIN (
SELECT
dict_id,
dict_item_name
FROM
base_dict
WHERE
dict_type_code = '002'
) b ON a.cust_source = b.dict_id
LEFT JOIN (
SELECT
dict_id,
dict_item_name
FROM
base_dict
WHERE
dict_type_code = '001'
) c ON a.cust_industry = c.dict_id
LEFT JOIN (
SELECT
dict_id,
dict_item_name
FROM
base_dict
WHERE
dict_type_code = '006'
@Controller public class CustomerController { // 依賴注入 @Autowired private CustomerService customerService; @Autowired private BaseDictService baseDictService; // 客戶來源 @Value("${customer.from.type}") private String FROM_TYPE; // 客戶所屬行業 @Value("${customer.industry.type}") private String INDUSTRY_TYPE; // 客戶級別 @Value("${customer.level.type}") private String LEVEL_TYPE; /** * 客戶列表 */ @RequestMapping(value = "/customer/list.action") public String list(@RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="10")Integer rows, String custName, String custSource, String custIndustry, String custLevel, Model model) { // 條件查詢全部客戶 Page<Customer> customers = customerService .findCustomerList(page, rows, custName, custSource, custIndustry,custLevel); model.addAttribute("page", customers); // 客戶來源 List<BaseDict> fromType = baseDictService .findBaseDictByTypeCode(FROM_TYPE); // 客戶所屬行業 List<BaseDict> industryType = baseDictService .findBaseDictByTypeCode(INDUSTRY_TYPE); // 客戶級別 List<BaseDict> levelType = baseDictService .findBaseDictByTypeCode(LEVEL_TYPE); // 添加參數 model.addAttribute("fromType", fromType); model.addAttribute("industryType", industryType); model.addAttribute("levelType", levelType); model.addAttribute("custName", custName); model.addAttribute("custSource", custSource); model.addAttribute("custIndustry", custIndustry); model.addAttribute("custLevel", custLevel); return "customer"; } }
) d ON a.cust_level = d.dict_id<include refid="selectCustomerListWhere"/><!-- 執行分頁查詢 --><if test="start !=null and rows != null">limit #{start},#{rows}</if></select><!-- 查詢客戶總數 --><select id="selectCustomerListCount" parameterType="customer" resultType="Integer">select count(*) from customer<include refid="selectCustomerListWhere"/></select>app
④在service包中建立CustomerService.java接口和BaseDictService.java接口,並在CustomerService接口中編寫經過id查詢客戶的方法,傳入customer對象和查詢客戶列表的方法。在BaseDict接口中編寫根據類別代碼查詢數據字典的方法 findBaseDictByTypeCode。編碼
⑤在service.impl包中建立CustomerServiceImpl.java接口實現類和BaseDictServiceImpl.java接口實現類,並在方法名上添加@Service註解和在方法內部添加@Autowired自動注入spa
⑥在Controller包中建立CustomerController.java類,在方法名上方加上@Controller註解,在方法內@Autowired自動注入CustomerService和BaseDictService。
code
@Controller
public class CustomerController {
// 依賴注入
@Autowired
private CustomerService customerService;
@Autowired
private BaseDictService baseDictService;
// 客戶來源
@Value("${customer.from.type}")
private String FROM_TYPE;
// 客戶所屬行業
@Value("${customer.industry.type}")
private String INDUSTRY_TYPE;
// 客戶級別
@Value("${customer.level.type}")
private String LEVEL_TYPE;
/**
* 客戶列表
*/
@RequestMapping(value = "/customer/list.action")
public String list(@RequestParam(defaultValue="1")Integer page,
@RequestParam(defaultValue="10")Integer rows,
String custName, String custSource, String custIndustry,
String custLevel, Model model) {
// 條件查詢全部客戶
Page<Customer> customers = customerService
.findCustomerList(page, rows, custName,
custSource, custIndustry,custLevel);
model.addAttribute("page", customers);
// 客戶來源
List<BaseDict> fromType = baseDictService
.findBaseDictByTypeCode(FROM_TYPE);
// 客戶所屬行業
List<BaseDict> industryType = baseDictService
.findBaseDictByTypeCode(INDUSTRY_TYPE);
// 客戶級別
List<BaseDict> levelType = baseDictService
.findBaseDictByTypeCode(LEVEL_TYPE);
// 添加參數
model.addAttribute("fromType", fromType);
model.addAttribute("industryType", industryType);
model.addAttribute("levelType", levelType);
model.addAttribute("custName", custName);
model.addAttribute("custSource", custSource);
model.addAttribute("custIndustry", custIndustry);
model.addAttribute("custLevel", custLevel);
return "customer";
}
}
總結:xml
寫到這裏,咱們的查詢客戶的功能就所有寫完了,回顧一下咱們作了什麼工做:首先,咱們先建立了POJO類(用戶類和字典表類),而後咱們在dao包中建立了用戶類和字典表類的對應接口,而且還對應的建立了xml文件。而後是service類,在service中咱們仍然仍是建立了接口,而且以後建立了該接口的實現類,最後在controller包中建立了控制類(我以爲這個類至關於一個開關 OFF/ON)。那麼將這個項目發佈到Tomocat服務器上,瀏覽器的執行順序是這樣子的:咱們在網頁上點擊操做,而後瀏覽器首先會查看教程(一)中寫好的配置文件(applicationContext.xml等~),而後會首先進入到Controller類中,檢查RequestMapping的映射路徑(看圖)對象