日期:2014-05-17  浏览次数:20696 次

急救!在使用ibatis+struts2写模糊查询时,报错
配置文件:
<select id="getGoodsByKeyword" resultClass="goods" parameterClass="string">
     select * from goods where goodName like concat('%',#goodName#,'%')
    </select>

异常:
com.ibatis.common.jdbc.exception.NestedSQLException:   
--- The error occurred in com/dcst/imicai/bean/goods.xml.  
--- The error occurred while applying a parameter map.  
--- Check the getGoodsByKeyword-InlineParameterMap.  
--- Check the statement (query failed).  
--- Cause: java.sql.SQLException: Illegal mix of collations (gb2312_chinese_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation 'like'

------解决方案--------------------
你换成 UTF-8 字符集试试。
以 MySQL 为例,连接 URL 这样写jdbc:mysql://localhost:3306/testdb?characterEncoding=utf8
数据库 ,CHARSET=utf8,Collation = utf8_general_ci
------解决方案--------------------
这个异常信息告诉你,编码不一样,无法进行模糊比较。首要问题还是要把编码格式统一。
------解决方案--------------------
楼主尝试吧goodName的值打印出来试试,看看是不是乱码。。
另外为了统一字符,可以把链接数据库的url写成这样:
jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF8