hibernate的中文问题的解决方法的补充

来源:互联网 发布:c语言中abs是什么意思 编辑:程序博客网 时间:2024/04/30 02:52

网上看到一个hibernate+mysql中文问题的解决方案,如下

系统配置:win2k3 server,jsdk 1.5.0 rc,mysql 4.0.20a,hibernate 2.1.0 ,elcipse 3.0.1
问题描述:在使用hibernate作为数据持久层的方案时,照样会遇到中文问题,具体情况时插入到数据库中的汉字显示为??,显示数据时汉字为??
探索: 经过试验发现与汉字编码有关的地方有以下两处,一个是数据库连接url ,一个是编程时获取Configuration 类的实例的语句的方式。
方案1:
    * 编程时获取Configuration 类的实例的语句为
    * new Configuration().addClass(xx.class);
    必须在hibernate.properties文件里数据库连接url后加上?    characterEncoding=gbk&useUnicode=true
方案2:
    * 编程时获取Configuration 类的实例的语句为
    * new Configuration().configure().
    在hibernate.cfg.xml文件的<session-factory/>块中设置
        <property name="connection.useUnicode">true</property> 
        <property name="connection.characterEncoding">UTF-8</property>
    或编程时写:
      Properties extraProperties = new Properties(); 
      extraProperties.put("hibernate.connection.useUnicode", "true"); 
      extraProperties.put("hibernate.connection.characterEncoding", "UTF-8"); 
      myConfiguration.addProperties(extraProperties);

 

但我子啊hibernate3.1+mysql5中,必须同时设置url的参数和<property>属性,否则照样不能正常存储中文

原创粉丝点击