mybatis 开发中遇到的问题(一)

1、sql中存在分号“;”问题:org.apache.ibatis.binding.BindingException: Parameter ‘logisticId’ not found. Available parameters are [logisticsId, param1, param2, type]

    <select id="selectByType" resultMap="BaseResultMap" parameterType="java.lang.String" > 
        select 
            <include refid="Base_Column_List" />
        from WL_INDEX_ADVERT
        where TYPE = #{type,jdbcType=VARCHAR}
        ORDER BY SORT ASC;
  select>

2、问题:mybatis 空指针异常:忘记写#,自己检查很难发现

  AND  TRUE_NAME like '%'||{sysUser.trueName,jdbcType=VARCHAR}||'%'
  AND  TRUE_NAME like '%'||#{sysUser.trueName,jdbcType=VARCHAR}||'%'

3、问题:The content of elements must consist of well-formed character data or markup.
CDATA
分析:出现这个问题是因为sql中使用了”<”“>”等,不允许在XML出现的符号
方案:

 标记避免Sql中与xml规范相冲突的字符对xml映射文件的合法性造成影响
    如:= #{price_from}]]>
因为这个是xml格式的,所以不允许出现类似“>”这样的字符,但是都可以使用符号进行说明,将此类符号不进行解析 
你的可以写成这个: 
mapper文件示例代码        
    <       < 
    &gt;       >  
    <>   <>
    &      & 
    '     '
    "     "

4、问题:java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.hc360.logistics.dao.ShopServiceMapper.selectByLogisticsId

 答案:没有shopServiceMapper.xml 或是 shopServiceMapper.xml 中没有id为selectByLogisticsId 的sql

5、问题:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.reflect.UndeclaredThrowableException
### The error may exist in file [D:\workSpace\cm\src\main\webapp\WEB-INF\classes\mybatis\mapper\SerMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### Cause: java.lang.reflect.UndeclaredThrowableException

答案:在分页插件中查询总记录数时,出的错,它会把第一个select 替换掉,导致出的错

   代码:
            mybatis分页插件: PagingStatementHandlerInterceptor   intercept()方法代码片段:
                         int idx = getFromIndex(newCountSql);
                         newCountSql = "select count(*) as cnt " + newCountSql.substring(idx);
             sql:
                         select count(*) as cnt  from sys_user su WHERE su.Id=122
                         union     
                         SELECT * from sys_user su WHERE su.Id>=618
方案:
                    select * from (
                         SELECT * from sys_user su WHERE su.Id=122
                         union     
                         SELECT * from sys_user su WHERE su.Id>=618
                    ) order by id 

你可能感兴趣的:(mybatis)