HIbernate创建表插入中文会乱码

在整合SSH的时候,我往数据库插入一条中文数据会报错

ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - Incorrect string value: '\xE6\xB2\x88\xE9\x9B\xAA...' for column 'username' at row 1

一般情况我们使用的mysql方言为org.hibernate.dialect.MySQL5Dialect 
默认返回的是:


@Override  
public String getTableTypeString()
{  
return" ENGINE=InnoDB";  
}


于是我重写了一个类,把这个方法重写了一下。

package com.liu.util;

import org.hibernate.dialect.MySQL5InnoDBDialect;

public class Unicodeextends MySQL5InnoDBDialect{

    @Override 

    public String getTableTypeString() {  

        return" ENGINE=InnoDB DEFAULT CHARSET=utf8";    

    }  

}


在修改下hibernate配置文件

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>


修改为

<propertyname="dialect">com.liu.util.Unicode</property>

我一般在创建URL的时候都会加上:

useUnicode=true&characterEncoding=UTF-8


接下来,大功告成。。。








猜你喜欢

转载自blog.csdn.net/fukua2017/article/details/78878443