如何通过 Id 从数据库中获取数据 Spring Boot Java REST API
How to get Data By Id From Database Spring Boot Java REST API
我构建了简单的 REST 服务,我想从基于数据库的 ID 获取数据密钥,但是,当我 运行 邮递员中没有显示结果时,我该如何解决?
这是我的控制器
//Get Key
@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
String getKey(@PathVariable int company_id) {
String encKey = null;
gkrepo.getKeyByCompanyid(company_id);
return encKey;
}
这是我的存储库
public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
{
@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();
public void getKeyByCompanyid(Integer companyid);
}
您应该按照以下步骤更改存储库中的方法。试试这个。
public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
{
@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();
public KeyEntity findByCompanyId(Integer companyid);
}
你的控制器应该是:
@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
String getKey(@PathVariable int company_id) {
String encKey = null;
KeyEntity keyEntity = gkrepo.getKeyByCompanyid(company_id);
return keyEntity.getKey;
}
你的存储库应该是这样的:
public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
{
@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();
public KeyEntity findByCompanyId(Integer companyid);
}
您可以尝试按照下面的方式更改方法
public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
{
@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();
public KeyEntity findByCompanyId(Integer companyid);
}
如果您使用此代码,则必须按以下方式更改代码
gkrepo.findByCompanyId
instead of
gkrepo.getKeyByCompanyid(company_id);
或
public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
{
@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();
@Query(Select k from KeyEntity k where companyid = :companyid)
public KeyEntity getKeyByCompanyid(@Param("companyid") Integer companyid);
}
您正在使用 Spring 数据 JPA。您的存储库接口从扩展的 JpaRepository
接口继承了各种方法。这就是它的全部意义。
再无读写查询方法:
@RestController
public class myController{
@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
public KeyEntity getKey(@PathVariable("company_id") int companyId) {
return gkrepo.findById(companyId); //inherited method
}
}
此外,如果启用 Spring Data JPA 的 Web 扩展,则根本不需要调用存储库,因为实体将从路径变量中自动解析:
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.web
The DomainClassConverter lets you use domain types in your Spring MVC
controller method signatures directly, so that you need not manually
lookup the instances through the repository
@RestController
public class myController{
@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
KeyEntity getKey(@PathVariable KeyEntity keyEntity) {
return keyEntity;
}
}
这里的问题是,您忽略了 repository
方法的 return 值和 return null
.
@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
String getKey(@PathVariable int company_id) {
String encKey = null;
gkrepo.findOneByCompanyId(company_id);
return encKey; //YOU RETURN NULL HERE
}
您需要做的是 return 来自 KeyEntity
对象的密钥。
@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
String getKey(@PathVariable int company_id) {
return gkrepo.getKeyByCompanyid(company_id).getKey();
}
您还需要在 repository
中添加一个方法。
public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer> {
@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();
public void findOneByCompanyId(Integer companyid);
}
我构建了简单的 REST 服务,我想从基于数据库的 ID 获取数据密钥,但是,当我 运行 邮递员中没有显示结果时,我该如何解决?
这是我的控制器
//Get Key
@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
String getKey(@PathVariable int company_id) {
String encKey = null;
gkrepo.getKeyByCompanyid(company_id);
return encKey;
}
这是我的存储库
public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
{
@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();
public void getKeyByCompanyid(Integer companyid);
}
您应该按照以下步骤更改存储库中的方法。试试这个。
public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
{
@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();
public KeyEntity findByCompanyId(Integer companyid);
}
你的控制器应该是:
@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
String getKey(@PathVariable int company_id) {
String encKey = null;
KeyEntity keyEntity = gkrepo.getKeyByCompanyid(company_id);
return keyEntity.getKey;
}
你的存储库应该是这样的:
public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
{
@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();
public KeyEntity findByCompanyId(Integer companyid);
}
您可以尝试按照下面的方式更改方法
public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
{
@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();
public KeyEntity findByCompanyId(Integer companyid);
}
如果您使用此代码,则必须按以下方式更改代码
gkrepo.findByCompanyId
instead of
gkrepo.getKeyByCompanyid(company_id);
或
public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer>
{
@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();
@Query(Select k from KeyEntity k where companyid = :companyid)
public KeyEntity getKeyByCompanyid(@Param("companyid") Integer companyid);
}
您正在使用 Spring 数据 JPA。您的存储库接口从扩展的 JpaRepository
接口继承了各种方法。这就是它的全部意义。
再无读写查询方法:
@RestController
public class myController{
@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
public KeyEntity getKey(@PathVariable("company_id") int companyId) {
return gkrepo.findById(companyId); //inherited method
}
}
此外,如果启用 Spring Data JPA 的 Web 扩展,则根本不需要调用存储库,因为实体将从路径变量中自动解析:
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#core.web
The DomainClassConverter lets you use domain types in your Spring MVC controller method signatures directly, so that you need not manually lookup the instances through the repository
@RestController
public class myController{
@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
KeyEntity getKey(@PathVariable KeyEntity keyEntity) {
return keyEntity;
}
}
这里的问题是,您忽略了 repository
方法的 return 值和 return null
.
@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
String getKey(@PathVariable int company_id) {
String encKey = null;
gkrepo.findOneByCompanyId(company_id);
return encKey; //YOU RETURN NULL HERE
}
您需要做的是 return 来自 KeyEntity
对象的密钥。
@RequestMapping(path="/getkey/{company_id}", method = RequestMethod.GET)
String getKey(@PathVariable int company_id) {
return gkrepo.getKeyByCompanyid(company_id).getKey();
}
您还需要在 repository
中添加一个方法。
public interface GenerateKeyRepository extends JpaRepository<KeyEntity, Integer> {
@Query(value= "SELECT * FROM tb_key", nativeQuery = true)
List<KeyEntity> getAll();
public void findOneByCompanyId(Integer companyid);
}