Springboot 无法提取 ResultSet

Sin*_*ssc 5 java jpa spring-boot

目前我在为我的 springboot 项目获取 mysql 数据时遇到问题:

出现意外错误(类型=内部服务器错误,状态=500)。无法提取结果集;SQL [不适用];嵌套异常是 org.hibernate.exception.SQLGrammarException: 无法提取 ResultSet

测试实体

@Entity
public class TestEntity implements Serializable {

    @Id
    private int id;
    private String p1;
    private String p2;
    private String p3;

    public TestEntity() {
    }

    public TestEntity(int id, String p1, String p2, String p3){
        this.id = id;
        this.p1 = p1;
        this.p2 = p2;
        this.p3 = p3;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getP1() {
        return p1;
    }

    public void setP1(String p1) {
        this.p1 = p1;
    }

    public String getP2() {
        return p2;
    }

    public void setP2(String p2) {
        this.p2 = p2;
    }

    public String getP3() {
        return p3;
    }

    public void setP3(String p3) {
        this.p3 = p3;
    }



}
Run Code Online (Sandbox Code Playgroud)

测试服务.java

@Service
public class TestService {

    @Autowired
    private TestRepository testRepository;

    public ArrayList<TestEntity> getAllTestEntities(){
       ArrayList<TestEntity> list = new ArrayList();
       testRepository.findAll().forEach(list::add);
       return list;
    }

    public Optional getTestEntity(int id){
       return testRepository.findById(id);
    }
    public void addTestEntity(TestEntity t){
        testRepository.save(t);
    }
    public void removeTestEntity(int index){

        testRepository.deleteById(index);

    }

}
Run Code Online (Sandbox Code Playgroud)

测试库.java

@Repository("mysql")
public interface TestRepository extends CrudRepository<TestEntity,Integer> {



}
Run Code Online (Sandbox Code Playgroud)

测试控制器.java

@RestController
public class TestController {

    @Autowired
    private TestService testService;

    @RequestMapping("/test/AllUnits")
    public ArrayList<TestEntity> getAllTestUnits(){
        return testService.getAllTestEntities();
    }

    @RequestMapping("/test/{id}")
    public Optional getAllTestUnit(@PathVariable int id){
        return testService.getTestEntity(id);
    }

    @RequestMapping(method=RequestMethod.POST,value = "/test" )
    public void addTestUnit(@RequestBody TestEntity t){
        testService.addTestEntity(t);
    }

    @RequestMapping(method=RequestMethod.DELETE,value = "/test/{id}" )
    public void deleteTestUnit(@RequestBody Integer id){
        testService.removeTestEntity(id);
    }

    @RequestMapping("/test/welcome")
    public String welcome(){

        return "welcome to springboot";

    }


}
Run Code Online (Sandbox Code Playgroud)

编辑application.properties

cloud.aws.region.auto=true
cloud.aws.region.static=us-east-2

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://alyxdev.czcdgqfkwsnr.us-east-2.rds.amazonaws.com:3306/CryptoCurrency
spring.datasource.username=*******
spring.datasource.password=*******
Run Code Online (Sandbox Code Playgroud)

我能够使 /test/welcome 映射正常工作,因此我相信我对服务和控制器的实现是正确的。所以我想知道我是否在访问存储库中的数据库时犯了错误,还是应该使用 JpaRepository 而不是 CrudRepository 并使用显式查询?

编辑堆栈跟踪:org.springframework.dao.InvalidDataAccessResourceUsageException:无法提取结果集;SQL [不适用];嵌套异常是 org.hibernate.exception.SQLGrammarException:无法提取 ResultSet 引起:org.hibernate.exception.SQLGrammarException:无法提取 ResultSet 引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表 'CryptoCurrency.test_entity ' 不存在

小智 2

在您的实体类(即 TestEntity.java)中,您需要指定您引用的表

@Entity
@Table(name="tbl_something")
public class TestEntity implements Serializable {
Run Code Online (Sandbox Code Playgroud)

使用 CrudRepository 可以很好地处理超出数据库的情况。application.properties 文件对我来说看起来不错。