Heim >Datenbank >MySQL-Tutorial >Hibernate+MySQL 中文问题的解决

Hibernate+MySQL 中文问题的解决

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 16:14:161078Durchsuche

以下的文章主要讲述的是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配置文件中,加上属性

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">property</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"connection.useUnicode"</span><span class="tag">></span><span>true</span><span class="tag"></span><span class="tag-name">property</span><span class="tag">></span><span>   </span></span></span></li>
<li><span class="tag"><span class="tag-name">property</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"connection.characterEncoding"</span><span class="tag">></span><span>UTF-8</span><span class="tag"></span><span class="tag-name">property</span><span class="tag">></span><span>  </span></span></li>
</ol>

3. web.xml设置Filter
 

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span class="tag-name">filter</span><span class="tag">></span><span>   </span></span></span></li>
<li><span class="tag"><span class="tag-name">filter-name</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span>Set Web Application Character Encoding   </span></li>
<li>
<span class="tag"></span><span class="tag-name">filter-name</span><span class="tag">></span><span>   </span>
</li>
<li class="alt"><span class="tag"><span class="tag-name">filter-class</span><span class="tag">></span><span>cn.com.commnet.util.SetEncodeFilter</span><span class="tag"></span><span class="tag-name">filter-class</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><span class="tag-name">init-param</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span class="tag"><span class="tag-name">param-name</span><span class="tag">></span><span>defaultencoding</span><span class="tag"></span><span class="tag-name">param-name</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><span class="tag-name">param-value</span><span class="tag">></span><span>UTF-8</span><span class="tag"></span><span class="tag-name">param-value</span><span class="tag">></span><span>   </span></span></li>
<li class="alt">
<span class="tag"></span><span class="tag-name">init-param</span><span class="tag">></span><span>   </span>
</li>
<li>
<span class="tag"></span><span class="tag-name">filter</span><span class="tag">></span><span>   </span>
</li>
<li class="alt"><span class="tag"><span class="tag-name">filter-mapping</span><span class="tag">></span><span>   </span></span></li>
<li><span class="tag"><span class="tag-name">filter-name</span><span class="tag">></span><span>   </span></span></li>
<li class="alt"><span>Set Web Application Character Encoding   </span></li>
<li>
<span class="tag"></span><span class="tag-name">filter-name</span><span class="tag">></span><span>   </span>
</li>
<li class="alt"><span class="tag"><span class="tag-name">url-pattern</span><span class="tag">></span><span>/*</span><span class="tag"></span><span class="tag-name">url-pattern</span><span class="tag">></span><span>   </span></span></li>
<li>
<span class="tag"></span><span class="tag-name">filter-mapping</span><span class="tag">></span><span>   </span>
</li>
<li class="alt"><span>SetEncodeFilter.java   </span></li>
<li><span>public class SetEncodeFilter implements Filter {   </span></li>
<li class="alt">
<span>protected FilterConfig </span><span class="attribute">filterConfig</span><span> = </span><span class="attribute-value">null</span><span>;   </span>
</li>
<li>
<span>protected String </span><span class="attribute">defaultEncoding</span><span> = </span><span class="attribute-value">null</span><span>;   </span>
</li>
<li class="alt"><span>/**//* (non-Javadoc)   </span></li>
<li><span>* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)   </span></li>
<li class="alt"><span>*/   </span></li>
<li><span>public void init(FilterConfig arg0) throws ServletException {   </span></li>
<li class="alt"><span>// TODO Auto-generated method stub   </span></li>
<li>
<span class="attribute">this.filterConfig</span><span> = </span><span class="attribute-value">arg0</span><span>;   </span>
</li>
<li class="alt">
<span class="attribute">this.defaultEncoding</span><span> = </span><span class="attribute-value">filterConfig</span><span>.getInitParameter("defaultencoding");   </span>
</li>
<li><span>}   </span></li>
<li class="alt"><span>/**//* (non-Javadoc)   </span></li>
<li><span>* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, <br>javax.servlet.ServletResponse, javax.servlet.FilterChain)   </span></li>
<li class="alt"><span>*/   </span></li>
<li><span>public void doFilter(   </span></li>
<li class="alt"><span>ServletRequest request,   </span></li>
<li><span>ServletResponse response,   </span></li>
<li class="alt"><span>FilterChain chain)   </span></li>
<li><span>throws IOException, ServletException {   </span></li>
<li class="alt"><span>// TODO Auto-generated method stub   </span></li>
<li><span>request.setCharacterEncoding(selectEncoding(request));   </span></li>
<li class="alt"><span>chain.doFilter(request, response);   </span></li>
<li><span>}   </span></li>
<li class="alt"><span>public void destroy() {   </span></li>
<li><span> </span></li>
<li class="alt">
<span class="attribute">this.defaultEncoding</span><span> = </span><span class="attribute-value">null</span><span>;   </span>
</li>
<li>
<span class="attribute">this.filterConfig</span><span> = </span><span class="attribute-value">null</span><span>;   </span>
</li>
<li class="alt"><span>}   </span></li>
<li><span>protected String selectEncoding(ServletRequest request) {   </span></li>
<li class="alt"><span>return this.defaultEncoding;   </span></li>
<li><span>}   </span></li>
<li class="alt"><span>}  </span></li>
</ol>

以上的相关内容就是对Hibernate+MySQL 中文问题解决方案. 的介绍,望你能有所收获。


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn