Hibernate Spring编码字符集错[UTF-8]

时间:2016-05-18 02:12:10

标签: mysql hibernate spring-mvc utf-8 character-encoding

开发环境:

  • Spring 4.2x
  • Hibernate 4.3.11
  • Tomcat 8服务器
  • MySQL 5.4

在更改Hibernate之前,插入,检索和显示字符没有问题。然而,在我的DAO中进行更改后奇怪地使用Hibernate我似乎无法将正确的字符插入到MySQL DB中。

我确保MySQL Schema确实可以通过使用查询“INSERT INTO spring_normalizeofferstextusers_username)VALUES来保存UTF-8字符集( 'ölm','lalalal');“ index.jsp上的输出是正确的。

我修改了我的hibernate配置

<bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.connection.useUnicode">true</prop><!-- added -->
                <prop key="hibernate.connection.characterEncoding">UTF-8</prop><!-- added -->
                <prop key="hibernate.connection.charSet">UTF-8</prop><!-- added -->

            </props>
        </property>

        <property name="packagesToScan">
            <list>
                <value>com.caveofprogramming.pring.web.dao</value>
            </list>
        </property>
    </bean>

这似乎不起作用

检查清单:

  1. 数据库架构设置为utf8 - utf8_unicode_ci。
  2. Hibernat config将charSet添加到UTF-8。
  3. jsp page language =“java”contentType =“text / html; charset = UTF-8” 的pageEncoding =“UTF-8
  4. 我还添加了过滤器,因为这篇文章建议...... Spring MVC UTF-8 Encoding
  5. *更新* 这是我的bean和DAO

    表格

    <sf:form method="POST" action="${pageContext.request.contextPath}/docreate" commandName="offer">
        <sf:input type="text" path="id" name="id" readonly="true" />
        <label for="text">Text</label>
        <sf:textarea id="text" name="text" row="3" path="text"></sf:textarea>
        <sf:errors path="text" cssClass="error"></sf:errors>
        <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
        <input type="submit" value="Submit">
    
    </sf:form>
    

    优惠

    @Entity
    @Table(name="offers")
     public class Offer{
      @Id
      private int id;
      private String text;
    
      getIn(){}
      .....
    }
    

    OfferDao

    public class OfferDao{
       @Autowired
       private SessionFactory sessionFactory;
    
       public Session currentSession(){
        return sessionFactory.getCurrentSession();
       }
    
       public boolean create(Offer offer){
         int hiberReturn =(int) currentSession().save(offer);
    
         return hiberReturn >= 0;
       }
    }  
    

    任何可以提供帮助的人都非常感激......真的......非常感谢

2 个答案:

答案 0 :(得分:3)

For Java(JDBC):

?useUnicode=yes&characterEncoding=UTF-8 in the getConnection() call.

对于Hikari(也许):

spring.jpa.properties.hibernate.connection.characterEncoding=utf-8  
spring.jpa.properties.hibernate.connection.CharSet=utf-8  
spring.jpa.properties.hibernate.connection.useUnicode=true

Spring / Hibernate过滤器:

<form accept-charset="UTF-8">

Spring/Hibernate: <property name="url"
    value="jdbc:mysql://localhost:3306/miniprojetjee?useUnicode=true
           &connectionCollation=utf8_general_ci
           &characterSetResults=utf8&characterEncoding=utf-8"/>

"Spring": @RequestMapping(value = "/getRegion2",
    produces={"application/json; charset=UTF-8"},method = RequestMethod.GET)

另请参阅:https://docs.jboss.org/exojcr/1.12.13-GA/developer/en-US/html/ch-db-configuration-hibernate.html

答案 1 :(得分:1)

当我使用简单的表单输入创建另一个较小的Web应用程序时,我实际上意外地解决了这个问题。对于那些遇到同样问题的人,请查看上面的检查清单,确保你拥有一切。

最重要的是要确保字符编码过滤器必须是web.xml上的第一个过滤器原因.....我不知道原因,所以如果你这样做...请在下面发表评论

重要!!!首先在web.xml上过滤

    <filter>
      <filter-name>SetCharacterEncodingFilter</filter-name>
      <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
        <filter-name>SetCharacterEncodingFilter</filter-name>
        <url-pattern>*</url-pattern>
  </filter-mapping>