SpringBoot使用JPA搭建RESTful风格

一、创建项目并导入依赖

   

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-rest</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid-spring-boot-starter</artifactId>

<version>1.1.10</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

<version>5.1.27</version>

</dependency>

   

   

二、相关配置和代码

   

2.1

Application.properties

   

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/jpa_restful?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT

spring.datasource.username=root

spring.datasource.password=123

   

spring.jpa.show-sql=true

spring.jpa.database-platform=mysql

spring.jpa.database=mysql

spring.jpa.hibernate.ddl-auto=update

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect

   

配置就不解释了,详情请看

https://www.cnblogs.com/fernfei/p/12113045.html

   

2.1

pojo,dao层

   

   

   

   

三、使用Postman工具测试

   

3.1

增删改

   

增加:

   

Localhost:8080/users,中的users是对应你实体类的user但是要加s

修改:

   

   

删除:

   

   

删除更简单直接川id就行

   

   

   

3.2

查询

   

查询分两种一是默认提供的查询,二是自定义查询

   

默认查询:

   

自定义查询:

JPA自定义规则请看https://www.cnblogs.com/fernfei/p/12113045.html

   

   

   

3.3

自定义查询路径

   

方法上:

   

@RestResource(path="byname",rel="findbyname")

   

exported :是否暴露出自定义的接口

   

path:

修改掉红框的太长接口名

   

rel:

替换掉红框

   

   

类上:

   

   

@RepositoryRestResource(path="query",collectionResourceRel="collectionuser",itemResourceRel="itemuser")

   

exported:同上

   

path:

   

   

collectionResourceRel:

   

   

itemResouceRel:

   

   

   

   

   

效果图:

   

   

   

   

   

Appliction.perterites配置路径

   

   

   

spring.data.rest.base-path=/api

http://localhost:8080/api/users

spring.data.rest.return-body-on-create=true

默认为true,添加时返回添加的记录

spring.data.rest.return-body-on-update=true

默认为true,修改时返回修改的记录

spring.data.rest.default-page-size

默认一页的记录数

spring.data.rest.max-page-size

最大的记录数

spring.data.rest.limit-param-name

   

spring.data.rest.page-param-name

   

spring.data.rest.sort-param-name

http://localhost:8080/api/users?page=0&size=2&sort=id,desc

自定义分页的参数名

limit对应size,page对应page,sort对应sort

spring.data.rest.default-media-type

默认参数的数据类型,比如上面例子postman里面添加修改时选的是json类型

   

   

通过配置类配置

   

   

   

Application.perteries里面配置是一模一样的,配置类优先级高于application.proteries 所有 两个同时存在时配置类生效

   

   

   

   

   

   

   

   

   

四、REST配置跨域

   

   

   

两种配置方法

1:全局配置

   

查看该篇博客https://www.cnblogs.com/fernfei/p/12079478.html

   

2:dao层配置跨域也一样行

   

猜你喜欢

转载自www.cnblogs.com/fernfei/p/12169405.html