本章主要实现的功能有两个
一是实现页面展示
二是实现条件查询初始化
即这几个下拉框中实现查询条件的初始化
一、实现页面显示
接着上一篇,在cn.zhao.crm.controller包下创建CustomerController.java文件
package cn.zhao.crm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 客户管理
* @author S1
*
*/
@Controller
public class CustomerController {
@RequestMapping("/customer/list")
public String list(Model model) {
return "customer";
}
}
这样就可以了,实验一下,在浏览器中输入url:http://localhost:8080/SSM_CRM/customer/list即可以看到页面
二、实现条件查询初始化
2.1 思考
我们需要在页面一加载时,便从数据库中查询数据放入select下拉框中,应该怎么做呢?
首先在Navicat或其他工具上编写sql语句,在表base_dict中根据dict_type_code查询客户来源,所属行业及客户级别
然后再想java中如何编写,先根据表创建一个pojo类,再写dao接口与mapper.xml,然后写Service接口实现类,最后在controller中调用保存到域中,根据jsp中下拉表单名称保存。
还是比较简单的。
2.2 编写pojo类
根据数据库中base_dict表中字段,在cn.zhao.crm.pojo包下创建BaseDict类
package cn.zhao.crm.pojo;
/**
* 客户相关信息类
* @author S1
*
*/
public class BaseDict {
private String dict_id;
private String dict_type_code;
private String dict_type_name;
private String dict_item_name;
private String dict_item_code;
private Integer dict_sort;
private String dict_enable;
private String dict_memo;
public String getDict_id() {
return dict_id;
}
public void setDict_id(String dict_id) {
this.dict_id = dict_id;
}
public String getDict_type_code() {
return dict_type_code;
}
public void setDict_type_code(String dict_type_code) {
this.dict_type_code = dict_type_code;
}
public String getDict_type_name() {
return dict_type_name;
}
public void setDict_type_name(String dict_type_name) {
this.dict_type_name = dict_type_name;
}
public String getDict_item_name() {
return dict_item_name;
}
public void setDict_item_name(String dict_item_name) {
this.dict_item_name = dict_item_name;
}
public String getDict_item_code() {
return dict_item_code;
}
public void setDict_item_code(String dict_item_code) {
this.dict_item_code = dict_item_code;
}
public Integer getDict_sort() {
return dict_sort;
}
public void setDict_sort(Integer dict_sort) {
this.dict_sort = dict_sort;
}
public String getDict_enable() {
return dict_enable;
}
public void setDict_enable(String dict_enable) {
this.dict_enable = dict_enable;
}
public String getDict_memo() {
return dict_memo;
}
public void setDict_memo(String dict_memo) {
this.dict_memo = dict_memo;
}
@Override
public String toString() {
return "BaseDict [dict_id=" + dict_id + ", dict_type_code=" + dict_type_code + ", dict_type_name="
+ dict_type_name + ", dict_item_name=" + dict_item_name + ", dict_item_code=" + dict_item_code
+ ", dict_sort=" + dict_sort + ", dict_enable=" + dict_enable + ", dict_memo=" + dict_memo + "]";
}
}
2.3 编写dao接口与mapper
在cn.zhao.crm.dao包下创建BaseDictDao.java和BaseDictDao.xml
BaseDictDao.java中编写根据类别code查询数据selectBaseDictByTypeCode
package cn.zhao.crm.mapper;
import java.util.List;
import cn.zhao.crm.pojo.BaseDict;
/**
* 客户基本信息dao
* @author S1
*
*/
public interface BaseDictDao {
//根据客户类别代码查询数据
public List<BaseDict> selectBaseDictByTypeCode(String typeCode);
}
BaseDictDao.xml中编写对应的select语句
<?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="cn.zhao.crm.mapper.BaseDictDao">
<select id="selectBaseDictByTypeCode" parameterType="String" resultType="BaseDict">
select * from base_dict where dict_type_code = #{value}
</select>
</mapper>
2.4 编写Service接口与实现类
在cn.zhao.crm.service包下创建BaseDictService接口与BaseBaseDictServiceImpl实现类
BaseDictService接口:
package cn.zhao.crm.service;
import java.util.List;
import cn.zhao.crm.pojo.BaseDict;
/**
* BaseDict的Service接口
* @author S1
*
*/
public interface BaseDictService {
//根据类别查找数据
public List<BaseDict> selectBaseDictByTypeCode(String typeCode);
}
BaseBaseDictServiceImpl实现类:
package cn.zhao.crm.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zhao.crm.mapper.BaseDictDao;
import cn.zhao.crm.pojo.BaseDict;
@Service
public class BaseDictServiceImpl implements BaseDictService {
@Autowired
private BaseDictDao baseDictDao;
@Override
public List<BaseDict> selectBaseDictByTypeCode(String typeCode) {
// TODO Auto-generated method stub
return baseDictDao.selectBaseDictByTypeCode(typeCode);
}
}
这里需要注意的是:@Service和@Autowired注解不要忘记写
2.5 编写CustomerController类
在原有CustomController类基础上写新的代码:
package cn.zhao.crm.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.zhao.crm.pojo.BaseDict;
import cn.zhao.crm.service.BaseDictService;
/**
* 客户管理
* @author S1
*
*/
@Controller
public class CustomerController {
@Autowired
private BaseDictService baseDictService;
@RequestMapping("/customer/list")
public String list(Model model) {
//一进入页面即调用方法,为select下拉框添加数据
List<BaseDict> fromType = baseDictService.selectBaseDictByTypeCode("002");
List<BaseDict> industryType = baseDictService.selectBaseDictByTypeCode("001");
List<BaseDict> levelType = baseDictService.selectBaseDictByTypeCode("006");
//添加域中
model.addAttribute("fromType", fromType);
model.addAttribute("industryType", industryType);
model.addAttribute("levelType", levelType);
return "customer";
}
}
这里有一个注意事项和一个问题:
注意事项:@Controller,@Autowired注解不要忘写,添加要域中的key名称要和jsp中相同
问题:客户类别Code被硬编码到代码中了,需要优化
在此,这个功能就完成了,可以访问url看一下
三、解决硬编码问题
在src下创建resource.properties配置文件,里面存储硬编码数据,在springmvc中配置扫描配置文件,Controller中使用@Value对成员变量进行注入
resource.properties:
TypeCode.fromType=002
TypeCode.industryType=001
TypeCode.levelType=006
springmvc.xml:
<!-- 配置 读取properties文件 resource.properties -->
<context:property-placeholder location="classpath:resource.properties" />
CustomerController.java:
@Value("${TypeCode.fromType}")
private String FromType;
@Value("${TypeCode.industryType}")
private String IndustryType;
@Value("${TypeCode.levelType}")
private String LevelType;
@RequestMapping("/customer/list")
public String list(Model model) {
//一进入页面即调用方法,为select下拉框添加数据
List<BaseDict> fromType = baseDictService.selectBaseDictByTypeCode(FromType);
List<BaseDict> industryType = baseDictService.selectBaseDictByTypeCode(IndustryType);
List<BaseDict> levelType = baseDictService.selectBaseDictByTypeCode(LevelType);
//添加域中
model.addAttribute("fromType", fromType);
model.addAttribute("industryType", industryType);
model.addAttribute("levelType", levelType);
return "customer";
}
这样就解决了硬编码问题,两个功能也实现完毕!