Today, a problem is encountered, using the Ibatis framework to access the Oracle data encoded as US7ASCII garbled,
It took a long time to find a solution. First Sqlmap-config.xml is configured as follows <sqlmapconfig> <!--<sqlMap> elements are used to include the SQL map map file and other SQL map configuration files. - <settings lazyloadingenabled = usestatementnamespaces = "true" /> <typealias type = "Com.jerry.spring.util.EncodingStringTypeHandlerCallback" Span style= "font-style:normal; Color:windowtext; " > alias = "ENCODE" / <typehandler callback = "ENCODE" javatype = "java.lang.String" /> <sqlmap resource="Com/jerry/spring/model/user_sqlmap.xml" /> <sqlmap resource="Com/jerry/spring/model/timerecord_ Sqlmap.xml " /> </sqlmapconfig>EncodingstringtypehandlercallbackEncodingstringtypehandlercallbackclass InheritanceTypehandlercallback Interface
import java.sql.SQLException; import com.ibatis.sqlmap.client.extensions.ParameterSetter; import com.ibatis.sqlmap.client.extensions.ResultGetter; import com.ibatis.sqlmap.client.extensions.TypeHandlerCallback; public class Encodingstringtypehandlercallback implements typehandlercallback { //Boolean flag = initparam.getdbencoding ("dbencoding"); boolean flag = true;Public Object getresult (resultgetter arg0) throws SQLException { //Latin Character set conversion if(flag) { return stringutil. ISOTOGBK (Arg0.getstring ()); } return arg0.getstring (); }Public void setparameter (parametersetter setter, Object arg1) throws SQLException { if(arg1 instanceof String) { //Latin Character set conversion if(flag) {setter.setstring (stringutil. Gbktoiso((String) arg1)); } Else{setter.setstring ((String) arg1); } } }Public Object valueOf (String arg0) { //Latin Character set conversion if(flag) { return stringutil. ISOTOGBK (arg0); } return arg0; }} |
Stringutil
public class Stringutil { Public static string ISOTOGBK (string s) { If (s = = null | | (S.trim ()). Equals ("")) return s; try {s = new String (S.getbytes ("iso8859-1"), "GB2312"); } Catch (Exception e) { } return s; } Public static string Gbktoiso (string s) { If (s = = null | | (S.trim ()). Equals ("")) return s; try {s = new String (S.getbytes ("GBK"), "iso-8859-1"); } Catch (Exception e) { } return s; } } |
At this point, the problem of Chinese garbled solution Huan drink attention to personal open source code Https://github.com/zuifengke/windy code under the Medqcwebapp project
From for notes (Wiz)
Ibatis Accessing Oracle Database US7ASCII Chinese garbled problem