时间:2021-07-01 10:21:17 帮助过:30人阅读
以下的文章主要讲述的是Hibernate+MySQL 中文问题的实际解决方法,即 MySQL数据库安装时我们设置的相关字符集为 utf-8, jdbc 驱动3.0.15 以上,以下的内容就是对Hibernate+MySQL 中文问题的实际解决方案的介绍。 1、Hibernate+MySQL 中文问题解决方案:MySQL
以下的文章主要讲述的是Hibernate+MySQL 中文问题的实际解决方法,即 MySQL数据库安装时我们设置的相关字符集为 utf-8, jdbc 驱动3.0.15 以上,以下的内容就是对Hibernate+MySQL 中文问题的实际解决方案的介绍。
1、Hibernate+MySQL 中文问题解决方案:MySQL安装时设置字符集utf-8, jdbc驱动3.0.15以上.
2. hibernate配置文件中,加上属性
- <property name="connection.useUnicode">trueproperty>
- <property name="connection.characterEncoding">UTF-8property>
3. web.xml设置Filter
- <filter>
- <filter-name>
- Set Web Application Character Encoding
- filter-name>
- <filter-class>cn.com.commnet.util.SetEncodeFilterfilter-class>
- <init-param>
- <param-name>defaultencodingparam-name>
- <param-value>UTF-8param-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;
- }
- }
以上的相关内容就是对Hibernate+MySQL 中文问题解决方案. 的介绍,望你能有所收获。