ps:昨天将管理员登录的功能完成了,并完美的解决跳过登录从而进入管理界面的bug,今天我们将实现"查询用户"功能。

    ①在po包中创建Customer类,并编写相关变量和添加set/get方法。(相关变量:客户编号,客户名称,负责人id,创建人id,客户信息来源,客户所属行业,客户级别,联系人,固定电话,移动电话,邮政编码,联系地址,创建时间,起始行,所取行数) 然后创建BaseDict类(字典类)

    ②在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>

       ④在service包中创建CustomerService.java接口和BaseDictService.java接口,并在CustomerService接口中编写通过id查询客户的方法,传入customer对象和查询客户列表的方法。在BaseDict接口中编写根据类别代码查询数据字典的方法 findBaseDictByTypeCode。

      ⑤在service.impl包中创建CustomerServiceImpl.java接口实现类和BaseDictServiceImpl.java接口实现类,并在方法名上添加@Service注解和在方法内部添加@Autowired自动注入

     ⑥在Controller包中创建CustomerController.java类,在方法名上方加上@Controller注解,在方法内@Autowired自动注入CustomerService和BaseDictService。        

@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";
}
}

        总结:

            写到这里,我们的查询客户的功能就全部写完了,回顾一下我们做了什么工作:首先,我们先创建了POJO类(用户类和字典表类),然后我们在dao包中创建了用户类和字典表类的对应接口,并且还对应的创建了xml文件。然后是service类,在service中我们仍然还是创建了接口,并且之后创建了该接口的实现类,最后在controller包中创建了控制类(我觉得这个类相当于一个开关 OFF/ON)。那么将这个项目发布到Tomocat服务器上,浏览器的执行顺序是这样子的:我们在网页上点击操作,然后浏览器首先会查看教程(一)中写好的配置文件(applicationContext.xml等~),然后会首先进入到Controller类中,检查RequestMapping的映射路径(看图)