错误代码:
SEVERE: Servlet.service() for servlet [springMVC] in context with path [/mavenSpringHibernateBase]
threw exception [Request processing failed; nested exception is org.hibernate.exception.GenericJDBCException:
Incorrect string value: '\xE5\xBC\xA0\xE4\xB8\x89' for column 'user_name' at row 1] with root cause
java.sql.SQLException: Incorrect string value: '\xE5\xBC\xA0\xE4\xB8\x89' for column 'user_name' at row 1
解决办法
出现这个错误的原因是,我的数据库字段是latin1导致的,我的数据库是通过heibernate自动创建的,你把latin1改为utf8就可以用了
为了下次使用hibernate创建数据库不再默认使用latin1的字符集,我们需要修改dialect
import org.hibernate.dialect.MySQL5InnoDBDialect;
/**
* 因为hibernate自动创建的表,不是utf-8的,
* 采用如下方式,让他创建的表是utf-8的
* @author Gao
*/
public class MySQL5DialectUTF8 extends MySQL5InnoDBDialect{
@Override
public String getTableTypeString() {
return " ENGINE=InnoDB DEFAULT CHARSET=utf8";
}
}
config.properties
hibernate.dialect=com.gao.utils.MySQL5DialectUTF8
jdbcUrl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8