1.JPA通过方法名解析数据可操作方式,其中Stock通过继承JpaRepository接口所指定的泛型决定的。
package com.test.repo;
import com.test.model.Stock;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @Author: 沐槿
* @Description: TODO
* @Date: 2025/12/15 14:38
* @Version: 1.0
*/
// 用@Component注解放入Spring容器中
@Component
public interface StockRepo extends JpaRepository<Stock, Integer> {
//JPA将根据方法名自动拼接查询语句
//JPA根据方法名拼接出如下数据库
//select * from Stock where name = #name
public List<Stock> findByName(String name);
2.通过@Query查询数据,实现基于JPQL的方式查询数据。
Repo类
package com.test.repo;
import com.test.model.Stock;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @Author: 沐槿
* @Description: TODO
* @Date: 2025/12/15 14:38
* @Version: 1.0
*/
// 用@Component注解放入Spring容器中
@Component
public interface StockRepo extends JpaRepository<Stock, Integer> {
//JPA将根据方法名自动拼接查询语句
//JPA根据方法名拼接出如下数据库
//select * from Stock where name = #name
public List<Stock> findByName(String name);
//JPA中@Query注解,实现基于JPQL的方式查询数据。
//JPQL语句等价于SQL (select s from Stock s where s.description like 'desc参数')
@Query("select s from Stock s where s.description like ?1%")
public List<Stock> getStockByDesc(String desc);
//使用nativeQuery参数运行原生数据库
@Query(value="select s from Stock s where s.description like ?1%",nativeQuery = true)
public List<Stock> getStockByNativeQuery(String nativeQuery);
}
2.通过@Query查询数据,实现基于JPQL的方式查询数据。
Repo类
package com.test.repo;
import com.test.model.Stock;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @Author: 沐槿
* @Description: TODO
* @Date: 2025/12/15 14:38
* @Version: 1.0
*/
// 用@Component注解放入Spring容器中
@Component
public interface StockRepo extends JpaRepository<Stock, Integer> {
//JPA将根据方法名自动拼接查询语句
//JPA根据方法名拼接出如下数据库
//select * from Stock where name = #name
public List<Stock> findByName(String name);
//JPA中@Query注解,实现基于JPQL的方式查询数据。
//JPQL语句等价于SQL (select s from Stock s where s.description like 'desc参数')
@Query("select s from Stock s where s.description like ?1%")
public List<Stock> getStockByDesc(String desc);
//使用nativeQuery参数运行原生数据库
@Query(value="select s from Stock s where s.description like ?1%",nativeQuery = true)
public List<Stock> getStockByNativeQuery(String nativeQuery);
}
2.通过@Query查询数据,实现基于JPQL的方式查询数据。
Repo类
package com.test.repo;
import com.test.model.Stock;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @Author: 沐槿
* @Description: TODO
* @Date: 2025/12/15 14:38
* @Version: 1.0
*/
// 用@Component注解放入Spring容器中
@Component
public interface StockRepo extends JpaRepository<Stock, Integer> {
//JPA将根据方法名自动拼接查询语句
//JPA根据方法名拼接出如下数据库
//select * from Stock where name = #name
public List<Stock> findByName(String name);
//JPA中@Query注解,实现基于JPQL的方式查询数据。
//JPQL语句等价于SQL (select s from Stock s where s.description like 'desc参数')
@Query("select s from Stock s where s.description like ?1%")
public List<Stock> getStockByDesc(String desc);
//使用nativeQuery参数运行原生数据库
@Query(value="select s from Stock s where s.description like ?1%",nativeQuery = true)
public List<Stock> getStockByNativeQuery(String nativeQuery);
}
业务实现类
package com.test.service;
import com.test.model.Stock;
import com.test.repo.StockRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @Author: 沐槿
* @Description: TODO
* @Date: 2025/12/15 14:38
* @Version: 1.0
*/
@Service
public class StockService {
@Autowired
StockRepo stockRepo;
public List<Stock> getStockByNativeQuery(String desc) {
return stockRepo.getStockByNativeQuery(desc);
}
public List<Stock> getStockByDesc(String desc) {
return stockRepo.getStockByDesc(desc);
}
}
控制器类
package com.test.controller;
import com.test.model.Stock;
import com.test.service.StockPageAndSortingService;
import com.test.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Query;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @Author: 沐槿
* @Description: TODO
* @Date: 2025/12/15 14:37
* @Version: 1.0
*/
@RestController
public class Controller {
@Autowired
StockService stockService;
@RequestMapping("/getStockByDesc/{desc}")
List<Stock> getStockByDesc(@PathVariable String desc){
return stockService.getStockByDesc(desc);
}
@RequestMapping("/getStockByNativeQuery/{desc}")
List<Stock> getStockByNativeQuery(@PathVariable String desc){
return stockService.getStockByNativeQuery(desc);
}
重启SpringBoot项目,在浏览器中输入地址
http://localhost:8080/getStockByDesc/Good
能看到
[{"id":1,"name":"computer","num":10,"description":"Good"},{"id":2,"name":"Mac","num":8,"description":"Good"}]
在浏览器中输入地址
http://localhost:8080/getStockByNativeQuery/Good
能看到
[{"id":1,"name":"computer","num":10,"description":"Good"},{"id":2,"name":"Mac","num":8,"description":"Good"}]
2410

被折叠的 条评论
为什么被折叠?



