Hibernate+mysql 中文问题解决方案.
生活随笔
收集整理的這篇文章主要介紹了
Hibernate+mysql 中文问题解决方案.
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1, mysql安裝時設置字符集utf-8, jdbc驅動3.0.15以上.
2. hibernate配置文件中,加上屬性
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
3. web.xml設置Filter
=================================================
<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;
????}
}
2. hibernate配置文件中,加上屬性
<property name="connection.useUnicode">true</property>
<property name="connection.characterEncoding">UTF-8</property>
3. web.xml設置Filter
=================================================
<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;
????}
}
轉載于:https://www.cnblogs.com/josson/archive/2005/03/16/119619.html
總結
以上是生活随笔為你收集整理的Hibernate+mysql 中文问题解决方案.的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络安全渗透概念
- 下一篇: 花呗的24期利息计算器_花呗24期怎么算