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;<!--不能写分号,因为他们底层拼接了sql-->
  </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出现的符号
方案:

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

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 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值