【发布时间】: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
truelike@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