【问题标题】:Single Quote coming before parameter in Spring JPA native QuerySpring JPA本机查询中参数之前的单引号
【发布时间】: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


【解决方案1】:

我认为对于具有相同字段的表,最好的做法是创建一个视图将您的表联合起来,然后根据字段的参数而不是表的名称来请求视图

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2021-09-15
    • 1970-01-01
    • 2019-01-27
    相关资源
    最近更新 更多