Spring Data JPA 必须掌握的查询关键字 让你开发事半功倍

方法命名规则查询

框架在进行方法名解析时,会先把方法名多余的前缀截取掉,比如 find、findBy、read、readBy、get、 getBy,然后对剩下部分进行解析。并且如果方法的最后一个参数是 Sort 或者 Pageable 类型,也会提取相 关的信息,以便按规则进行排序或者分页查询,用这种方法你甚至可以不写Sql语句。如下表列举部分关键字抽象方法的含义:
在这里插入图片描述在这里插入图片描述
代码如下

package com.mml.service;

import com.mml.entity.Students;

import javax.persistence.criteria.CriteriaBuilder;
import java.time.LocalDateTime;
import java.util.List;

public interface StudentsService {
    Students InsertStudents(Students students);

    void deleteById(Integer stId);

    Students updateById(Students students);

    List<Students> findAll();

    /**
     * 查询
     * @param stName
     * @return
     */
    List<Students> findByStNameContaining(String stName);

    /**
     * And 关键字查询姓名和年龄 返回的结果必须符合两个条件
     * @param stName
     * @param stAge
     * @return
     */
    List<Students> findByStNameAndStAge(String stName,Integer stAge);

    /**
     * 使用Or 关键字查询姓名和年龄查询出的结果只需符合其中一个即可
     * @param stName
     * @param stAge
     * @return
     */
    List<Students> findByStNameOrStAge(String stName,Integer stAge);

    /**
     * 使用 Is关键字
     * @param stName
     * @return
     */
    List<Students> findByStNameIs(String stName);

    /**
     * 使用equals关键字
     * @param stName
     * @return
     */
    List<Students> findByStNameEquals(String stName);

    /**
     * 使用between关键字查询年龄 满足第一个条件到第二个条件的区间范围即可
     * @param stAge
     * @return
     */
    List<Students> findByStAgeBetween(Integer stAgeb,Integer stAgea);

    /**
     * LessThAn 关键词查询小于
     * @param stAge
     * @return
     */
    List<Students> findByStAgeLessThan(Integer stAge);

    /**
     * LessThanEqual查询小于等于
     * @param stAge
     * @return
     */
    List<Students> findByStAgeLessThanEqual(Integer stAge);

    /**
     * GreaterThan查询大于的
     * @param stAge
     * @return
     */
    List<Students> findByStAgeGreaterThan(Integer stAge);

    /**
     * GreaterThanEqual查询大于等于的
     * @param stAge
     * @return
     */
    List<Students> findByStAgeGreaterThanEqual(Integer stAge);

    /**
     * after类似于LessThan
     * @param stAge
     * @return
     */
    List<Students> findByStBirthdayAfter(LocalDateTime stBirthday);
    /**
     * Before类似于GreaterThan
     * @param stAge
     * @return
     */
    List<Students> findByStBirthdayBefore(LocalDateTime stBirthday);

    /**
     * IsNull 查找年龄为空的
     * @param StAge
     * @return
     */
    List<Students> findByStAgeIsNull();

    /**
     * isNotNUll和NotNull作用一样都是查找不为空的
     * @param StAge
     * @return
     */
    List<Students> findByStAgeIsNotNull();

    List<Students> findByStAgeNotNull();

    /**
     * Like本来以为是模糊查询,但是生成语句的时候没有加通配符
     * @param stName
     * @return
     */
    List<Students> findByStNameLike(String stName);
    /**
     * NotLike查出不符合的结果
     * @param stName
     * @return
     */
    List<Students> findByStNameNotLike(String stName);

    /**
     * 类似于数据库中的模糊查询以xx开头的
     * @param stName
     * @return
     */
    List<Students> findByStNameStartingWith(String stName);

    /**
     * 类似于数据库的模糊查询以xx结尾的
     * @param stName
     * @return
     */
    List<Students> findByStNameEndingWith(String stName);

    /**
     * 根据输入的年龄并且按照姓名倒序排列
     * @param stAge
     * @return
     */
    List<Students> findByStAgeOrderByStNameDesc(Integer stAge);

    /**
     * 查找包含多个指定age返回的数据
     * @param stAge
     * @return
     */
    List<Students> findByStAgeIn(List<Integer> stAge);

    /**
     * 查找不包含指定age数据
     * @param stAge
     * @return
     */
    List<Students> findByStAgeNotIn(List<Integer> stAge);

    /**
     * 是将字段的内容转换成大写进行返回  注意:字段的数据类型必须是字符串类型
     * @param stName
     * @return
     */
    List<Students> findByStNameIgnoreCase(String stName);


 }

Controller测试代码如下传参方式用的是Restful传参风格

package com.mml.controller;

import com.mml.entity.Students;
import com.mml.service.StudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RestController
public class StudentsController {
    @Autowired
    private StudentsService sts;

    @RequestMapping("/insertStudents")
    public Object findByName(Students students){
         Students s=sts.InsertStudents(students);
        return s;
    }
    @RequestMapping("/deleteById")
    public void deleteById(Integer stId){
       sts.deleteById(stId);
    }

    @RequestMapping("/findAll")
    public Object findAll(){

        return sts.findAll();
    }

    @RequestMapping("/upDateById")
    public Object upDateById(Students students){

        return sts.updateById(students);
    }


    @RequestMapping("/findAllStudent/{stName}")
    public Object findAllStudent(@PathVariable String stName){

         List<Students> students=sts.findByStNameContaining(stName);
        return students;
    }

    @RequestMapping("/findByStNameAndStAge/{stName}/{stAge}")
    public Object findByStNameAndStAge(@PathVariable String stName,@PathVariable Integer stAge){

        List<Students> students=sts.findByStNameAndStAge(stName,stAge);
        return students;
    }


    @RequestMapping("/findByStNameOrStAge/{stName}/{stAge}")
    public Object findByStNameOrStAge(@PathVariable String stName,@PathVariable Integer stAge){

        List<Students> students=sts.findByStNameOrStAge(stName,stAge);
        return students;
    }

    @RequestMapping("/findByStNameIs/{stName}")
    public Object findByStNameIs(@PathVariable String stName){

        List<Students> students=sts.findByStNameIs(stName);
        return students;
    }

    @RequestMapping("/findByStNameEquals/{stName}")
    public Object findByStNameEquals(@PathVariable String stName){

        List<Students> students=sts.findByStNameEquals(stName);
        return students;
    }

    @RequestMapping("/findByStAge/{stAgea}/{stAgeb}")
    public Object findByStAge(@PathVariable Integer stAgea,@PathVariable Integer stAgeb){

        List<Students> students=sts.findByStAgeBetween(stAgea,stAgeb);
        return students;
    }

    @RequestMapping("/findByLessThan/{stAge}")
    public Object findByLessThan(@PathVariable Integer stAge){

        List<Students> students=sts.findByStAgeLessThan(stAge);
        return students;
    }

    @RequestMapping("/findByLessThanEqual/{stAge}")
    public Object findByLessThanEqual(@PathVariable Integer stAge){

        List<Students> students=sts.findByStAgeLessThanEqual(stAge);
        return students;
    }


    @RequestMapping("/findByStAgeGreaterThan/{stAge}")
    public Object findByStAgeGreaterThan(@PathVariable Integer stAge){

        List<Students> students=sts.findByStAgeGreaterThan(stAge);
        return students;
    }


    @RequestMapping("/findByStAgeGreaterThanEqual/{stAge}")
    public Object findByStAgeGreaterThanEqual(@PathVariable Integer stAge){

        List<Students> students=sts.findByStAgeGreaterThanEqual(stAge);
        return students;
    }

    @RequestMapping("/findByStBirthdayAfter/{stBirthday}")
    public Object findByStBirthdayAfter(@PathVariable String stBirthday){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime st=LocalDateTime.parse(stBirthday,df);
        List<Students> students=sts.findByStBirthdayAfter(st);
        return students;
    }


    @RequestMapping("/findByStBirthdayBefore/{stBirthday}")
    public Object findByStBirthdayBefore(@PathVariable String stBirthday){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime st=LocalDateTime.parse(stBirthday,df);
        List<Students> students=sts.findByStBirthdayBefore(st);
        return students;
    }



    @RequestMapping("/findByStAgeIsNull")
    public Object findByStAgeIsNull(){

        List<Students> students=sts.findByStAgeIsNull();
        return students;
    }


    @RequestMapping("/findByStAgeIsNotNull")
    public Object findByStAgeIsNotNull(){

        List<Students> students=sts.findByStAgeIsNotNull();
        return students;
    }


    @RequestMapping("/findByStAgeNotNull")
    public Object findByStAgeNotNull(){

        List<Students> students=sts.findByStAgeNotNull();
        return students;
    }

    @RequestMapping("/findByStNameLike/{stName}")
    public Object findByStNameLike(@PathVariable String stName){

        List<Students> students=sts.findByStNameLike(stName);
        return students;
    }

    @RequestMapping("/findByStNameNotLike/{stName}")
    public Object findByStNameNotLike(@PathVariable String stName){

        List<Students> students=sts.findByStNameNotLike(stName);
        return students;
    }

    @RequestMapping("/findByStNameStartingWith/{stName}")
    public Object findByStNameStartingWith(@PathVariable String stName){

        List<Students> students=sts.findByStNameStartingWith(stName);
        return students;
    }

    @RequestMapping("/findByStNameEndingWith/{stName}")
    public Object findByStNameEndingWith(@PathVariable String stName){

        List<Students> students=sts.findByStNameEndingWith(stName);
        return students;
    }

    @RequestMapping("/findByStAgeOrderByStName/{stAge}")
    public Object findByStAgeOrderByStName(@PathVariable Integer stAge){

        List<Students> students=sts.findByStAgeOrderByStNameDesc(stAge);
        return students;
    }

    @RequestMapping("/findByStAgeIn/{stAge}")
    public Object findByStAgeIn(@PathVariable List<Integer> stAge){

        List<Students> students=sts.findByStAgeIn(stAge);
        return students;
    }


    @RequestMapping("/findByStAgeNotIn/{stAge}")
    public Object findByStAgeNotIn(@PathVariable List<Integer> stAge){

        List<Students> students=sts.findByStAgeNotIn(stAge);
        return students;
    }

    @RequestMapping("/findByStNameIgnoreCase/{stName}")
    public Object findByStNameIgnoreCase(@PathVariable String stName){

        List<Students> students=sts.findByStNameIgnoreCase(stName);
        return students;
    }


}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值