Solution to the Chinese problem of Hibernate + MySQL

Source: Internet
Author: User

The following articles mainly describe the actual solution to the Chinese problem of Hibernate + MySQL, that is, the relevant character set we set during MySQL database installation is UTF-8, And the jdbc driver is later than 3.0.15, the following describes the actual solution to the Chinese problem of Hibernate + MySQL.

1. Hibernate + MySQL Chinese problem solution: Set the character set UTF-8 during MySQL installation, jdbc driver 3.0.15 or above.

2. Add attributes to the hibernate configuration file.

 
 
  1. <property name="connection.useUnicode">true</property>   
  2. <property name="connection.characterEncoding">UTF-8</property>  

3. Set Filter in web. xml
 

 
 
  1. <filter>   
  2. <filter-name>   
  3. Set Web Application Character Encoding   
  4. </filter-name>   
  5. <filter-class>cn.com.commnet.util.SetEncodeFilter</filter-class>   
  6. <init-param>   
  7. <param-name>defaultencoding</param-name>   
  8. <param-value>UTF-8</param-value>   
  9. </init-param>   
  10. </filter>   
  11. <filter-mapping>   
  12. <filter-name>   
  13. Set Web Application Character Encoding   
  14. </filter-name>   
  15. <url-pattern>/*</url-pattern>   
  16. </filter-mapping>   
  17. SetEncodeFilter.java   
  18. public class SetEncodeFilter implements Filter {   
  19. protected FilterConfig filterConfig = null;   
  20. protected String defaultEncoding = null;   
  21. /**//* (non-Javadoc)   
  22. * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)   
  23. */   
  24. public void init(FilterConfig arg0) throws ServletException {   
  25. // TODO Auto-generated method stub   
  26. this.filterConfig = arg0;   
  27. this.defaultEncoding = filterConfig.getInitParameter("defaultencoding");   
  28. }   
  29. /**//* (non-Javadoc)   
  30. * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, 
    javax.servlet.ServletResponse, javax.servlet.FilterChain)   
  31. */   
  32. public void doFilter(   
  33. ServletRequest request,   
  34. ServletResponse response,   
  35. FilterChain chain)   
  36. throws IOException, ServletException {   
  37. // TODO Auto-generated method stub   
  38. request.setCharacterEncoding(selectEncoding(request));   
  39. chain.doFilter(request, response);   
  40. }   
  41. public void destroy() {   
  42.  
  43. this.defaultEncoding = null;   
  44. this.filterConfig = null;   
  45. }   
  46. protected String selectEncoding(ServletRequest request) {   
  47. return this.defaultEncoding;   
  48. }   
  49. }  

The above content is an introduction to the Chinese problem solution for Hibernate + MySQL. I hope you will have some gains.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.