mybatis的常见问题

mybatis的常见问题


分页

分页经常会遇到的问题,就是< (小于)、>(大于) 的问题,这里提供2种方式

方式1:使用html替换符

   <if test="startIndex !=null and startIndex !=''">
    rn &gt;${startIndex}
   </if>
   <if test="endIndex !=null and endIndex !=''">
    rn &lt;${endIndex}
   </if>

方式2:使用cdata可以直接写SQL操作

   <if test="startIndex !=null and startIndex !=''">
    <![CDATA[ and rn <= ${startIndex}  ]]>
   </if>
   <if test="endIndex !=null and endIndex !=''">
    <![CDATA[ and rn <= ${endIndex}  ]]>
   </if>

Mapper.xml中的 # 与 $

#可以防止SQL注入 $不能防止

#的参数是带引号的 而$不带引号

使用#和$ 主要取决于 1 是否需要防止SQL注入 2 是否是字符串参数

时间段的处理方式

<if test="operateBeginDate!=null and operateBeginDate!='' and operateEndDate!=null and operateEndDate!=''">
       <![CDATA[  
       and to_date(OPERATE_DATE, 'yyyy-mm-dd') between  to_date(#{operateBeginDate}, 'yyyy-mm-dd')  
       and to_date(#{operateEndDate},'yyyy-mm-dd')    
       ]]>
</if>

异常为:Mapped Statements collection does not contain value for后面是什么类什么方法之类的

错误原因有几种:

  • mapper.xml中没有加入namespace
  • mapper.xml中的方法和接口mapper的方法不对应
  • mapper.xml没有加入到mybatis-config.xml中(即总的配置文件),例外:配置了mapper文件的包路径的除外
  • mapper.xml文件名和所写的mapper名称不相同。

mybatis 的外键关联 ,查询父表

1.数据库增加字段关联 (可选)
 add constraint FK_AREA foreign key (AREA)
  references SYS_AREA (ID);
commit;
2. 首先生成VO 和 mapper.xml 在VO中添加对象字段
<association property="sysArea" column="BANK_AREA"  javaType="model.Area" select="model.Area.selectByPrimaryKey" />

mybatis 的外键关联 ,查询子表

生成VO和mapper.xml 在VO中添加子对象集合(1对多情况)
<collection property="comments" javaType="ArrayList" column="id" ofType="Comment" select="getCommentsById"/>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值