idea创建一个RESTful风格的接口程序
RESTful是一个规范,一种风格
满足RESTful规范的接口,请求地址不包含动词,用户的操作,通过HTTP请求方法来描述
例子如下
http://localhost:8080/users/1
//GET表示查询id为1的用户
//POST表示插入一个用户,且插入的用户id为1
//PUT表示更新id为1的用户
//DELETE表示删除id为1的用户
使用idea创建
加入数据库连接池依赖,并进行相应配置
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
更改mysql的版本号
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>5.1.27</version>
</dependency>
配置数据库连接
在application中配置
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/demorestful?useUnicode=true&characterEncoding=UTF-8
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=mysql
创建实体类和方法
创建Book和Mapper
Book实体类如下
package com.restfuldemo.restfuldemo.vo;
import javax.persistence.*;
/**
* @author 偷闲
* @date 2019/5/20 14:15
*/
//@Entity表示这是一个实体类,默认情况,类名就是表名,但是,也可以通过name指定
@Entity(name = "books")
public class Book {
//必须要有一个主键
@Id
//设置自增
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
//默认情况下,定义的属性名就是生成表的字段名,但是可以通过@Coloumn指定
@Column(name = "bookauthor")
private String author;
private String bookname;
private String bookprice;
//如果不想让属性在数据库中生产字段名使用 @Transient来忽略一个属性
@Transient
private String publisher;
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public String getBookprice() {
return bookprice;
}
public void setBookprice(String bookprice) {
this.bookprice = bookprice;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
Bookmapper如下
package com.restfuldemo.restfuldemo.Mapper;
import com.restfuldemo.restfuldemo.vo.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/**
* @author 偷闲
* @date 2019/5/20 14:35
*/
public interface Bookmapper extends JpaRepository<Book,Integer> {
}
到这里,已经自动的具备了自带RESTful风格增删改查的程序
测试
注册数据
运行程序,会在数据库生产books表
使用Postman进行测试
注册使用post请求,默认为小写实体类名后面加个s
添加成功
返回数据如下
{
"author": "测试1",
"bookname": "书名1",
"bookprice": "100",
"publisher": null,
"_links": {
"self": {
"href": "http://localhost:8080/books/2"
},
"book": {
"href": "http://localhost:8080/books/2"
}
}
}
查询数据/分页查询
查询使用get请求
查询单个数据
返回数据如下
{
"author": "测试1",
"bookname": "书名1",
"bookprice": "100",
"publisher": null,
"_links": {
"self": {
"href": "http://localhost:8080/books/2"
},
"book": {
"href": "http://localhost:8080/books/2"
}
}
}
分页查询
返回数据如下
{
"_embedded": {
"books": [
{
"author": "测试1",
"bookname": "书名1",
"bookprice": "100",
"publisher": null,
"_links": {
"self": {
"href": "http://localhost:8080/books/2"
},
"book": {
"href": "http://localhost:8080/books/2"
}
}
},
{
"author": "测试2",
"bookname": "书名2",
"bookprice": "100",
"publisher": null,
"_links": {
"self": {
"href": "http://localhost:8080/books/3"
},
"book": {
"href": "http://localhost:8080/books/3"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8080/books{?page,size,sort}",
"templated": true
},
"profile": {
"href": "http://localhost:8080/profile/books"
}
},
"page": {
"size": 20,
"totalElements": 2,
"totalPages": 1,
"number": 0
}
}
可以根据http://localhost:8080/books{?page,size,sort}
加上不同的参数进行不同的分页查询
修改数据
修改的时候使用PUT请求
更新id为2的数据,注意更新的时候是要将所有列都列上,不然会将没有的列置为null
返回数据如下
{
"author": "新测试1",
"bookname": "新书名2",
"bookprice": "200",
"publisher": null,
"_links": {
"self": {
"href": "http://localhost:8080/books/2"
},
"book": {
"href": "http://localhost:8080/books/2"
}
}
}
删除数据
修改的时候使用DELETE请求
删除id为2的数据
自定义查询
在BookMapper中添加方法
//查询作者名字包含传入参数的
List<Book> getBookByAuthorContains(String author);
//查询以传入参数结尾并且id大于某个数的数据
List<Book> getBookByIdGreaterThanAndAuthorContains(Integer id,String author);
通过get的search请求可以查询当前可调用的方法
返回数据如下
{
"_links": {
"getBookByAuthorContains": {
"href": "http://localhost:8080/books/search/getBookByAuthorContains{?author}",
"templated": true
},
"getBookByIdGreaterThanAndAuthorContains": {
"href": "http://localhost:8080/books/search/getBookByIdGreaterThanAndAuthorContains{?id,author}",
"templated": true
},
"self": {
"href": "http://localhost:8080/books/search"
}
}
}
如果觉得名字比较长,可以自定义名字
@RestResource(path = "likename")
List<Book> getBookByAuthorContains(String author);
可以http://localhost:8080/books/search/likename?author=测试
自定义sql语句
//查询author包含某数据的id最大的Book
@Query(value = "select * from books where bookauthor like %:author% order by id desc limit 1",nativeQuery = true)
List<Book> getAuthorOne(@Param("author") String author);
//修改需要加 @Modifying
@Query(value = "update books set bookprice =:price where id=:id",nativeQuery = true)
@Modifying
int updatebyid(@Param("price")String price,@Param("id")Integer id);
给所有的请求加上前缀
在application中加上
spring.data.rest.base-path=/api
这样就需要加上api了