spring boot mysql_如何在Spring Boot中使用MySql数据库查询?

bd96500e110b49cbb3cd949968f18be7.png

I am creating an API by using spring boot. In this project, I used spring web, JPA, jstl and MySql as dependencies of the API. In this project, I have created a Controller, Model and Repository. Basically, this API does CRUD operations. When I use GET request, I want to get only 3 columns. But, the case is I used JPA in this and I don't know how to use custom queries like

"SELECT devname,hrs,ot FROM imaginaryTable"

.

How can I do this ??

My Controller class.

@RestController

@RequestMapping("/api")

public class ImController {

@Autowired

private ImRepository TaskRepository;

@GetMapping("/projects")

public List findAll() {

return (List) TaskRepository.findAll();

}

@GetMapping("/developers/{id}")

public ImModel findByName(@PathVariable final int id){

return TaskRepository.findById(id);

}

}

My Repository interface.

package com.kisalka.pacrestapi.repository;

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

import com.kisalka.pacrestapi.model.ImModel;

public interface ImRepository extends JpaRepository {

ImModel findById(int id);

}

解决方案

You could use @Query("Your query") annotation inside the repository to query the database.

For Example

@Query(value="SELECT devname,hrs,ot FROM imaginaryTable",nativeQuery=true)

private List getValues();

Hope it solves your issue.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值