快速构建RESTful应用

🎈博客主页:🌈我的主页🌈
🎈欢迎点赞 👍 收藏 🌟留言 📝 欢迎讨论!👏
🎈本文由 【泠青沼~】 原创,首发于 CSDN🚩🚩🚩
🎈由于博主是在学小白一枚,难免会有错误,有任何问题欢迎评论区留言指出,感激不尽!🌠个人主页



🌟 一、RESTFUL

RESTFUL是一种网络应用程序的设计风格和开发方式,基于HTTP,可以使用XML格式定义或JSON格式定义。RESTFUL适用于移动互联网厂商作为业务接口的场景,实现第三方OTT调用移动网络资源的功能,动作类型为新增、变更、删除所调用资源

🌟 二、项目配置文件

🌟🌟 2.1、POM.XML

		//SpringDataJPA依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        //restful依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        //SpringWeb依赖
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
		//Mysql驱动
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

🌟🌟 2.2、application.properties

//数据库配置mysql
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/user?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8
//JPA配置
spring.jpa.database=mysql
spring.jpa.database-platform=mysql
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect

🌟 三、实体类

@Entity(name = "user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String username;
    private String password;
    GetSet方法省略...
}

🌟 四、持久层

public interface UserDao extends JpaRepository<User,Integer> {
}

不需要编写任何有关数据的操作,遵守restful规范的请求会自动被执行,去查询数据库

🌟 五、测试结果

  • GET 根据id查询用户(users是实体类名小写加s)

http://localhost:8080/users/2
在这里插入图片描述

  • GET 分页查询所有用户
    http://localhost:8080/users
    在这里插入图片描述
    在这里插入图片描述
  • GET 分页查询用户指定当前页和每页的记录数
    http://localhost:8080/users?page=0&size=1
    在这里插入图片描述
  • GET 分页查询用户指定当前页和每页的记录数并且按照id排序(倒序)
    http://localhost:8080/users?page=0&size=1&sort=id,desc
    在这里插入图片描述
  • POST 修改更新用户数据(在请求体中写上JSON格式的更新内容)
    http://localhost:8080/users
    {“username”:“lqz1”,“password”:“root1”}
    在这里插入图片描述
  • PUT 添加一条数据指定id(在请求体中写上JSON格式的添加内容)
    http://localhost:8080/users/4
    {“username”:“lqz123”,“password”:“root1”}
    在这里插入图片描述
  • DELETE 根据id删除用户(没有响应体)
    http://localhost:8080/users/4
    在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值