iBATIS sql语句中中文乱码解决

    因为sql语句直接在数据库里面能够查到数据,可是经过ibatis配置之后查不到数据,后经检查发现由于sql语句中的中文出现乱码引发的,例如 select a,b from table where c='合格' 经过iBATIS配置后在后台打印出来是select a,b from table where c='????',从而致使查不出数据,解决方案以下:java

一、建立监听器,主要是标红的语句web

package com.dhx.common;sql

import java.nio.charset.Charset;数据库

import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;编码

/**
 * Application Lifecycle Listener implementation class ChartSetListen
 *
 */
public class ChartSetListen implements ServletContextAttributeListener {spa

    /**
     * Default constructor. 
     */
    public ChartSetListen() {
        // TODO Auto-generated constructor stub
        com.ibatis.common.resources.Resources.setCharset(Charset.forName("gbk"));
    }.net

    /**
     * @see ServletContextAttributeListener#attributeAdded(ServletContextAttributeEvent)
     */
    public void attributeAdded(ServletContextAttributeEvent arg0) {
        // TODO Auto-generated method stub
    }xml

    /**
     * @see ServletContextAttributeListener#attributeReplaced(ServletContextAttributeEvent)
     */
    public void attributeReplaced(ServletContextAttributeEvent arg0) {
        // TODO Auto-generated method stub
    }部署

    /**
     * @see ServletContextAttributeListener#attributeRemoved(ServletContextAttributeEvent)
     */
    public void attributeRemoved(ServletContextAttributeEvent arg0) {
        // TODO Auto-generated method stub
    }
    
}
 get

二、在webxml中配置

<listener>
        <listener-class>com.dhx.common.ChartSetListen</listener-class>
    </listener>

注意:配置位置须要配置到Spring的ContextLoaderListener以前

三、将gbk编码配置成<context-param>

<context-param>
        <param-name>encoding</param-name>
        <param-value>
            gbk
        </param-value>
    </context-param>

四、从新部署应用,问题解决

相关文章
相关标签/搜索