SpringBoot使用SpringDataJPA通过方法名查询

JPA依赖及配置在这篇文章: SpringBoot框架使用SpringDataJPA

JPA方法名查询

约定方法名一定要根据命名规范来书写,Spring Data 会根据前缀、中间连接词(Or、And、Like、NotNull 等类似 SQL 中的关键字)、内部拼接 SQL 代理生成方法的实现,约定方法名的方法见表:

1. 方法名表图

关键词SQL符号举例最终执行的JPQL 片段
AndandfindByAddressLikeAndState… where x.lastname = ?1 and x.firstname = ?2
OrorfindByLastnameOrFirstname… where x.lastname = ?1 or x.firstname = ?2
toplimitfindTop4… where x.lastname limit (4)
Is,Equals=findByFirstname/ findByFirstnameIs / findByFirstnameEquals… where x.firstname = ?1
BetweenbetweenfindByStartDateBetween… where x.startDate between ?1 and ?2
LessThan<findByAgeLessThan… where x.age < ?1
LessThanEqual<=findByAgeLessThanEqual… where x.age <= ?1
GreaterThan>findByAgeGreaterThan… where x.age > ?1
GreaterThanEqual>=findByAgeGreaterThanEqual… where x.age >= ?1
After>findByStartDateAfter… where x.startDate > ?1
Before<findByStartDateBefore… where x.startDate < ?1
IsNullis nullfindByAgeIsNull… where x.age is null
NotNullis not nullfindByAge(Is)NotNull… where x.age not null
LikelikefindByFirstnameLike… where x.firstname like ?1
NotLikenot likefindByFirstnameNotLike… where x.firstname not like ?1
StartingWithlike ‘xxx%’findByFirstnameStartingWith… where x.firstname like ?1(parameter bound with appended %)
EndingWithlike ‘xxx%’findByFirstnameEndingWith… where x.firstname like ?1(parameter bound with prepended %)
OrderByorder byOrderByCreatetimeDesc… where x.state= ?1 order by x.createtime desc
Not<>findByLastnameNot… where x.lastname <> ?1
Inin()findByAgeIn(Collection ages)… where x.age in ?1
NotInnot in()findByAgeNotIn(Collection ages)… where x.age not in ?1
TRUE=truefindByActiveTrue()… where x.active = true
FALSE=falsefindByActiveFalse()… where x.active = false

2. 举例说明

RecruitDao 代码

package com.tensquare.recruit.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.tensquare.recruit.pojo.Recruit;

import java.util.List;

/**
 * recruit数据访问接口
 *
 * @author Administrator
 */
public interface RecruitDao extends JpaRepository<Recruit, String>, JpaSpecificationExecutor<Recruit> {
    // 查询状态为2并以创建日期降序排序,查询前4条记录 推荐职位查询
    public List<Recruit> findTop4ByStateOrderByCreatetimeDesc(String state);

    // 查询状态不为0 并以创建日期降序排序,查询前12条记录 最新职位查询
    public List<Recruit> findTop12ByStateNotOrderByCreatetimeDesc(String state);

    // 查询招聘信息地址在大望路的并且状态为2  And,Like应用查询
    public List<Recruit> findByAddressLikeAndState(String address,String state);

}

RecruitService 代码

/**
 * recruit服务层
 *
 * @author Administrator
 */
@Service
@Transactional
public class RecruitService {

    @Autowired
    private RecruitDao recruitDao;

    @Autowired
    private IdWorker idWorker;

    /*
     * 查询招聘信息地址在大望路的并且状态为2
     * */
    public List<Recruit> findAdressAndState() {
        List<Recruit> latestJobList = recruitDao.findByAddressLikeAndState("%大望路%", "2");
        return latestJobList;
    }

    /*
     * 最新职位查询
     * */

    public List<Recruit> theLatestJob() {
        List<Recruit> latestJobList = recruitDao.findTop12ByStateNotOrderByCreatetimeDesc("0");
        return latestJobList;
    }

    /*
     * 查询前四条推荐职位
     * */
    public List<Recruit> recommendPosition() {
        List<Recruit> tuijianList = recruitDao.findTop4ByStateOrderByCreatetimeDesc("2");
        return tuijianList;
    }
  
}

RecruitController 代码

/**
 * recruit控制器层
 *
 * @author Administrator
 */
@RestController
@CrossOrigin
@RequestMapping("/recruit")
public class RecruitController {

    @Autowired
    private RecruitService recruitService;

    /*
     * 查询招聘信息地址在大望路的并且状态为2
     * */
    @RequestMapping(value = "/search/filterList", method = RequestMethod.GET)
    public Result findByAddressLikeAndState() {
        List<Recruit> recruits = recruitService.findAdressAndState();
        return new Result(true, StatusCode.OK, "查询成功", recruits);
    }

    /*
     * 最新职位查询
     * */
    @RequestMapping(value = "/search/newlist", method = RequestMethod.GET)
    public Result theLatestJob() {
        List<Recruit> recruits = recruitService.theLatestJob();
        return new Result(true, StatusCode.OK, "查询成功", recruits);
    }

    /*
     * 推荐职位查询
     * */
    @RequestMapping(value = "/search/recommend", method = RequestMethod.GET)
    public Result recommendPosition() {
        List<Recruit> recruits = recruitService.recommendPosition();
        return new Result(true, StatusCode.OK, "查询成功", recruits);
    }
}

3. 测试

按地址和状态查询:

在这里插入图片描述

最新职位查询

在这里插入图片描述

推荐职位查询

在这里插入图片描述

引用资料: https://blog.csdn.net/weixin_43094965/article/details/117203091

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叫我三胖哥哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值