【问题标题】:Set jpa utf-8 encoding in application.properties在 application.properties 中设置 jpa utf-8 编码
【发布时间】:2017-03-08 03:14:26
【问题描述】:

这是以下问题的后续问题:

JPA utf-8 characters not persisted

答案 (https://stackoverflow.com/a/32574280) 对我有用。但是,我想将这些配置保存在我的 application.properties 文件中,与 url 配置分开。这些配置的名称是什么?

我试过了:

connection.useUnicode=true connection.characterEncoding=utf8

然后我在我的配置类的属性中设置它们:

properties.put("connection.useUnicode", env.getRequiredProperty("connection.useUnicode")); properties.put("connection.characterEncoding", env.getRequiredProperty("connection.characterEncoding"));

但它不起作用。

【问题讨论】:

标签: spring hibernate jpa utf-8 spring-boot


【解决方案1】:

我偶然发现了同样的问题。即,我需要在使用 Tomcat 数据源和 Spring Boot 时通过设置 Oracle 特定属性来启用连接加密。

事实证明,使用 Spring Boot 执行此操作的唯一方法是在应用程序配置类中创建自定义 DataSource bean。

没有办法将自定义连接属性传递给池,Spring Boot 不允许这样做。我检查了 Spring Boot 1.4.1 和 Spring Boot 的当前主版本。

如果有兴趣,请查看DataSourceConfiguration.Tomcat 类。它传递给池的唯一属性是DataSourceProperties 类中定义的属性。后者没有任何允许传递自定义属性的字段,它仅支持所有数据源通用的预定义属性集。

【讨论】:

  • 嗨@AndreyK,您知道在SpringBoot 中配置oracle.net.CONNECT_TIMEOUT 吗?如果你想作为一个问题,有人已经问过here。谢谢。
【解决方案2】:

如果您使用的是 Spring Boot,则在应用程序属性中

spring.datasource.connectionProperties=useUnicode=true;characterEncoding=utf-8;

【讨论】:

  • Spring Boot(从 1.4 开始)没有 spring.datasource.connectionProperties。这是特定于您正在使用的连接池的属性,因此它在很大程度上取决于您使用的连接池。如果您使用的是 Tomcat 连接池,则在 1.4 中必须是 spring.datasource.tomcat.connection-properties
  • 嗨@StephaneNic​​oll,你知道在SpringBoot中配置oracle.net.CONNECT_TIMEOUT吗?如果你想作为一个问题,有人已经问过here。谢谢。
猜你喜欢
  • 2016-09-23
  • 1970-01-01
  • 2012-05-26
  • 2018-07-29
  • 1970-01-01
  • 1970-01-01
  • 2017-01-02
  • 1970-01-01
  • 2012-03-10
相关资源
最近更新 更多