亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Spring Boot 應用程序中手動注冊非標準化 SQL 函數?

如何在 Spring Boot 應用程序中手動注冊非標準化 SQL 函數?

慕容森 2023-05-17 16:50:52
我在當前的 spring-boot 項目中使用 JPA 查詢。如何添加GROUP_CONCAT等非標準化 SQL 函數?我發現 GROUP_CONCAT 不是 JPA 查詢中的注冊函數,但可以通過手動注冊來訪問。我已經嘗試過以下鏈接但對我不起作用:如何在 Spring Boot 應用程序中添加非標準化的 sql 函數?使用 JPA 和 Hibernate 注冊 SQL 函數https://thoughts-on-java.org/database-functions/https://vladmihalcea.com/hibernate-sql-function-jpql-criteria-api-query/1.public class SqlFunctionsMetadataBuilderContributor? ? ? ? implements MetadataBuilderContributor {? ? @Override? ? public void contribute(MetadataBuilder metadataBuilder) {? ? ? ? metadataBuilder.applySqlFunction(? ? ? ? ? ? ? ? "group_concat",? ? ? ? ? ? ? ? new StandardSQLFunction(? ? ? ? ? ? ? ? ? ? ? ? "group_concat",? ? ? ? ? ? ? ? ? ? ? ? StandardBasicTypes.STRING? ? ? ? ? ? ? ? )? ? ? ? );? ? }}2.?public String render(Type firstArgumentType, List arguments, SessionFactoryImplementor factory)? ? ? ? ? ? throws QueryException {? ? ? ? if (arguments.size() < 1) {? ? ? ? ? ? throw new QueryException(new IllegalArgumentException("group_concat should have at least one arg"));? ? ? ? }? ? ? ? StringBuilder builder = new StringBuilder();? ? ? ? if (arguments.size() > 1 && arguments.get(0).equals("'distinct'")) {? ? ? ? ? ? builder.append("distinct ");? ? ? ? ? ? builder.append(arguments.get(1));? ? ? ? } else {? ? ? ? ? ? builder.append(arguments.get(0));? ? ? ? }? ? ? ? return "group_concat(" + builder.toString() + ")";? ? }3.@Configurationpublic class DataSource {? ? @Bean? ? public JpaVendorAdapter jpaVendorAdapter() {? ? ? ? AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();? ? ? ? adapter.setShowSql(true);? ? ? ? adapter.setDatabase(Database.MYSQL);? ? ? ? // package to CustomMysqlDialect? ? ? ? adapter.setDatabasePlatform("com.myprojet.admin.configuration.RegisterSqlFunction");? ? ? ? adapter.setGenerateDdl(false);? ? ? ? return adapter;? ? }}我除了將 group_concat 與 JPA 查詢一起使用。
查看完整描述

3 回答

?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

您可以在我的高性能 Java 持久性 GitHub 存儲庫中找到一個功能齊全的示例。

在您的情況下,您不需要自定義JpaPlatform.?那應該設置為HibernateJpaPlatform.

您可以MetadataBuilderContributer通過配置文件以編程方式注冊application.properties

hibernate.metadata_builder_contributor=com.vladmihalcea.book.hpjp.SqlFunctionsMetadataBuilderContributor



查看完整回答
反對 回復 2023-05-17
?
www說

TA貢獻1775條經驗 獲得超8個贊

創建一個類,在重寫的方法中添加需要用到的 mySql 函數:


public class SqlFunctionsMetadataBuilderContributor implements MetadataBuilderContributor{


 @Override

    public void contribute(MetadataBuilder metadataBuilder) {

        metadataBuilder.applySqlFunction(

            "group_concat",

            new StandardSQLFunction(

                "group_concat",

                StandardBasicTypes.STRING

            )

        );

    }

}

之后,通過 application.properties 提供您的 metadata_builder_contributor:


spring.jpa.properties.hibernate.metadata_builder_contributor = qualifiedClassName


查看完整回答
反對 回復 2023-05-17
?
SMILET

TA貢獻1796條經驗 獲得超4個贊

如果有人在 SpringBoot 應用程序中注冊時遇到問題,這是正確的方法:


創建一個實現的類:MetadataBuilderContributor 接口。


package com.application.config;


public class SqlFunctionsMetadataBuilderContributor implements MetadataBuilderContributor {


  @Override

  public void contribute(MetadataBuilder metadataBuilder) {

    metadataBuilder.applySqlFunction(

        "STRING_AGG",

        new StandardSQLFunction(

            "STRING_AGG",

            StandardBasicTypes.STRING

        )

    );

  }

}

在您的應用程序 .yml(或 .properties)中,在以下屬性路徑中引用先前創建的類:spring.jpa.properties.hibernate.metadata_builder_contributor


  spring:      

    jpa:

      properties:

        hibernate:

          metadata_builder_contributor: com.application.config.SqlFunctionsMetadataBuilderContributor



查看完整回答
反對 回復 2023-05-17
  • 3 回答
  • 0 關注
  • 223 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號