开发环境:
- Spring 4.2 倍
- hibernate 4.3.11
- Tomcat 8 服务器
- MySQL 5.4
在更改为 Hibernate 之前,插入、检索和显示字符没有问题。然而,在我的 DAO 中进行更改以奇怪地使用 Hibernate 之后,我似乎无法将正确的字符插入到 MySQL 数据库中。
我已经确保 MySQL Schema 确实可以通过使用查询“INSERT INTO spring_normalize
.offers
(text
, users_username
) VALUES ('ölm', 'lalalal');"index.jsp 上的输出是正确的。
我修改了我的 hibernate 配置
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.connection.useUnicode">true</prop><!-- added -->
<prop key="hibernate.connection.characterEncoding">UTF-8</prop><!-- added -->
<prop key="hibernate.connection.charSet">UTF-8</prop><!-- added -->
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.caveofprogramming.pring.web.dao</value>
</list>
</property>
</bean>
这似乎行不通
检查 list :
- 数据库架构设置为 utf8 - utf8_unicode_ci。
- Hibernat 配置将字符集添加到 UTF-8。
- jsp 页面 language="java"contentType="text/html; charset=UTF-8" pageEncoding="UTF-8
- 我还按照这篇文章的建议添加了过滤器... Spring MVC UTF-8 Encoding
* 更新 * 这是我的 bean 和 DAO
表格
<sf:form method="POST" action="${pageContext.request.contextPath}/docreate" commandName="offer">
<sf:input type="text" path="id" name="id" readonly="true" />
<label for="text">Text</label>
<sf:textarea id="text" name="text" row="3" path="text"></sf:textarea>
<sf:errors path="text" cssClass="error"></sf:errors>
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
<input type="submit" value="Submit">
</sf:form>
报价
@Entity
@Table(name="offers")
public class Offer{
@Id
private int id;
private String text;
getIn(){}
.....
}
OfferDao
public class OfferDao{
@Autowired
private SessionFactory sessionFactory;
public Session currentSession(){
return sessionFactory.getCurrentSession();
}
public boolean create(Offer offer){
int hiberReturn =(int) currentSession().save(offer);
return hiberReturn >= 0;
}
}
非常感谢任何能提供帮助的人......真的......非常感谢
最佳答案
对于 Java (JDBC):
?useUnicode=yes&characterEncoding=UTF-8 in the getConnection() call.
对于 Hikari(也许):
spring.jpa.properties.hibernate.connection.characterEncoding=utf-8
spring.jpa.properties.hibernate.connection.CharSet=utf-8
spring.jpa.properties.hibernate.connection.useUnicode=true
Spring/Hibernate 过滤器:
<form accept-charset="UTF-8">
Spring/Hibernate: <property name="url"
value="jdbc:mysql://localhost:3306/miniprojetjee?useUnicode=true
&connectionCollation=utf8_general_ci
&characterSetResults=utf8&characterEncoding=utf-8"/>
"Spring": @RequestMapping(value = "/getRegion2",
produces={"application/json; charset=UTF-8"},method = RequestMethod.GET)
另请参阅:https://docs.jboss.org/exojcr/1.12.13-GA/developer/en-US/html/ch-db-configuration-hibernate.html
关于mysql - Hibernate Spring 编码字符集错误 [UTF-8],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37289097/