一、介绍
Spring Boot完整教程 | 主页及目录
上一篇我们对Data JPA的用法进行了基本介绍,本篇将对Data JPA API进行进一步说明。
二、用法说明
2.1 方法名解析
Data JPA在进行方法名解析时,会先把方法名多余的前缀截取掉,比如 find、findBy、read、readBy、get、getBy,然后对剩下部分进行解析。并且如果方法的最后一个参数是 Sort 或者 Pageable 类型,也会提取相关的信息,以便按规则进行排序或者分页查询。在创建查询时,我们通过在方法名中使用属性名称来表达,比如 findById()。框架在解析该方法时,首先剔除 findBy,然后对剩下的属性进行解析。
like查询时,如果需要查询的是其中的字符,同样使用通配符 “%”,如%王%
下面我们尝试一下,先做几条数据
StudentDao中增加根据性别筛选人员的方法
package com.kcsm.training.bootdemo.dao;
import com.kcsm.training.bootdemo.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import java.util.List;
/**
* 学生信息数据库访问接口
*
* @author lqk
* @date 2019/7/9 15:43
*/
public interface StudentDao extends JpaRepository<Student, String>, JpaSpecificationExecutor<Student> {
/**
* 根据性别查询学习信息
*
* @author l