将 @Bind 用于 Dropwizard API 会导致 SQL 语法错误
Using @Bind for Dropwizard API results in SQL syntax error
我正在根据此处提供的 pdf 开发 Dropwizard RESTfull 应用程序 (http://it-ebooks.directory/book-1783289538.html)。我已经尝试研究这个问题,但没有发现任何人遇到同样的问题,请耐心等待,因为我不太熟悉使用 @Bind 或接口。
无论如何,我在我的计算机上设置了一个 MYsql 服务器,以便通过浏览器从我的 SQL 数据库的学生 table 中查询不同的值,我必须输入 localhost:8080/students/[A number]。为了实现这一点,文档说使用具有以下代码的数据库访问对象接口:
public interface StudentsDAO {
@Mapper(StudentMapper.class)
@SqlQuery("select * from students where studentId = :studentId")
Student getStudentById(@Bind("studentId") int studentId);
}
我的 studentResource class 使用以下代码:
@Path("/students")
@Produces(MediaType.APPLICATION_JSON)
public class StudentResource {
private final StudentsDAO studentsDao;
public StudentResource(DBI jdbi) {
studentsDao = jdbi.onDemand(StudentsDAO.class);
}
@GET
@Path("/{studentId}")
public Response getStudent(@PathParam("studentId") int studentId) {
// retrieve information about the contact with the provided id
Student student = studentsDao.getStudentById(studentId);
return Response.ok(student).build();
}
...
我已经在我的 MYsql 服务器中验证 运行 "Select * from students where studentId = 123" returns 是合适的学生,但是当我这样做时 localhost:8080/students/123 它给了我以下错误:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OPTION SQL_SELECT_LIMIT=DEFAULT' at line 1"
触发错误的行是:
Student student = studentsDao.getStudentById(studentId);
它在 studentResource class 中,所以我认为该错误与我的 DAO 接口中的某些内容有关...
感谢您的帮助!
问题似乎与您的 mysql 版本或 JDBC 连接器有关。尝试将您的 mysql 更新到最新版本。我正在使用 mysql 5.6,这段代码运行完美。
但是,要使其适用于您当前的 mysql 设置,请尝试添加 'LIMIT' 进行查询。例如:-
@SqlQuery("select * from students where studentId = :studentId LIMIT 1")
我正在根据此处提供的 pdf 开发 Dropwizard RESTfull 应用程序 (http://it-ebooks.directory/book-1783289538.html)。我已经尝试研究这个问题,但没有发现任何人遇到同样的问题,请耐心等待,因为我不太熟悉使用 @Bind 或接口。
无论如何,我在我的计算机上设置了一个 MYsql 服务器,以便通过浏览器从我的 SQL 数据库的学生 table 中查询不同的值,我必须输入 localhost:8080/students/[A number]。为了实现这一点,文档说使用具有以下代码的数据库访问对象接口:
public interface StudentsDAO {
@Mapper(StudentMapper.class)
@SqlQuery("select * from students where studentId = :studentId")
Student getStudentById(@Bind("studentId") int studentId);
}
我的 studentResource class 使用以下代码:
@Path("/students")
@Produces(MediaType.APPLICATION_JSON)
public class StudentResource {
private final StudentsDAO studentsDao;
public StudentResource(DBI jdbi) {
studentsDao = jdbi.onDemand(StudentsDAO.class);
}
@GET
@Path("/{studentId}")
public Response getStudent(@PathParam("studentId") int studentId) {
// retrieve information about the contact with the provided id
Student student = studentsDao.getStudentById(studentId);
return Response.ok(student).build();
}
...
我已经在我的 MYsql 服务器中验证 运行 "Select * from students where studentId = 123" returns 是合适的学生,但是当我这样做时 localhost:8080/students/123 它给了我以下错误:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'OPTION SQL_SELECT_LIMIT=DEFAULT' at line 1"
触发错误的行是:
Student student = studentsDao.getStudentById(studentId);
它在 studentResource class 中,所以我认为该错误与我的 DAO 接口中的某些内容有关...
感谢您的帮助!
问题似乎与您的 mysql 版本或 JDBC 连接器有关。尝试将您的 mysql 更新到最新版本。我正在使用 mysql 5.6,这段代码运行完美。 但是,要使其适用于您当前的 mysql 设置,请尝试添加 'LIMIT' 进行查询。例如:-
@SqlQuery("select * from students where studentId = :studentId LIMIT 1")