目录
- 1 SpringMVC
- 1.1 重定向和转发
- 1.1.1 转发
- 1.1.2 重定向
- 1.1.3 转发练习
- 1.1.4 重定向练习
- 1.1.5 重定向/转发特点
- 1.1.6 重定向/转发意义
- 1.2 RestFul风格
- 1.2.1 RestFul入门案例
- 1.2.2 简化业务调用
- 1.3 JSON
- 1.3.1 JSON介绍
- 1.3.2 JSON格式
- 1.3.2.1 Object格式
- 1.3.2.2 Array格式
- 1.3.2.3 嵌套格式
- 1.4 SpringMVC使用JSON返回数据
- 1.4.1 意义
- 1.4.2 @ResponseBody注解
- 1.4.2 @RestController
- 2 SpringBoot整合SSM
- 2.1 创建项目springboot_demo_4
- 2.2 编辑pom.xml文件
- 2.2 编辑yml配置文件
- 2.3 编辑User
- 2.4 编辑UserMapper
- 2.5 编辑UserService/UserServiceImpl
- 2.6 编辑UserController
- 2.7 编辑userList.html页面
- 2.8 页面效果展现
- 2.9 RestFul策略优化
- 3 jQuery实现数据获取
- 3.1 业务说明
- 3.2 jQuery下载
- 3.3 Ajax介绍
- 3.3.1 Ajax特点
- 3.3.2 Ajax异步原理
- 3.4 关于JS 循环遍历的写法
- 3.5 编辑页面JS
- 3.6 编辑UserController
- 3.7 页面效果调用
- 3.8 关于常见Ajax种类介绍
- 3.8.1 带参数的请求
- 3.8.2 post请求结构
- 3.8.3 getJSON类型
- 3.8.4 $.ajax类型
- 3.9 请求类型说明
-
1 SpringMVC
1.1 重定向和转发
1.1.1 转发
由服务器内部进行页面的跳转
一般情况下 SpringMVC内部 以转化为主

1.1.2 重定向
当用户发起请求时,由服务器返回有效的网址信息.之后由用户再次发起请求的结构

1.1.3 转发练习
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @RequestMapping("/findUser") |
| public String findUser(String name) { |
| |
| |
| return "forward:/findDog"; |
| } |
| |
| |
| @RequestMapping("/findDog") |
| public String findDog(String name, Model model) { |
| System.out.println("动态获取name属性值:" + name); |
| model.addAttribute("name", name + "旺旺旺"); |
| return "dog"; |
| } |
复制
| <!DOCTYPE html> |
| <html lang="en" xmlns:th="http://www.thymeleaf.org"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>转发与重定向练习</title> |
| </head> |
| <body> |
| <h1>我是Dog页面</h1> |
| |
| <h3 th:text="${name}"></h3> |
| </body> |
| </html> |
复制


1.1.4 重定向练习
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @RequestMapping("/findUser") |
| public String findUser2(String name) { |
| return "redirect:/findDog"; |
| } |
| |
| |
| @RequestMapping("/findDog") |
| public String findDog2(String name, Model model) { |
| System.out.println("动态获取name属性值:" + name); |
| model.addAttribute("name", name + "旺旺旺"); |
| return "dog"; |
| } |
复制


1.1.5 重定向/转发特点
- 转发 forward
- 重定向 redirect
- 由于是多次请求,所以不会携带用户的数据
- 由于是多次请求,所以用户的浏览器的地址会发生变化
1.1.6 重定向/转发意义
- 实现了方法内部的松耦合
- 如果需要携带参数 使用转发
- 如果一个业务已经完成需要一个新的开始则使用重定向
1.2 RestFul风格
1.2.1 RestFul入门案例
| import org.springframework.stereotype.Controller; |
| import org.springframework.web.bind.annotation.PathVariable; |
| import org.springframework.web.bind.annotation.RequestMapping; |
| |
| @Controller |
| public class RestFulController { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @RequestMapping("/restFul/{id}/{name}") |
| public String restFul(@PathVariable Integer id,@PathVariable String name){ |
| System.out.println("获取参数:"+id+"|"+name); |
| return "success"; |
| } |
| } |
复制


1.2.2 简化业务调用
- 按照常规说明执行增删改查的操作需要多个业务方法
- 新增用户 /insertUser
- 修改用户 /updateUser
- 删除用户 /deleteUser
- 查询用户 /selectUser
上述的操作在早期这么写没有问题.但是新的请求规范规定应该让请求尽可能变成无状态的请求.(删除动词)
- 常见请求类型: 1.GET 2.POST 3.PUT 4.DELETE
- 优化:
- 新增用户 /user 请求类型: POST
- 修改用户 /user 请求类型: PUT
- 删除用户 /user 请求类型: DELETE
- 查询用户 /user 请求类型: GET
优化注解:

- 总结:
- 利用RestFul 可以简化get请求类型
- 利用RestFul可以使用无状态的请求,通过不同的请求类型 控制不同的业务逻辑(较为常用)
1.3 JSON
1.3.1 JSON介绍
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式(字符串)。它使得人们很容易的进行阅读和编写。同时也方便了机器进行解析和生成。
1.3.2 JSON格式
1.3.2.1 Object格式
对象(object) 是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。

| {"key1":"value1","key2":"value2"} |
复制
1.3.2.2 Array格式
数组(array) 是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。

| ["value1","value2","value3"] |
复制
1.3.2.3 嵌套格式
值(value) 可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。

| [true,false,{"id":100,"name":"tomcat","hobbys":["敲代码","玩游戏","找对象",{"username":"admin","password":"123456"}]}] |
复制
1.4 SpringMVC使用JSON返回数据
1.4.1 意义
现阶段一般的请求都是前后端分离的方式ajax (jQuery/Axios),一般向服务器请求的数据通常详情下都是采用JSON串的方式返回.
1.4.2 @ResponseBody注解
| import com.jt.pojo.User; |
| import org.springframework.stereotype.Controller; |
| import org.springframework.web.bind.annotation.RequestMapping; |
| import org.springframework.web.bind.annotation.ResponseBody; |
| |
| @Controller |
| public class JSONController { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @RequestMapping("/getJSON") |
| @ResponseBody |
| public User getJSON(){ |
| User user = new User(); |
| user.setId(1000).setName("JSON测试"); |
| return user; |
| } |
| } |
复制

1.4.2 @RestController

2 SpringBoot整合SSM
- Spring
- SpringMVC
- Mybatis-plus
2.1 创建项目springboot_demo_4


2.2 编辑pom.xml文件
| <?xml version="1.0" encoding="UTF-8"?> |
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| <modelVersion>4.0.0</modelVersion> |
| |
| <parent> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-parent</artifactId> |
| <version>3.1.8</version> |
| <relativePath/> |
| </parent> |
| <groupId>com.jt</groupId> |
| <artifactId>springboot_demo_4</artifactId> |
| <version>0.0.1-SNAPSHOT</version> |
| <name>springboot_demo_4</name> |
| <description>springboot_demo_4</description> |
| <properties> |
| <java.version>17</java.version> |
| </properties> |
| <dependencies> |
| |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-jdbc</artifactId> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-thymeleaf</artifactId> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-web</artifactId> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-devtools</artifactId> |
| <scope>runtime</scope> |
| <optional>true</optional> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.projectlombok</groupId> |
| <artifactId>lombok</artifactId> |
| <optional>true</optional> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-starter-test</artifactId> |
| <scope>test</scope> |
| </dependency> |
| |
| <dependency> |
| <groupId>com.mysql</groupId> |
| <artifactId>mysql-connector-j</artifactId> |
| <scope>runtime</scope> |
| </dependency> |
| |
| <dependency> |
| <groupId>com.baomidou</groupId> |
| <artifactId>mybatis-plus-boot-starter</artifactId> |
| <version>3.5.5</version> |
| </dependency> |
| </dependencies> |
| |
| <build> |
| <plugins> |
| <plugin> |
| <groupId>org.springframework.boot</groupId> |
| <artifactId>spring-boot-maven-plugin</artifactId> |
| <configuration> |
| <image> |
| <builder>paketobuildpacks/builder-jammy-base:latest</builder> |
| </image> |
| <excludes> |
| <exclude> |
| <groupId>org.projectlombok</groupId> |
| <artifactId>lombok</artifactId> |
| </exclude> |
| </excludes> |
| </configuration> |
| </plugin> |
| </plugins> |
| </build> |
| </project> |
复制
2.2 编辑yml配置文件
| server: |
| port: 8090 |
| |
| spring: |
| datasource: |
| url: jdbc:mysql://127.0.0.1:3306/jtadmin?serverTimezone=GMT+8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true |
| username: root |
| password: root |
| |
| |
| thymeleaf: |
| |
| prefix: classpath:/templates/ |
| |
| suffix: .html |
| |
| cache: false |
| |
| mybatis-plus: |
| type-aliases-package: com.jt.pojo |
| mapper-locations: classpath:/mappers/*.xml |
| |
| configuration: |
| map-underscore-to-camel-case: true |
| |
| |
| logging: |
| level: |
| com.jt.mapper: debug |
复制
2.3 编辑User
| import com.baomidou.mybatisplus.annotation.IdType; |
| import com.baomidou.mybatisplus.annotation.TableId; |
| import com.baomidou.mybatisplus.annotation.TableName; |
| import lombok.Data; |
| import lombok.experimental.Accessors; |
| |
| import java.io.Serializable; |
| |
| @Data |
| @Accessors(chain = true) |
| @TableName("demo_user") |
| public class User implements Serializable { |
| @TableId(type = IdType.AUTO) |
| private Integer id; |
| private String name; |
| private Integer age; |
| private String sex; |
| } |
复制
2.4 编辑UserMapper
| import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| import com.jt.pojo.User; |
| |
| public interface UserMapper extends BaseMapper<User> { |
| |
| } |
复制
2.5 编辑UserService/UserServiceImpl
| import com.jt.pojo.User; |
| import java.util.List; |
| |
| public interface UserService { |
| List<User> findAll(); |
| } |
复制
| import com.jt.mapper.UserMapper; |
| import com.jt.pojo.User; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.stereotype.Service; |
| |
| import java.util.List; |
| |
| @Service |
| public class UserServiceImpl implements UserService{ |
| @Autowired |
| private UserMapper userMapper; |
| |
| @Override |
| public List<User> findAll() { |
| return userMapper.selectList(null); |
| } |
| } |
复制
2.6 编辑UserController
| |
| |
| |
| @RequestMapping("/userList") |
| public String userList(Model model){ |
| |
| List<User> userList = userService.findAll(); |
| |
| model.addAttribute("userList",userList); |
| return "userList"; |
| } |
复制
2.7 编辑userList.html页面
| <!DOCTYPE html> |
| |
| <html lang="en" xmlns:th="http://www.thymeleaf.org"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>用户列表数据</title> |
| </head> |
| <body> |
| |
| <table border="1px" align="center" width="800px"> |
| <tr align="center"> |
| <td colspan="4"><h3>用户列表</h3></td> |
| </tr> |
| <tr align="center"> |
| <td>ID</td> |
| <td>名称</td> |
| <td>年龄</td> |
| <td>性别</td> |
| </tr> |
| |
| <tr align="center" th:each="user : ${userList}"> |
| <td th:text="${user.id}"></td> |
| <td th:text="${user.name}"></td> |
| <td th:text="${user.age}"></td> |
| <td th:text="${user.sex}"></td> |
| </tr> |
| </table> |
| </body> |
| </html> |
复制
2.8 页面效果展现

2.9 RestFul策略优化

| import com.jt.pojo.User; |
| |
| import java.util.List; |
| |
| public interface UserService { |
| List<User> findAll(); |
| |
| void insertUser(User user); |
| |
| void updateUser(User user); |
| |
| void deleteUserById(Integer id); |
| } |
复制
| import com.jt.mapper.UserMapper; |
| import com.jt.pojo.User; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.stereotype.Service; |
| |
| import java.util.List; |
| |
| @Service |
| public class UserServiceImpl implements UserService{ |
| |
| @Autowired |
| private UserMapper userMapper; |
| |
| |
| @Override |
| public List<User> findAll() { |
| |
| return userMapper.selectList(null); |
| } |
| |
| @Override |
| public void insertUser(User user) { |
| |
| userMapper.insert(user); |
| } |
| |
| @Override |
| public void updateUser(User user) { |
| userMapper.updateById(user); |
| } |
| |
| @Override |
| public void deleteUserById(Integer id) { |
| userMapper.deleteById(id); |
| } |
| } |
复制
| import com.jt.pojo.User; |
| import com.jt.service.UserService; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.stereotype.Controller; |
| import org.springframework.ui.Model; |
| import org.springframework.web.bind.annotation.PathVariable; |
| import org.springframework.web.bind.annotation.RequestMapping; |
| import org.springframework.web.bind.annotation.ResponseBody; |
| |
| import java.util.List; |
| |
| @Controller |
| public class UserController { |
| |
| @Autowired |
| private UserService userService; |
| |
| @RequestMapping("/demo") |
| @ResponseBody |
| public String demo(){ |
| return "框架整合初步完成"; |
| } |
| |
| |
| |
| |
| @RequestMapping("/userList") |
| public String userList(Model model){ |
| |
| List<User> userList = userService.findAll(); |
| |
| model.addAttribute("userList",userList); |
| return "userList"; |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| @RequestMapping("/user/{name}/{age}/{sex}") |
| public String insertUser(User user){ |
| userService.insertUser(user); |
| return "redirect:/userList"; |
| } |
| |
| @RequestMapping("/user/{id}/{name}") |
| public String updateUser(User user){ |
| userService.updateUser(user); |
| return "redirect:/userList"; |
| } |
| |
| @RequestMapping("/user/{id}") |
| public String deleteUser(@PathVariable Integer id){ |
| userService.deleteUserById(id); |
| return "redirect:/userList"; |
| } |
| } |
复制
3 jQuery实现数据获取
3.1 业务说明
- 用户通过http://localhost:8090/userAjax 要求跳转到userAjax.html中
准备userAjax页面
| <!DOCTYPE html> |
| |
| <html lang="en" xmlns:th="http://www.thymeleaf.org"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>用户列表数据</title> |
| </head> |
| <body> |
| |
| <table border="1px" align="center" width="800px"> |
| <tr align="center"> |
| <td colspan="4"><h3>用户列表</h3></td> |
| </tr> |
| <tr align="center"> |
| <td>ID</td> |
| <td>名称</td> |
| <td>年龄</td> |
| <td>性别</td> |
| </tr> |
| </table> |
| </body> |
| </html> |
复制
- 通过ajax请求向后端服务器获取数据,并且在页面中展现数据
3.2 jQuery下载
官网:https://jquery.com/




3.3 Ajax介绍
3.3.1 Ajax特点
- 局部刷新-异步访问
- 同步:
- 当用户发起请求时,如果这时服务器正忙
- 那么用户处于等待的状态,同时不可以做其他的操作
- 异步:
- 当用户发起请求时,如果遇到服务器正忙
- 这时用户可以继续执行后续的操作.同时通过回调函数与服务器沟通
3.3.2 Ajax异步原理

- Ajax异步调用
- Ajax通过Ajax引擎实现异步的调用.当后台的服务器响应数据之后,通过预先设置好的回调函数进行数据的展现

3.4 关于JS 循环遍历的写法
| 1.常规for循环 |
| for(var i=0;i<result.length;i++){ |
| var user = result[i]; |
| console.log(user) |
| } |
| |
| 2.使用in的关键字 |
| |
| for(index in result){ |
| var user = result[index] |
| console.log(user) |
| } |
| |
| 3.使用of关键字 |
| for(user of result){ |
| console.log(user) |
| } |
复制
3.5 编辑页面JS
| <!DOCTYPE html> |
| |
| <html lang="en" xmlns:th="http://www.thymeleaf.org"> |
| <head> |
| <meta charset="UTF-8"> |
| <title>用户列表数据</title> |
| |
| |
| |
| |
| |
| <script src="/jquery-3.6.0.js"></script> |
| |
| <script> |
| |
| $(function () { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| $.get("/findAjaxUser", function (result) { |
| |
| |
| for(user of result){ |
| |
| var id = user.id |
| var name = user.name |
| var age = user.age |
| var sex = user.sex |
| var tr = "<tr align='center'><td>"+ id +"</td><td>"+name+"</td><td>"+age+"</td><td>"+sex+"</td></tr>" |
| |
| |
| $("#userTable").append(tr) |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| }) |
| }) |
| </script> |
| </head> |
| <body> |
| |
| <table id="userTable" border="1px" align="center" width="800px"> |
| <tr align="center"> |
| <td colspan="4"><h3>用户列表</h3></td> |
| </tr> |
| <tr align="center"> |
| <td>ID</td> |
| <td>名称</td> |
| <td>年龄</td> |
| <td>性别</td> |
| </tr> |
| </table> |
| </body> |
| </html> |
复制
3.6 编辑UserController
根据前端ajax请求.处理业务
| import com.jt.pojo.User; |
| import com.jt.service.UserService; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.stereotype.Controller; |
| import org.springframework.web.bind.annotation.RequestMapping; |
| import org.springframework.web.bind.annotation.ResponseBody; |
| |
| import java.util.List; |
| |
| @Controller |
| public class AjaxController { |
| @Autowired |
| private UserService userService; |
| |
| |
| |
| |
| @RequestMapping("/userAjax") |
| public String userAjax() { |
| return "userAjax"; |
| } |
| |
| |
| |
| |
| |
| @RequestMapping("/findAjaxUser") |
| @ResponseBody |
| public List<User> findAjaxUser(){ |
| return userService.findAll(); |
| } |
| } |
复制
3.7 页面效果调用

3.8 关于常见Ajax种类介绍
3.8.1 带参数的请求
| |
| |
| |
| |
| |
| |
| $.get("/findAjaxUser", 'id=1&name=tom', function (result) { |
| console.log("带参数请求,字符串拼接") |
| }) |
复制

| |
| |
| |
| |
| |
| |
| $.get("/findAjaxUser", {id:1,name:tom}, function (result) { |
| console.log("带参数请求,js对象写法") |
| }) |
复制

3.8.2 post请求结构
| |
| |
| |
| $.post('/findAjaxUser',{id:3,name:'post请求'},function (result) { |
| console.log('post请求成功') |
| }) |
复制


3.8.3 getJSON类型
| |
| |
| |
| |
| $.getJSON('/findAjaxUser',{id:4,name:'getJSON类型'},function (result) { |
| console.log('getJSON请求成功') |
| }) |
复制


3.8.4 $.ajax类型
| |
| |
| |
| $.ajax({ |
| type: "put", |
| url: '/findAjaxUser', |
| data: {id:5,name:'ajax基本请求方式'}, |
| |
| success: function (result) { |
| console.log('ajax基本请求方式成功') |
| }, |
| error: function (result) { |
| console.log('ajax基本请求方式失败') |
| } |
| }) |
复制


3.9 请求类型说明
3.9.1 get请求
- 会将参数通过?号的形式进行拼接. http://localhost:8090/findUser?id=1&password=123456
- get请求会将所有的参数动态的拼接到URL中,相对不安全.
- Get请求不适用于大量的数据提交 各大的浏览器对于Get请求一般都是有上限的.
- 总结:
- 查询数据时使用
- 获取简单数据时使用(页面/JS/CSS…)
- 一般请求(查询)中GET请求居多
3.9.2 POST
- POST请求将所有的参数都会进行form的封装
- 如果需要传递海量的数据,则首选POST
- POST的请求使用form进行封装,相对于GET请求更加的安全
- 总结:
- 提交海量的数据时使用
- 一般提交文件时使用
- 提交方式使用POST居多