【问题标题】:Error while creating custom queries in spring boot在 Spring Boot 中创建自定义查询时出错
【发布时间】:2018-05-28 05:21:47
【问题描述】:

我是 Spring Boot 新手,在向我的代码添加查询时遇到以下错误,

org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“testController”的 bean 时出错:不满足的依赖关系 通过字段“testService”表示;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为“testService”的bean:调用init方法 失败的;嵌套异常是 java.lang.IllegalArgumentException: 查询方法公共抽象 rest.Test 的验证失败 rest.services.TestService.findByXY(java.lang.String)!

以下是我的代码文件,

Test.java

@Entity
public class Test {
@Id
private int id;
@Column
private String x;
@Column
private String y;

public Test() {

}

public Test(int id, String x, String y) {
    this.id = id;
    this.x = x;
    this.y = y;
}
}

TestService.java

public interface TestService extends CrudRepository<Test, Integer> {
@Query("select id, x, y from test where x = :x")
Employee findByXY(@Param("x") String x);
}

TestController.java

@Controller
public class TestController {

@Autowired
private TestService testService;

@GetMapping("/get-x")
public Employee findX() {
    //System.out.println(testService.findByXY("123"));
    return testService.findByXY("123");
}
}

PS:我正在关注这个教程页面 - link to tutorial

提前致谢!!

【问题讨论】:

  • @Service注释你的TestService.java接口,让我知道状态。
  • @AtaurRahmanMunna 我得到了同样的错误..
  • 尝试通过nativeQuery true like @Query("select id, x, y from test where x = :x", nativeQuery = true)
  • @hrdkisback 成功了!!但是为什么前一个失败了?
  • @Arigato Manga 没有nativeQuery 标志,你必须编写非原生的 HQL 查询,所以如果你想编写原生查询,你必须设置 nativeQuery 标志 true

标签: java mysql spring-mvc spring-boot spring-data


【解决方案1】:

你已经写了native查询所以,试着像传递nativeQuery true

@Query("select id, x, y from test where x = :x", nativeQuery = true)

或者你可以写HQL查询

@Query("SELECT t.id, t.x, t.y FROM Test t where t.x = :x")

请参阅此链接了解更多信息。 https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query

【讨论】:

  • 请注意,@Query("select id, x, y from Test where x = :x") 不是有效的 HQL 或 JPQL 语法。
【解决方案2】:

错误很明显:

方法 public abstract rest.Test 的查询验证失败 rest.services.TestService.findByXY(java.lang.String)!

JPQL 查询的语法不正确:

 @Query("select id, x, y from test where x = :x")
 Employee findByXY(@Param("x") String x);

选择Test 并返回与您的查询匹配的类型:

 @Query("select t from Test t where t.x = :x")
 Test findByXY(@Param("x") String x);

否则,如果您想按照 hrdkisback 的建议执行此操作,请通过添加 nativeQuery = true 来指定本机查询。

【讨论】:

  • 我发布的代码是我实际代码的蓝图,所以实际代码是针对员工的,我将其发布为测试..在问题中我将员工更改为测试但我忘记更改返回键入该函数..
  • @Arigato Manga 好的。无论如何,语法不正确。您必须选择实体,而不是它的字段。
  • @YCF_L 你是救世主!!我出于同样的目的修改了我的代码。您的回答节省了很多时间..真的很感谢..(您获取数据的答案..想知道为什么删除它)如果可能的话,您能告诉我还有多少其他方法可以做同样的事情..(链接只有在可能的情况下,才能获得教程或指向 SO 问题的链接也会有所帮助)......但真的很感谢!
【解决方案3】:

这个查询:

select id, x, y from test where x = :x

返回 3 个参数 idxy,而不是 Employee 类型的对象

所以返回类型应该是List&lt;Object[]&gt; 而不是Employee

@Query("select id, x, y from test where x = :x")
List<Object[]> findByXY(@Param("x") String x);

然后你可以像这样遍历这个列表:

List<Object[]> listTest = findByXY(x);
List<Test> resultList = new ArrayList<>();

for (Object[] test : listTest) {
    resultList.add(
            new Test((Integer) test[0],
                    (String) test[1],
                    (String) test[2])
    );
}

【讨论】:

    猜你喜欢
    • 2016-12-11
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 2016-12-25
    相关资源
    最近更新 更多