1. <small id='CDWtJ'></small><noframes id='CDWtJ'>

        <bdo id='CDWtJ'></bdo><ul id='CDWtJ'></ul>
      <i id='CDWtJ'><tr id='CDWtJ'><dt id='CDWtJ'><q id='CDWtJ'><span id='CDWtJ'><b id='CDWtJ'><form id='CDWtJ'><ins id='CDWtJ'></ins><ul id='CDWtJ'></ul><sub id='CDWtJ'></sub></form><legend id='CDWtJ'></legend><bdo id='CDWtJ'><pre id='CDWtJ'><center id='CDWtJ'></center></pre></bdo></b><th id='CDWtJ'></th></span></q></dt></tr></i><div id='CDWtJ'><tfoot id='CDWtJ'></tfoot><dl id='CDWtJ'><fieldset id='CDWtJ'></fieldset></dl></div>
    2. <legend id='CDWtJ'><style id='CDWtJ'><dir id='CDWtJ'><q id='CDWtJ'></q></dir></style></legend>

    3. <tfoot id='CDWtJ'></tfoot>

      使用@Query 从 SPRING BOOT 中的文件中获取查询

      时间:2023-09-26

      <small id='pJhNJ'></small><noframes id='pJhNJ'>

      <legend id='pJhNJ'><style id='pJhNJ'><dir id='pJhNJ'><q id='pJhNJ'></q></dir></style></legend>
          <bdo id='pJhNJ'></bdo><ul id='pJhNJ'></ul>
            <tbody id='pJhNJ'></tbody>

              <tfoot id='pJhNJ'></tfoot>

                <i id='pJhNJ'><tr id='pJhNJ'><dt id='pJhNJ'><q id='pJhNJ'><span id='pJhNJ'><b id='pJhNJ'><form id='pJhNJ'><ins id='pJhNJ'></ins><ul id='pJhNJ'></ul><sub id='pJhNJ'></sub></form><legend id='pJhNJ'></legend><bdo id='pJhNJ'><pre id='pJhNJ'><center id='pJhNJ'></center></pre></bdo></b><th id='pJhNJ'></th></span></q></dt></tr></i><div id='pJhNJ'><tfoot id='pJhNJ'></tfoot><dl id='pJhNJ'><fieldset id='pJhNJ'></fieldset></dl></div>

              1. 本文介绍了使用@Query 从 SPRING BOOT 中的文件中获取查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我有一个用于加载查询的项目:

                I have a proyect that use to load queries this:

                @Query(value = SELECT_BY_USER_ID, nativeQuery = true)
                Employee findByUserId(@Param("userId") String userId);
                

                在SELECT_BY_USER_ID"上是一个普通的字符串查询.

                On "SELECT_BY_USER_ID" is a normal String query.

                我在 jar 外有一个 YML 配置,我用它来加载不同的配置,我也想使用这个 YML 来加载查询.

                I have a YML configuration outside jar, that I use to load differents configurations, and I want to use this YML, to load queries too.

                示例 YML:

                file:
                    query1: SELECT * FROM DUAL;
                

                但我不知道如何在@Query 值中直接从我的文件中加载,我试过这样:

                But I don't know how to load directly from my file in @Query value, I tried like that:

                @Query(value = ("${file.query1}"), nativeQuery = true)
                List<Employee> findByCost();
                

                我该怎么办?谢谢.

                推荐答案

                如果需要从resources文件夹加载SQL,可以试试spring-data-sqlfile 库.它支持从资源加载 SQL 查询.因此,您只需要将 SQL 查询放到资源文件夹中,然后就可以在 SqlFromResource 注释中引用它们:

                If you need to load SQL from resources folder, you can try spring-data-sqlfile library. It supports loading SQL queries from resources. So you just need to put your SQL queries to the resources folder and than you can reference them in SqlFromResource annotation:

                @Repository
                public interface UserRepository extends JpaRepository<User, Integer> {
                    @SqlFromResource(path = "select_user_by_id.sql")
                    User findById(int userId);
                }
                

                输出会是这样的:

                @Repository
                public interface UserRepositoryGenerated extends JpaRepository<User, Integer> {    
                  @Query(
                      value = "SELECT *     FROM users     WHERE id = :userId",
                      nativeQuery = true
                  )
                  User findById(int userId);
                }
                

                这篇关于使用@Query 从 SPRING BOOT 中的文件中获取查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:从 Java 中的 yaml 读取地图变为空 下一篇:application.yml 是否支持环境变量?

                相关文章

                最新文章

                  <legend id='VVecR'><style id='VVecR'><dir id='VVecR'><q id='VVecR'></q></dir></style></legend>

                  1. <small id='VVecR'></small><noframes id='VVecR'>

                  2. <tfoot id='VVecR'></tfoot>
                    <i id='VVecR'><tr id='VVecR'><dt id='VVecR'><q id='VVecR'><span id='VVecR'><b id='VVecR'><form id='VVecR'><ins id='VVecR'></ins><ul id='VVecR'></ul><sub id='VVecR'></sub></form><legend id='VVecR'></legend><bdo id='VVecR'><pre id='VVecR'><center id='VVecR'></center></pre></bdo></b><th id='VVecR'></th></span></q></dt></tr></i><div id='VVecR'><tfoot id='VVecR'></tfoot><dl id='VVecR'><fieldset id='VVecR'></fieldset></dl></div>
                    • <bdo id='VVecR'></bdo><ul id='VVecR'></ul>