【发布时间】:2022-01-07 19:20:34
【问题描述】:
在存储库界面中创建一个本机 sql 查询,如下所示:
@Query(value = "select Student.id as id , Student.name as studentname, Teacher.name as teachername from :firstdatabase inner join :seconddatabase on Student.id = Teacher.id;", nativeQuery = true)
List<StudentTeacher> getListStudentTeacher( @Param("firstdatabase") String firstdatabase, @Param("seconddatabase") String seconddatabase);
数据库和表名作为控制器类的参数,如下所示:
List<StudentTeacher> studentTeacherList = studentTeacherRepo.getListStudentTeacher("database1.Student","database2.Teacher");
得到以下错误:
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 ''database1.Student' inner join 'database2.Teacher' on Student.id = Teacher.id' at line 1.
我的问题是如何跨数据库进行连接,其中数据库名称使用本机查询进行参数化,以及为什么在参数之前有引号。在 where 条件下使用参数时,我们不会得到引号。请对此提供帮助
【问题讨论】:
-
你不应该这样使用参数化。这也是出于安全原因,其他人可能会使用 SQL 注入来删除数据库等。所以 JDBC 对传递给查询的参数进行转义/编码。
标签: spring spring-boot hibernate spring-data-jpa