zoukankan      html  css  js  c++  java
  • hibernate自动建表采用UTF-8字符编码

    hibernate自动建表采用UTF-8字符编码

    hibernate建表默认为UTF-8编码

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

    蕃薯耀 2016年4月14日 15:30:49 星期四

    http://fanshuyao.iteye.com/

     

    一、问题:

    hibernate自动建表的编码应该是数据默认的编码格式,一般也不是utf-8。所以想要建表默认的编码是UTF-8,应该怎么做呢?

     

    二、解决方法:

    拿mysql举例:

     

    (一)、修改hibernate建表的方言

     

    1、一般情况我们使用的mysql方言为:org.hibernate.dialect.MySQL5Dialect

    默认返回的是

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

     

    2、重写MySQL5InnoDBDialect类,覆盖getTableTypeString方法

    package com.lqy.spring.hibernate.mysql;
    
    import org.hibernate.dialect.MySQL5InnoDBDialect;
    
    public class MySQL5DialectUTF8 extends MySQL5InnoDBDialect{
    
    	@Override
    	public String getTableTypeString() {
    		return " ENGINE=InnoDB DEFAULT CHARSET=utf8";  
    	}
    }

     

    3、方言配置使用我们重写的类,配置如下:

     

    (1)Jpa数据库连接配置:

    把默认的配置

    <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />

    修改成

    <property name="hibernate.dialect" value="com.lqy.spring.hibernate.mysql.MySQL5DialectUTF8" />

    (2)spring整合hibernate配置:

    <prop key="hibernate.dialect">com.lqy.spring.hibernate.mysql.MySQL5DialectUTF8</prop>
    <property name="jpaProperties">
    			<props>
    			
                  	
                  	<!-- 自动建表类型 validate|create|create-drop|update -->
                  	<prop key="hibernate.hbm2ddl.auto">update</prop>
                  	<!-- 是否显示SQL -->
                  	<prop key="hibernate.show_sql">false</prop>
                  	<!-- 显示SQL是否格式化 -->
                  	<prop key="hibernate.format_sql">false</prop>
                  	<prop key="hibernate.dialect">com.lqy.spring.hibernate.mysql.MySQL5DialectUTF8</prop>
    			</props>
    		</property>

    4、修改数据连接Url

    jdbc.url=jdbc:mysql://192.168.1.11:3306/db?useUnicode=true&amp;characterEncoding=UTF-8

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

    蕃薯耀 2016年4月14日 15:30:49 星期四

    http://fanshuyao.iteye.com/

  • 相关阅读:
    Delux DLVB13摄像头在Windows Vista下的使用
    Windows在删除文件时怎么不确认了?
    Tornado启动仿真器时出现错误:error : simulator failed to initialize before timeout.
    VxWorks下使用双向链表的小例子
    MPI错误:提示XXX Credentials for yyy rejected connecting to XXX
    运行Google CTemplate首页的例子遇到_CrtIsValidHeapPointer异常
    拖延不是毛病,是你不够强大
    BNF范式含义和基本用法
    堆栈的区别
    永不抱怨
  • 原文地址:https://www.cnblogs.com/fanshuyao/p/6227139.html
Copyright © 2011-2022 走看看