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.
- <property name="connection.useUnicode">true</property>
- <property name="connection.characterEncoding">UTF-8</property>
3. Set Filter in web. xml
- <filter>
- <filter-name>
- Set Web Application Character Encoding
- </filter-name>
- <filter-class>cn.com.commnet.util.SetEncodeFilter</filter-class>
- <init-param>
- <param-name>defaultencoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>
- Set Web Application Character Encoding
- </filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- SetEncodeFilter.java
- public class SetEncodeFilter implements Filter {
- protected FilterConfig filterConfig = null;
- protected String defaultEncoding = null;
- /**//* (non-Javadoc)
- * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
- */
- public void init(FilterConfig arg0) throws ServletException {
- // TODO Auto-generated method stub
- this.filterConfig = arg0;
- this.defaultEncoding = filterConfig.getInitParameter("defaultencoding");
- }
- /**//* (non-Javadoc)
- * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
javax.servlet.ServletResponse, javax.servlet.FilterChain)
- */
- public void doFilter(
- ServletRequest request,
- ServletResponse response,
- FilterChain chain)
- throws IOException, ServletException {
- // TODO Auto-generated method stub
- request.setCharacterEncoding(selectEncoding(request));
- chain.doFilter(request, response);
- }
- public void destroy() {
-
- this.defaultEncoding = null;
- this.filterConfig = null;
- }
- protected String selectEncoding(ServletRequest request) {
- return this.defaultEncoding;
- }
- }
The above content is an introduction to the Chinese problem solution for Hibernate + MySQL. I hope you will have some gains.