mysql 默认使用的字符集是 latin1,而通常在开发中保存的字符串是 UTF-8 编码的(字符集是 Unicode),所以数据库通常使用是是 UTF-8 编码。
在Hibernate的主配置文件【hibernate.cfg.xml】中找到属性:
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
在“jdbc:mysql://localhost:3306/test”后面加上“?useUnicode=true&characterEncoding=UTF-8”。在xml文件中,“&” 要改成 “&”,所以此处的“&”就表示“&”。
最终结果如下:
<property name="connection.url">
jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
</property>