【Springboot学习 | 5】jpa全查询+主键查询+按属性查询+模糊查询

一、jpa中查询方法

查询方法jpa方法名返回
1全查询findAll()list集合
2主键查询findById()Optional< UserEntity>实体对象
3自定义条件查询findByNameAndPassword(String name, Long password)UserEntity实体
自定义模糊查询findByNameLike("%"+name+"%")list集合
注意:自定义查询需要在jpa中声明自定义的方法,确保命名规范!

二、具体实现

controller中:
在这里插入图片描述
模糊查询:
在这里插入图片描述
这里给出四种方法部分代码,全部代码见github:UserController.java

    @Autowired
    private UserJPA userJPA;
    @RequestMapping(value ="/queryUser",method = RequestMethod.GET)
    public UserEntity login(String name,Long password) {
        UserEntity userEntity= userJPA.findByNameAndPassword(name,password);
        return userEntity;
    }
    @RequestMapping(value ="/findUser",method = RequestMethod.GET)
    public List<UserEntity> find(String name) {
        return userJPA.findByNameLike("%"+name+"%");
    }
    @RequestMapping(value ="/info",method = RequestMethod.GET)
    public Optional<UserEntity> userInfo(Long id) {
        return userJPA.findById(id);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<UserEntity> list() {
        return userJPA.findAll();//SpringDataJPA为我们提供的内置方法,它可以查询表内所有的数据
    }

在这里插入图片描述
声明:UserJPA.java

UserEntity findByNameAndPassword(String name, Long password);
List<UserEntity> findByNameLike(String name);

三、查询效果

3.1 关键字查询,及模糊查询

自定义关键词查询请求:
http://localhost:8080/api/user/queryUser?name=test&password=123456
在这里插入图片描述
数据库查询无结果返回nul,有相应内容返回json:(这种适合登录判断)
在这里插入图片描述
模糊查询请求:http://localhost:8080/api/user/findUser?name=n
返回结果:(这适合输入关键词、下拉框等模糊查询)
在这里插入图片描述

3.2 主键查询

请求:http://localhost:8080/api/user/info?id=3
结果:
在这里插入图片描述

3.3 全查询

请求:http://localhost:8080/api/user/info?id=3
结果:
在这里插入图片描述

总结

今天学习了jpa全查询+主键查询+全查询的三种方法,

用途
自定义属性查询登录(name,password)
主键(id)查询登录后显示个人信息
全查询表格,图表显示 (list)
关键字模糊查询`输入框、下拉框模糊查询
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值