1、创建包
1.Dao层:全称Data Access Object。Dao层比较底层,负责与数据库打交道具体到对某个表、某个实体的增删改查
2.Service层:又叫服务层或业务层,封装Dao层的操作,使一个方法对外表现为实现一种功能,例如:网购生成订单时,不仅要插入订单信息记录,还要查询商品库存是否充足,购买是否超过限制等等。
3.Controller层:业务控制层,负责接收数据和请求,并且调用Service层实现这个业务逻辑。
Controller层像是一个服务员,他把客人(前端)点的菜(数据、请求的类型等)进行汇总什么口味、咸淡、量的多少,交给厨师长(Service层),厨师长则告诉沾板厨师(Dao 1)、汤料房(Dao 2)、配菜厨师(Dao 3)等(统称Dao层)我需要什么样的半成品,副厨们(Dao层)就负责完成厨师长(Service)交代的任务。不知道这个比喻是否合适。
Entity层
Dao层
Service层
Controller层
准备工作完成后在resources中创建templates(用来写HTML文件):
显示全部的页面 index :
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<center>
<table border = "1" width="80%">
<tr>
<th colspan="7">学生信息表</th>
</tr>
<tr>
<th colspan="7"><a href="/student/toAdd">添加</a></th>
</tr>
<tr>
<th colspan="7">
<form action="/student/findAllByNameLike">
<input type="text" name="name">
<input type="submit" value="查询">
</form>
</th>
</tr>
<tr>
<td>编号</td>
<td>姓名</td>
<td>成绩</td>
<td>生日</td>
<td>班级</td>
<td>照片</td>
<td>操作</td>
</tr>
<tr th:each="student:${student}">
<td th:text="${student.id}"></td>
<td th:text="${student.name}"></td>
<td th:text="${student.score}"></td>
<td th:text="${student.birthday}"></td>
<td th:text="${student.cname}"></td>
<td><img th:src="@{${pageInfo.file}}" alt="" width="100" heigh="100"></td>
<td>
<a th:href="@{/student/doDelete(id=${student.id})}">删除</a>
<a th:href="@{/student/toUpdate(id=${student.id})}">修改</a>
</td>
</tr>
</table>
</center>
</body>
</html>
添加的页面 student_add (包括Ajax删除) :
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<center>
<form action="/student/doadd" method="post" enctype="multipart/form-data">
<table border="1">
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>成绩:</td>
<td><input type="text" name="score"></td>
</tr>
<tr>
<td>生日:</td>
<td><input type="date" name="birthday"></td>
</tr>
<tr>
<td>班级:</td>
<td><select name="scid">
<option th:each="classes:${classes}" th:value="${classes.cid}" th:text="${classes.cname}"></option>
</select></td>
</tr>
<tr>
<td>照片</td>
<td>
<input type="file" name="name" id="file"><br>
<p id="url"><img src="#" alt="" width="200" height="200"></p>
<input type="text" name="sfile" id="sfile"><br>
<input type="button" value="上传" id="upload">
<input type="button" value="删除" id="delete">
</td>
</tr>
<input type="submit" value="添加">
</table>
</form>
</center>
<script>
$(function () {
$("#upload").click(function () {
/* var form = new FormData();
form.append("file",document.getElementById("file").files[0]);
$.ajax({
url:"/student/upload",
type: "post",
async: false,
cache: false,
data: form,
dateType: 'json',
contentType: false,
processData: false,
success:function (data) {
alert(JSON.stringify(data));
if(data){
var pic = dara.xfilename;
$("#url img").attr("src",pic);
$("#sfile").val(pic);
}
},
})*/
})
})
</script>
</body>
</html>
修改的页面 student_update :
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<center>
<form action="/student/doUpdate">
姓名:<input type="text" name="name" th:value="${student.name}"><br><!-- th:value 获取将要修改的属性内容 -->
成绩:<input type="text" name="score" th:value="${student.score}"><br><!-- 同上 -->
生日:<input type="date" name="birthday" th:value="${student.birthday == null} ? '' : ${#dates.format(student.birthday, 'yyyy-MM-dd')}"><br><!-- 同上 但跟之前用的方法不同 -->
班级:
<select name="scid">
<option th:each="classes:${classes}" th:selected="(${student.scid} == ${classes.cid})" th:value="${classes.cid}" th:text="${classes.cname}"></option><!-- th:selected 使scid显示classes表中的cid内容 -->
</select><br>
<input type="hidden" name="id" th:value="${student.id}"><!-- 隐藏域 获取id属性 为修改获取id属性做准备 -->
<input type="submit" value="添加">
</form>
</center>
</body>
</html>
因为表连接,所以也要在Dao、Entity、Service中写classes表
Entity、
package com.example.demo.Entity;
import javax.persistence.*;
@Table
@Entity(name="classes")
public class Classes {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int cid;
private String cname;
public void setCid(int cid){
this.cid = cid;
}
public void setCname(String cname) {
this.cname = cname;
}
public int getCid() {
return cid;
}
public String getCname() {
return cname;
}
}
Service、
package com.example.demo.Service;
import com.example.demo.Dao.ClassesDao;
import com.example.demo.Entity.Classes;
import com.example.demo.Entity.Student;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class ClassesService {
@Resource
ClassesDao classesDao;
public List<Classes> findAll(){
return classesDao.findAll();
}
}
Dao、
package com.example.demo.Dao;
import com.example.demo.Entity.Classes;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ClassesDao extends JpaRepository<Classes,Integer> {
}