在service层要写接口和该接口的实现类,这样就能在controller层通过service接口来调用方法,就是面向接口开发。
controller层调用service层,service层调用dao层,这些都是通过接口来调用的。
在controller层如果要使用一些自己定义的常量,那么可以把常量定义在一个单独的properties文件中,然后在controller层使用这个文件中常量的办法是使用value(),如本项目所示:
resource.properties文件内容如下:
customer.dict.source=002
customer.dict.industry=001
customer.dict.level=006
然后在controller层中使用这个文件中常量的办法是在成员变量上方使用@value("${ properties文件中的key}")
:
public class CustomerController {
@Autowired
private CustomerService customerService;
@Value("${customer.dict.source}")
private String source;
@Value("${customer.dict.industry}")
private String industry;
@Value("${customer.dict.level}")
private String level;
......
}
这样就可把resource.properties文件中常量的值注入到私有的成员变量中,再使用该成员变量即可。
而resource.properties文件是通过springMvc.xml文件加载进来的,即在springMvc.xml文件中有如下这个配置:
<!-- 引入字典资源文件 -->
<context:property-placeholder location="classpath:resource.properties"/>
controller类的代码如下:
package cn.itheima.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.itcast.utils.Page;
import cn.itheima.pojo.BaseDict;
import cn.itheima.pojo.Customer;
import cn.itheima.pojo.QueryVo;
import cn.itheima.service.CustomerService;
@Controller
@RequestMapping("/customer")
public class CustomerController {
@Autowired /*按类型注入,spring 管理bean实例的方式默认是单例模式,
所以整个项目只会有一个 CustomerServiceImpl实例。
如果容器中同一个类型的bean如果有多个,使用Autowried报错。*/
private CustomerService customerService; //定义接口,再调用接口的方法,这就是面向接口开发
@Value("${customer.dict.source}")
private String source;
@Value("${customer.dict.industry}")
private String industry;
@Value("${customer.dict.level}")
private String level;
@RequestMapping("/list")
public String list(QueryVo vo, Model model) throws Exception{
/*使用QueryVo来接收从页面传过来的参数(根据springmvc的参数绑定,只要表
单元素的name和QueryVo中的属性名相同,就可把参数值传进来)。*/
//使用Model来向页面发送数据。
//所以QueryVo,Model都定义在形参上。
//客户来源
List<BaseDict> sourceList = customerService.findDictByCode(source);
//所属行业
List<BaseDict> industryList = customerService.findDictByCode(industry);
//客户级别
List<BaseDict> levelList = customerService.findDictByCode(level);
//下面这个if解决乱码问题
if(vo.getCustName() != null){
vo.setCustName(new String(vo.getCustName().getBytes("iso8859-1"), "utf-8"));
}
if(vo.getPage() == null){
vo.setPage(1);
}
//设置查询的起始记录条数
vo.setStart((vo.getPage() - 1) * vo.getSize());
//查询数据列表和数据总数
List<Customer> resutList = customerService.findCustomerByVo(vo);
Integer count = customerService.findCustomerByVoCount(vo);
//Page是一个自定义的分页的类
Page<Customer> page = new Page<Customer>();
page.setTotal(count); //数据总数
page.setSize(vo.getSize()); //每页显示条数
page.setPage(vo.getPage()); //当前页数
page.setRows(resutList); //数据列表
//向页面发送数据
model.addAttribute("page", page);
//高级查询下拉列表数据
model.addAttribute("fromType", sourceList);
model.addAttribute("industryType", industryList);
model.addAttribute("levelType", levelList);
//高级查询选中数据回显,
/*因为使用了QueryVo来接收了数据,所以可以把接收到
的数据再次发送到页面,这样就实现了数据回显*/
model.addAttribute("custName", vo.getCustName());
model.addAttribute("custSource", vo.getCustSource());
model.addAttribute("custIndustry", vo.getCustIndustry());
model.addAttribute("custLevel", vo.getCustLevel());
return "customer";
}
@RequestMapping("/detail")
@ResponseBody //向页面返回一个java对象时要使用@ResponseBody把这个对象变成json格式
public Customer detail(Long id) throws Exception{
Customer customer = customerService.findCustomerById(id);
return customer;
}
@RequestMapping("/update")
public String update(Customer customer)throws Exception{
/*进行更新操作时往往是要传一个对象进来(因为sql语句中的set后面要设置很多个字段,
所以传入一个对象会好些),所以使用Customer作为形参。*/
customerService.updateCustomerById(customer);
return "customer";
}
@RequestMapping("/delete")
public String delete(Long id) throws Exception{
/*删除操作只需要从页面得到id即可(`因为sql语句就是delete from customer where cust_id=#{id}`),
所以形参就是id,再根据这个id调用下层的方法删除即可*/
customerService.delCustomerById(id);
return "customer";
}
}