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