因爲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>
四、從新部署應用,問題解決