springmvc学习笔记(18)——CRUD之查询
来源:互联网 发布:crm软件系统发展 编辑:程序博客网 时间:2024/06/06 09:03
CRUD之查询
有了前面十几个笔记的基础,我们现在可以开始做程序员最常做的事情CRUD(增删改差)啦。
为了充分用上前面学的知识,本次CRUD将使用REST风格,不知道REST的同学,请查看springmvc学习笔记(4)——REST
创建实体类
package com.zj.model;/** * 学生类 * @author zhoujun */public class Student { private Integer studentId; //学生id private String name; //学生姓名 private String sex; //性别 private Address address; //地址 public Student() { super(); } public Student(Integer studentId, String name, String sex, Address address) { super(); this.studentId = studentId; this.name = name; this.sex = sex; this.address = address; } public Integer getStudentId() { return studentId; } public void setStudentId(Integer studentId) { this.studentId = studentId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; }}
package com.zj.model;/** * 地址类 * @author zhoujun */public class Address { private String province; //省份 private String city; //城市 public Address() { super(); } public Address(String province, String city) { super(); this.province = province; this.city = city; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; }}
创建DAO
由于本次例子的逻辑十分简单,因此省去了service层,并且不使用数据库,在DAO中模拟数据库
package com.zj.dao;import java.util.Collection;import java.util.HashMap;import java.util.Map;import org.springframework.stereotype.Repository;import com.zj.model.Address;import com.zj.model.Student;@Repositorypublic class StudentDao { private static Map<Integer,Student> students = null; //模拟数据库 static{ students = new HashMap<Integer, Student>(); students.put(0, new Student(0,"tom","男",new Address("福建","福州"))); students.put(1, new Student(1,"jane","女",new Address("福建","泉州"))); students.put(2, new Student(2,"jackson","男",new Address("黑龙江","哈尔滨"))); students.put(3, new Student(3,"mary","女",new Address("香港","九龙"))); students.put(4, new Student(4,"pole","男",new Address("新疆","乌鲁木齐"))); } /** * 得到所有学生信息 * @return */ public Collection<Student> getAll(){ return students.values(); }}
这里出现了@Repository注解,这在之前的笔记中没有讲到过。其实很简单,就是告诉springmvc这个类是一个DAO,让springmvc帮我们实例化对象。
控制层
package com.zj.controller;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.zj.dao.StudentDao;/** * 学生控制器类 * @author zhoujun * */@Controllerpublic class StudentCtrl { @Autowired private StudentDao studentDao; @RequestMapping("/student") public String list(Map<String, Object> map){ map.put("students", studentDao.getAll()); return "list"; }}
这里出现了@Autowired注解,Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。
简单来说,就是拿到一个SutdentDao的实例(不再需要我们自己去new)
前端
在jsp前面加上jstl标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
写一个table显示数据
<table cellspacing="0" cellpadding="10"> <tr> <th>id</th> <th>姓名</th> <th>性别</th> <th>省份</th> <th>城市</th> <th colspan="2">操作</th> </tr> <c:forEach var="stu" items="${students }"> <tr> <td>${stu.studentId }</td> <td>${stu.name }</td> <td>${stu.sex }</td> <td>${stu.address.province }</td> <td>${stu.address.city }</td> <td><a href="">修改</a></td> <td><a href="">删除</a></td> </tr> </c:forEach></table>
访问/student 得到以下结果
jstl标签和el表达式,在javaee中应该都学过,就不详述了。本次的例子中暂时还未体现到REST风格,因此例子本身太过简单。
请持续关注springmvc学习笔记
0 0
- springmvc学习笔记(18)——CRUD之查询
- springmvc学习笔记(19)——CRUD之增加
- springmvc学习笔记(21)——CRUD之修改
- springmvc学习笔记(20)——CRUD之删除 & 静态资源处理
- SpringMVC学习笔记-REST风格CRUD
- 学习SpringMVC(十五)之CRUD操作
- Lucene学习笔记之(二)索引文档的CRUD
- SpringMVC 学习笔记(五) 基于RESTful的CRUD
- SpringMVC 学习笔记(五) 基于RESTful的CRUD
- MongoDB学习笔记之文档的CRUD
- SpringMVC+easyUI 分页,查询 (完整的CRUD)
- springmvc学习笔记(1)——搭建springmvc项目之HelloWorld
- Rails学习笔记(4) ————Model:CRUD
- MyBatis学习笔记二——CRUD操作
- JavaWeb学习笔记——jdbc连接数据库&CRUD
- springmvc学习笔记(31)——springmvc运行流程
- springmvc学习笔记(31)——spring整合springmvc
- 学习SpringMVC——你们要的REST风格的CRUD来了
- linux内核设计与实现的读书笔记1 -内核开发头文件
- 第八周阅读程序
- MFC CComboBox 选择项是空的
- Androidの解决ViewFlipper与ScrollView滑动响应事件拦截的问题
- 关于CentOS5.3内核升级至2.6.30+版本的问题
- springmvc学习笔记(18)——CRUD之查询
- 哈哈,学习中
- CSU1578: Opening Ceremony
- Android导入android-support-v4.jar
- C++析构函数知识点小结
- 十大常用数据结构
- FTP实现断点续传
- C++编程风格1——头文件
- 硬币问题 DP 已知起始点的最长最短路径问题