第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 Spring Boot 應(yīng)用程序中手動(dòng)注冊(cè)非標(biāo)準(zhǔn)化 SQL 函數(shù)?

如何在 Spring Boot 應(yīng)用程序中手動(dòng)注冊(cè)非標(biāo)準(zhǔn)化 SQL 函數(shù)?

慕容森 2023-05-17 16:50:52
我在當(dāng)前的 spring-boot 項(xiàng)目中使用 JPA 查詢。如何添加GROUP_CONCAT等非標(biāo)準(zhǔn)化 SQL 函數(shù)?我發(fā)現(xiàn) GROUP_CONCAT 不是 JPA 查詢中的注冊(cè)函數(shù),但可以通過(guò)手動(dòng)注冊(cè)來(lái)訪問(wèn)。我已經(jīng)嘗試過(guò)以下鏈接但對(duì)我不起作用:如何在 Spring Boot 應(yīng)用程序中添加非標(biāo)準(zhǔn)化的 sql 函數(shù)?使用 JPA 和 Hibernate 注冊(cè) SQL 函數(shù)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 回答

?
繁華開(kāi)滿天機(jī)

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以在我的高性能 Java 持久性 GitHub 存儲(chǔ)庫(kù)中找到一個(gè)功能齊全的示例。

在您的情況下,您不需要自定義JpaPlatform.?那應(yīng)該設(shè)置為HibernateJpaPlatform.

您可以MetadataBuilderContributer通過(guò)配置文件以編程方式注冊(cè)application.properties

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



查看完整回答
反對(duì) 回復(fù) 2023-05-17
?
www說(shuō)

TA貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超8個(gè)贊

創(chuàng)建一個(gè)類,在重寫的方法中添加需要用到的 mySql 函數(shù):


public class SqlFunctionsMetadataBuilderContributor implements MetadataBuilderContributor{


 @Override

    public void contribute(MetadataBuilder metadataBuilder) {

        metadataBuilder.applySqlFunction(

            "group_concat",

            new StandardSQLFunction(

                "group_concat",

                StandardBasicTypes.STRING

            )

        );

    }

}

之后,通過(guò) application.properties 提供您的 metadata_builder_contributor:


spring.jpa.properties.hibernate.metadata_builder_contributor = qualifiedClassName


查看完整回答
反對(duì) 回復(fù) 2023-05-17
?
SMILET

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

如果有人在 SpringBoot 應(yīng)用程序中注冊(cè)時(shí)遇到問(wèn)題,這是正確的方法:


創(chuàng)建一個(gè)實(shí)現(xiàn)的類: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

        )

    );

  }

}

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


  spring:      

    jpa:

      properties:

        hibernate:

          metadata_builder_contributor: com.application.config.SqlFunctionsMetadataBuilderContributor



查看完整回答
反對(duì) 回復(fù) 2023-05-17
  • 3 回答
  • 0 關(guān)注
  • 214 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)