Spring Boot:整合Spring Data JPA
综合概述
JPA 是 Java Persistence API 的简称,是一套 Sun 官方提出的 Java 持久化规范。其设计目标主要是为了简化现有的持久化开发工作和整合 ORM 技术,它为 Java 开发人员提供了一种 ORM 工具来管理 Java 应用中的关系数据。 简而言之,JPA 提供了使用面向对象的方式操作数据库的功能。JPA 充分吸收了现有 Hibernate,TopLink,JDO 等 ORM 框架的优势,具有易于使用、伸缩性强等优点。
Spring Data JPA 是 Spring 基于 Spring Data 框架对于 JPA 规范的一套具体实现方案,使用 Spring Data JPA 可以极大地简化 JPA 的写法,几乎可以在不写具体实现的情况下完成对数据库的操作,并且除了基础的 CRUD 操作外,Spring Data JPA 还提供了诸如分页和排序等常用功能的实现方案。合理的使用 Spring Data JPA 可以极大的提高我们的日常开发效率和有效的降低项目开发成本。
实现案例
接下来,我们就通过实际案例来讲解 Spring Data JPA 的整合,以及提供 JPA 相关操作的一些示例。
生成项目模板
为方便我们初始化项目,Spring Boot 给我们提供一个项目模板生成网站。
1. 打开浏览器,访问:https://start.spring.io/
2. 根据页面提示,选择构建工具,开发语言,项目信息等。
3. 点击 Generate the project,生成项目模板,生成之后会将压缩包下载到本地。
4. 使用 IDE 导入项目,我这里使用 Eclipse,通过导入 Maven 项目的方式导入。
添加相关依赖
清理掉不需要的测试类及测试依赖,添加 Maven 相关依赖,这里需要添加上 WEB 和 Swagger 和 JPA 的依赖,Swagger 的添加是为了方便接口测试。
<?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 http://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>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.louis.springboot</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description><properties> <java.version><span style="color: rgba(128, 0, 128, 1)">1.8</span></java.version> </properties> <dependencies> <!-- web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version><span style="color: rgba(128, 0, 128, 1)">2.9</span>.<span style="color: rgba(128, 0, 128, 1)">2</span></version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version><span style="color: rgba(128, 0, 128, 1)">2.9</span>.<span style="color: rgba(128, 0, 128, 1)">2</span></version> </dependency> <!-- jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <!-- 打包时拷贝MyBatis的映射文件 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/sqlmap<span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>*</span><span style="color: rgba(0, 128, 0, 1)">*/</span>*.*</include> </includes> <filtering><span style="color: rgba(0, 0, 255, 1)">true</span></filtering> </resource> </resources> </build>
</project>
添加相关配置
1. 添加数据源配置
将 application.properties 文件改名为 application.yml ,并在其中添加 MySQL 数据源连接信息。
注意:
这里需要首先创建一个 MySQL 数据库,并输入自己的用户名和密码。这里的数据库是 springboot。
另外,如果你使用的是 MySQL 5.x 及以前版本,驱动配置 driverClassName 是 com.mysql.jdbc.Driver。
application.yml
server: port: 8080 spring: datasource: driverClassName: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8 username: root password: 123456 jpa: show-sql: true # 默认 false,在日志里显示执行的 sql 语句 database: mysql hibernate.ddl-auto: update #指定为 update,每次启动项目检测表结构有变化的时候会新增字段,表不存在时会新建,如果指定 create,则每次启动项目都会清空数据并删除表,再新建 properties.hibernate.dialect: org.hibernate.dialect.MySQL5Dialect database-platform: org.hibernate.dialect.MySQL5Dialect hibernate: naming: implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl #指定 jpa 的自动表生成策略,驼峰自动映射为下划线格式 #physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
2. 添加 swagger 配置
添加一个 swagger 配置类,在工程下新建 config 包并添加一个 SwaggerConfig 配置类。
SwaggerConfig.java
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration
@EnableSwagger2
public class SwaggerConfig {@Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()).build(); } private ApiInfo apiInfo(){ return new ApiInfoBuilder() .title("SpringBoot API Doc") .description("This is a restful api document of Spring Boot.") .version("1.0") .build(); }
}
编写业务代码
首先,编写一个实体类,并添加相关注解,具体注解说明参见代码。
SysUser.java
package com.louis.springboot.demo.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;
@Entity // @Entity: 实体类, 必须
// @Table: 对应数据库中的表, 必须, name= 表名, Indexes 是声明表里的索引, columnList 是索引的列, 同时声明此索引列是否唯一, 默认 false
@Table(name = "sys_user", indexes = {@Index(name = "id", columnList = "id", unique = true), @Index(name = "name", columnList = "name", unique = true)})
public class SysUser {
@Id </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> @Id: 指明id列, 必须</span>
@GeneratedValue(strategy = GenerationType.IDENTITY) <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> @GeneratedValue: 表明是否自动生成, 必须, strategy也是必写, 指明主键生成策略, 默认是Oracle</span>
<span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Long id;
@Column(name </span>= <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">name</span><span style="color: rgba(128, 0, 0, 1)">"</span>, nullable = <span style="color: rgba(0, 0, 255, 1)">false</span>) <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> @Column: 对应数据库列名,可选, nullable 是否可以为空, 默认true</span>
<span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String name;
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String password;
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String email;
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Long getId() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> id;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setId(Long id) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.id =<span style="color: rgba(0, 0, 0, 1)"> id;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getName() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> name;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setName(String name) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getPassword() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> password;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setPassword(String password) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.password =<span style="color: rgba(0, 0, 0, 1)"> password;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getEmail() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> email;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setEmail(String email) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.email =<span style="color: rgba(0, 0, 0, 1)"> email;
}
}
然后,编写一个 SysUserDao 并继承 JpaRepository,由此我们已经继承了大部分可用的 CURD 操作,针对基础操作,DAO 完全不用写任何方法。
SysUserDao.java
package com.louis.springboot.demo.dao;
import java.io.Serializable;
import org.springframework.data.jpa.repository.JpaRepository;
import com.louis.springboot.demo.model.SysUser;
public interface SysUserDao extends JpaRepository<SysUser, Long>, Serializable {
}
使用 Spring Data JPA,可以通过两种方式使用 JPA 进行数据持久化。
方式一:使用 Spring Data JPA 提供的接口默认实现,如上面我们的 DAO 实现。
方式二:自定义符合 Spring Data JPA 规则的查询方法,由框架将其自动解析为 SQL。
Spring Data JPA 提供了一些实现了基本的数据库操作的接口类,这些接口和类的关系如下。
其中 CrudRepository 是顶层 CURD 接口,提供了一些简单的增删查改功能,接口定义如下。
CrudRepository.java
package org.springframework.data.repository;
import java.util.Optional;
/
-
Interface for generic CRUD operations on a repository for a specific type.
-
@author Oliver Gierke
-
@author Eberhard Wolff
*/
@NoRepositoryBean
public interface CrudRepository<T, ID> extends Repository<T, ID> {
/
- Saves a given entity. Use the returned instance for further operations as the save operation might have changed the
- entity instance completely.
- @param entity must not be {@literal null}.
- @return the saved entity will never be {@literal null}.
*/
<S extends T> S save(S entity);
/
- Saves all given entities.
- @param entities must not be {@literal null}.
- @return the saved entities will never be {@literal null}.
- @throws IllegalArgumentException in case the given entity is {@literal null}.
*/
<S extends T> Iterable<S> saveAll(Iterable<S> entities);
/
- Retrieves an entity by its id.
- @param id must not be {@literal null}.
- @return the entity with the given id or {@literal Optional#empty()} if none found
- @throws IllegalArgumentException if {@code id} is {@literal null}.
*/
Optional<T> findById(ID id);
/
- Returns whether an entity with the given id exists.
- @param id must not be {@literal null}.
- @return {@literal true} if an entity with the given id exists, {@literal false} otherwise.
- @throws IllegalArgumentException if {@code id} is {@literal null}.
*/
boolean existsById(ID id);
/
- Returns all instances of the type.
- @return all entities
*/
Iterable<T> findAll();
/
- Returns all instances of the type with the given IDs.
- @param ids
- @return
*/
Iterable<T> findAllById(Iterable<ID> ids);
/
- Returns the number of entities available.
- @return the number of entities
*/
long count();
/
- Deletes the entity with the given id.
- @param id must not be {@literal null}.
- @throws IllegalArgumentException in case the given {@code id} is {@literal null}
*/
void deleteById(ID id);
/
- Deletes a given entity.
- @param entity
- @throws IllegalArgumentException in case the given entity is {@literal null}.
*/
void delete(T entity);
/
- Deletes the given entities.
- @param entities
- @throws IllegalArgumentException in case the given {@link Iterable} is {@literal null}.
*/
void deleteAll(Iterable<? extends T> entities);
/
- Deletes all entities managed by the repository.
*/
void deleteAll();
}
PagingAndSortingRepository 在继承了 CrudRepository 基础上实现了排序和分页的方法。
PagingAndSortingRepository.java
package org.springframework.data.repository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
/
-
Extension of {@link CrudRepository} to provide additional methods to retrieve entities using the pagination and
-
sorting abstraction.
-
@author Oliver Gierke
-
@see Sort
-
@see Pageable
-
@see Page
*/
@NoRepositoryBean
public interface PagingAndSortingRepository<T, ID> extends CrudRepository<T, ID> {
/
- Returns all entities sorted by the given options.
- @param sort
- @return all entities sorted by the given options
*/
Iterable<T> findAll(Sort sort);
/
- Returns a {@link Page} of entities meeting the paging restriction provided in the {@code Pageable} object.
- @param pageable
- @return a page of entities
*/
Page<T> findAll(Pageable pageable);
}
JpaRepository 又在继承 PagingAndSortingRepository 的基础上,同时继承了 QueryByExampleExecutor 接口,使其拥有了匹配指定样例查询的能力。
JpaRepository.java
package org.springframework.data.jpa.repository;
import java.util.List;
import javax.persistence.EntityManager;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;
/
-
JPA specific extension of {@link org.springframework.data.repository.Repository}.
-
@author Oliver Gierke
-
@author Christoph Strobl
-
@author Mark Paluch
*/
@NoRepositoryBean
public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> {
/*
- (non-Javadoc)
- @see org.springframework.data.repository.CrudRepository#findAll()
*/
List<T> findAll();
/*
- (non-Javadoc)
- @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort)
*/
List<T> findAll(Sort sort);
/*
- (non-Javadoc)
- @see org.springframework.data.repository.CrudRepository#findAll(java.lang.Iterable)
*/
List<T> findAllById(Iterable<ID> ids);
/*
- (non-Javadoc)
- @see org.springframework.data.repository.CrudRepository#save(java.lang.Iterable)
*/
<S extends T> List<S> saveAll(Iterable<S> entities);
/
- Flushes all pending changes to the database.
*/
void flush();
/
- Saves an entity and flushes changes instantly.
- @param entity
- @return the saved entity
*/
<S extends T> S saveAndFlush(S entity);
/
- Deletes the given entities in a batch which means it will create a single {@link Query}. Assume that we will clear
- the {@link javax.persistence.EntityManager} after the call.
- @param entities
*/
void deleteInBatch(Iterable<T> entities);
/
- Deletes all entities in a batch call.
*/
void deleteAllInBatch();
/
- Returns a reference to the entity with the given identifier. Depending on how the JPA persistence provider is
- implemented this is very likely to always return an instance and throw an
- {@link javax.persistence.EntityNotFoundException} on first access. Some of them will reject invalid identifiers
- immediately.
- @param id must not be {@literal null}.
- @return a reference to the entity with the given identifier.
- @see EntityManager#getReference(Class, Object) for details on when an exception is thrown.
*/
T getOne(ID id);
/*
- (non-Javadoc)
- @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example)
*/
@Override
<S extends T> List<S> findAll(Example<S> example);
/*
- (non-Javadoc)
- @see org.springframework.data.repository.query.QueryByExampleExecutor#findAll(org.springframework.data.domain.Example, org.springframework.data.domain.Sort)
*/
@Override
<S extends T> List<S> findAll(Example<S> example, Sort sort);
}
上面因为我们的 SysUserDao 直接继承了 JpaRepository,所以上述所有的接口 SysUserDao 都是可以直接使用的,当然,除了可以直接使用默认提供的基础接口外,Spring Data JPA 还允许我们自定义查询方法,对于符合以下命名规则的方法,Spring Data JPA 能够根据其方法名为其自动生成 SQL,除了使用示例中的 find 关键字,还支持的关键字有:query、get、read、count、delete 等。
只要按照以下命名规范的定义的方法,Spring Data JPA 都能够帮我们自动生成 SQL,无需自己实现。
接着编写一个服务接口,添加用户保存、删除、查询全部和分页查询的方法。
SysUserService.java
package com.louis.springboot.demo.service;
import java.util.List;
import com.louis.springboot.demo.model.SysUser;
import com.louis.springboot.demo.util.PageQuery;
public interface SysUserService {
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 保存用户
* @param user
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> save(SysUser user);
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 删除用户
* @param id
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> delete(SysUser user);
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 查询全部用户
* @return
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span> List<SysUser><span style="color: rgba(0, 0, 0, 1)"> findAll();
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* 查询分页数据
* @return
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Object findPage(PageQuery pageQuery);
}
继续编写服务实现类并调用 DAO 实现相应功能,以下 DAO 方法都是继承而来的,除此之后,JPA 还提供了大量的 API 可用。
SysUserServiceImpl.java
package com.louis.springboot.demo.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import com.louis.springboot.demo.dao.SysUserDao;
import com.louis.springboot.demo.model.SysUser;
import com.louis.springboot.demo.service.SysUserService;
import com.louis.springboot.demo.util.PageQuery;
@Service
public class SysUserServiceImpl implements SysUserService {
@Autowired
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> SysUserDao sysUserDao;
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> save(SysUser user) {
sysUserDao.save(user);
}
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> delete(SysUser user) {
sysUserDao.delete(user);
}
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span> List<SysUser><span style="color: rgba(0, 0, 0, 1)"> findAll() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> sysUserDao.findAll();
}
@Override
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Object findPage(PageQuery pageQuery) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> sysUserDao.findAll(PageRequest.of(pageQuery.getPage(), pageQuery.getSize()));
}
}
接着编写一个用户控制器,调用服务接口实现对应功能。
SysUserController.java
package com.louis.springboot.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.louis.springboot.demo.model.SysUser;
import com.louis.springboot.demo.service.SysUserService;
import com.louis.springboot.demo.util.PageQuery;
@RestController
@RequestMapping("user")
public class SysUserController {
@Autowired
</span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> SysUserService sysUserService;
@PostMapping(value</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/save</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Object save(@RequestBody SysUser user) {
sysUserService.save(user);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">;
}
@PostMapping(value</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/delete</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Object delete(@RequestBody SysUser user) {
sysUserService.delete(user);
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">;
}
@GetMapping(value</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/findAll</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Object findAll() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> sysUserService.findAll();
}
@PostMapping(value</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/findPage</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Object findPage(@RequestBody PageQuery pageQuery) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> sysUserService.findPage(pageQuery);
}
}
上面对分页请求进行了简单的封装,主要包含查询页码和每页数量两个属性。
PageQuery.java
package com.louis.springboot.demo.util;
public class PageQuery {
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> page;
</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> size;
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getPage() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> page;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setPage(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> page) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.page =<span style="color: rgba(0, 0, 0, 1)"> page;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> getSize() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> size;
}
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> setSize(<span style="color: rgba(0, 0, 255, 1)">int</span><span style="color: rgba(0, 0, 0, 1)"> size) {
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.size =<span style="color: rgba(0, 0, 0, 1)"> size;
}
}
编译测试运行
1. 右键项目 -> Run as -> Maven install,开始执行 Maven 构建,第一次会下载 Maven 依赖,可能需要点时间,如果出现如下信息,就说明项目编译打包成功了。
2. 打开数据库,创建一个 springboot 数据库,然后右键文件 DemoApplication.java -> Run as -> Java Application,开始启动应用,如果一开始数据库没有对应的表,在应用启动时会创建,我们可以通过控制台查看到对应的 SQL 语句。
3. 打开浏览器,访问:http://localhost:8080/swagger-ui.html,进入 swagger 接口文档界面。
4. 首先访问 findAll 接口,此时并没有数据,所以返回结果为空。
然后调用 save 接口,分别插入以下三条数据。
{
"id": 1,
"name": "111",
"email": "111@qq.com",
"password": "111"
}
{
"id": 2,
"name": "222",
"email": "222@qq.com",
"password": "222"
}
{
"id": 3,
"name": "333",
"email": "333@qq.com",
"password": "333"
}
接着回来继续调用 findAll 接口,可以看到我们已经成功的插入了三条数据。
接着测试分页查询接口 findPage,输入 { "page": 0, "size": 2 },标识查询第一页,每页显示两条记录,下面返回正确的分页查询数据。
最后我们测试一下删除接口 delete,删除掉 id 为 1 的数据,再次调用 findAll 接口,我们发现目标记录已经成功被删除。
参考资料
项目主页:https://spring.io/projects/spring-data-jpa
参考文档:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/
网上资料:http://www.360doc.com/content/17/0801/09/16915_675758662.shtml
网上资料:https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-jpa/index.html
相关导航
源码下载
码云:https://gitee.com/liuge1988/spring-boot-demo.git
作者:朝雨忆轻尘
出处:https://www.cnblogs.com/xifengxiaoma/
版权所有,欢迎转载,转载请注明原文作者及出处。