配置hibernate常见问题

连接MySql时出现:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration

转载自:https://www.cnblogs.com/EasonJim/p/6906713.html

在连接字符串后面加上?serverTimezone=UTC

其中UTC是统一标准世界时间。

完整的连接字符串示例:jdbc:mysql://localhost:3306/test?serverTimezone=UTC

或者还有另一种选择:jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8,这个是解决中文乱码输入问题,当然也可以和上面的一起结合:jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC

注:报错 The reference to entity "characterEncoding" must end with the ';' delimiter

在xml中不能直接写&,这是由xml文件中的编码规则决定要这么变换。

在xml文件中有以下几类字符要进行转义替换:

 
原文 替换
&lt; < 小于号
&gt; > 大于号
&amp; & 和
&apos; ’ 单引号
&quot; ” 双引号

 

解决mysql插入中文出现错误Incorrect string value: '\xE7\xA8\x8B\xE5\xBA\x8F...' for column 'course' at row 1

参考:

  https://blog.csdn.net/qq_27632921/article/details/84031078

  https://blog.csdn.net/xuebing1995/article/details/75087726

1. 通过命令行登陆到当前使用的数据库中;

2. 使用 show create table tableName;查看数据表的编码是什么,如果是下图所示则需要修改为 utf-8 的编码格式。

3. 修改数据表的字符集: 

  (1)通过命令行修改:alter table tableName(你自己的表名) default character set utf8;

  (2)通过hibernate配置修改:

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

    

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

    我们自定义类,继承 org.hibernate.dialect.MySQL5Dialect ,重写getTableTypeString就好了:

  

public class MysqlDialectUTF8 extends MySQL5Dialect {
    @Override
    public String getTableTypeString() {
        return "ENGINE=InnoDB DEFAULT CHARSET=utf8";
    }
}

4. 查看修改结果: show create table tableName;

猜你喜欢

转载自www.cnblogs.com/yingtoumao/p/11027843.html