在上一篇文章中我详细的介绍了如何搭建SSM框架。正所谓工具再好,也要使用在正确的地方。这篇我们来教大家SSM框架如何使用
1.、在Dao层编写dao文件
public interface InfoDao {
int selectCofirmAllCount();
}
在xml文件中加载dao文件
<?xml version="1.0" encoding="UTF-8"?>
<!--表明实现mapper层接口的xml -->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hqyj.yiqing.dao.InfoDao">
<select id="selectCofirmAllCount" resultType="java.lang.Integer">
select sum(confirmCount) from info where code < 35;
</select>
</mapper>
在service层调用dao层的方法
@Service
public class InfoService {
@Autowired
InfoDao dao;
public int selectCofirmAllCount(){
return dao.selectCofirmAllCount();
}
}
在控制层调用service的方法
@Controller
@RequestMapping("zznb")
public class InfoController {
@Autowired
InfoService service;
//查询截止当日所有确诊人数
@RequestMapping("allCofirm.do")
@ResponseBody
public int selectCofirmAllCount(){
return service.selectCofirmAllCount();
}
}
启动服务器,在地址栏输入:
http://localhost:8080/yiqingdemo/zznb/allCofirm.do
最后的成功结果: