Hibernate 存入中文到 mysql 乱码解决方案

前置条件mysql 数据库编码为utf-8

在与spring的集成中 配置如下

<bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFact oryBean">
<property name="dataSource">
<ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>sk/krajci/mvp/business/hbm/person.hbm.xml</value>
<value>sk/krajci/mvp/business/hbm/role.hbm.xml</value>
</list> 
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.connection.useUnicode">true</prop>
<prop key="hibernate.connection.characterEncoding">utf-8</prop>
</props>
</property>
</bean>

单个 hibernate 配置文件中 配置如下 在hibernate.cfg.XML文件的<session-factory/>块中设置

<property name="connection.useUnicode">true</property> 
<property name="connection.characterEncoding">UTF8</property>

设置连接字符串 注意xml中 & 对应&amp;

<property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</property>
 

猜你喜欢

转载自tiame.iteye.com/blog/1571424