Hibernate+mysql 不能保存text类型的中文的解决方法
hibernate.cfg.xml<property name="connection.url">
jdbc:mysql://localhost:3306/somedb?useUnicode=true&characterEncoding=GBK
</property>
注意后面的useUnicode=true&characterEncoding=GBK参数,作用是存入或取出的都用gbk编码。
但是当数据库是utf-8时,会出现中文不能保存。
如
class News{ TABLE NEWS(
String title; 对应 TITLE varchar(50) ,
String content; CONTENT text,
} )
News.hbm.xml
<property name="title" column="TITLE" type="string"></property>
<property name="content" column="CONTENT" type="text"></property>
如果是这样
News news=new News();
news.setTitle("xxxx")
news.setContent("english");
正常。
但是如果是
News news=new News();
news.setTitle("xxxx")
news.setContent("中文");
则出错:com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'CONTENT'
办法就是把hibernate.cfg.xml 的?useUnicode=true&characterEncoding=GBK这串参数去掉。
如果 <b>text</b> 那个字段是utf8 ,万一还是不行的话,将text 字符类型改成 <b>gbk</b>
页:
[1]