我目前正在构建Spring MVC webapp,数据库是重要的后端部分.但无论出于何种原因,Spring拒绝将数据处理为UTF-8.由于视图,资源和浏览器都设置为Unicode,因此数据库中的表也是如此(并且还要阅读相当多的类似问题),我已经确定问题在于数据库连接.
JDBC驱动程序应该在connectionProperties中提供两个项目:useUnicode(设置为yes和characterEncoding(设置为utf8).但事实证明,这是不可能的.
JDBC是一个bean,因此通过XML文件配置,如下所示:
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/<database>" />
<property name="username" value="<not telling>" />
<property name="password" value="<not telling>" />
Run Code Online (Sandbox Code Playgroud)
此设置将从数据库中提取的所有非字母数字字符(例如箭头或希腊字母)转换为问号.显然,这是不可接受的.
我尝试了多种解决方案:指定JDBC URL jdbc:mysql://localhost:3306/<database>?useUnicode=yes&characterEncoding=utf8,使用my.ini文件(和MySQL Workbench)来强制数据库中的所有内容都默认为utf8charset,这引起了最大的麻烦:添加<property name="connectionProperties" value="useUnicode=yes;characterEncoding=utf8" />.事实证明,在单个bean中设置两个connectionProperties实际上是不可能的,因为......在任何地方都没有提到的分隔字符(bean会尝试将其读作为尝试设置yes;characterEncoding=utf8为值useUnicode).所以我的问题是:我如何utf8?
我有一个简单的Web应用程序,我使用JPA.
我有一个名为BlogEntry的实体.
当我提交一个新的BlogEntry时,当我调试我的应用程序时,我看到utf8字符就好了.
例如
em.persist(entity);
Run Code Online (Sandbox Code Playgroud)
在这一行中,如果我调试例如:
entity.getTitle()
Run Code Online (Sandbox Code Playgroud)
我可以成功地在IDE中看到utf-8字符.(如ğğ,或çç)
此外,我的数据库具有UTF8排序规则,我可以使用类似"INSERT INTO ..."的sql插入utf-8字符.
但是,使用JPA时,字符会保持为????
为什么会这样?
问候.
这是我的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="Persistence">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.tugay.blog.core.model.Blogentry</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/blogdatabase"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="aabbccdd"/>
</properties>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud) 我有一个名为"test"的表,其中包含Postgres 9.3中json类型的列"sample_column".我正在尝试使用Spring/JPA将以下内容写入列:{"name":"Updated name"}
我在其他帖子上读到我需要添加一个自定义转换器来将字符串映射到json类型.这是我现在的代码:
TestDAO.java:
@Entity
@Table(name="test")
public class TestDAO implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id", unique=true, nullable=false)
private Long id;
@Column(name="sample_column")
@Convert(converter = MyCustomConverter.class)
private MyCustomClass sampleColumn;
// Getter / Setters
}
Run Code Online (Sandbox Code Playgroud)
用于映射json内容的CustomClass:
public class MyCustomClass {
@JsonProperty("name")
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
最后,转换器类:
@javax.persistence.Converter
public class MyCustomConverter implements AttributeConverter<MyCustomClass, String> {
private final static ObjectMapper objectMapper …Run Code Online (Sandbox Code Playgroud) 我需要设置一些特定的Oracle JDBC连接属性,以加快批处理INSERTs(defaultBatchValue)和质量SELECTs(defaultRowPrefetch).我得到了如何用DBCP实现这一目标的建议(感谢M. Deinum),但我想:
我正在考虑spring.datasource.custom_connection_properties将来支持或类似的功能请求,并且由于这种尝试,这已经成为可能.我这样做是通过在创建DataSource时传递相关信息并操纵DataSource的创建,如下所示:
@Bean
public DataSource dataSource() {
DataSource ds = null;
try {
Field props = DataSourceBuilder.class.getDeclaredField("properties");
props.setAccessible(true);
DataSourceBuilder builder = DataSourceBuilder.create();
Map<String, String> properties = (Map<String, String>) props.get(builder);
properties.put("defaultRowPrefetch", "1000");
properties.put("defaultBatchValue", "1000");
ds = builder.url( "jdbc:oracle:thin:@xyz:1521:abc" ).username( "ihave" ).password( "wonttell" ).build();
properties = (Map<String, String>) props.get(builder);
log.debug("properties after: {}", properties);
} ... leaving out the catches ...
}
log.debug("We are using this …Run Code Online (Sandbox Code Playgroud) connection-pooling jdbctemplate ojdbc spring-boot tomcat-jdbc
jpa ×2
spring ×2
java ×1
jdbc ×1
jdbctemplate ×1
json ×1
ojdbc ×1
spring-boot ×1
spring-mvc ×1
tomcat-jdbc ×1
utf-8 ×1