zoukankan      html  css  js  c++  java
  • 关于mybatis中基本类型条件判断问题

    零:sql动态语句中经常会有根据数据库某个字段状态进行判断的

    如:status=0为未激活,status=1为激活的,那搜索未激活时:

    <if test="model.activeStatus != null and model.activeStatus !='' or model.activeStatus==0">
                  and status=#{model.activeStatus}
    </if>

    但由于java的int类型默认值为0,导致0与null的判定无法识别。解决办法:

    1、int修改为Integer类型

    2、参数不要带activeStatus变量名,如:json的请求参数中不要带activeStatus参数名称。(未验证)

    一:发现问题

    sql动态语句中如果 parameterType="int"

    <select id="sel_campusinfo" parameterType="int" resultType="Campusinfo">  
            select cmpid,cmpname from campusinfo where state!='d' and cmpid=#{cmpid}  
    </select> 

    是正确的,但是如果加上if test

    <select id="sel_campusinfo" parameterType="int" resultType="Campusinfo">  
            select cmpid,cmpname from campusinfo where state!='d' and cmpid!=0  
            <if test="cmpid!=0">and cmpid=#{cmpid}</if>  
    </select> 

    There is no getter for property named 'cmpid' in 'class java.lang.Integer'   

    出错原因:Mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式取Integer.cmpid。Integer对象没有cmpid属性。如果不解析参数,mybatis自动识别传入的参数,不会报错。

    二:解决办法

    1.修改select语句

    <select id="sel_campusinfo" parameterType="int" resultType="Campusinfo">  
            select cmpid,cmpname from campusinfo where state!='d' and cmpid!=0  
            <if test="_parameter!=0">and cmpid=#{_parameter}</if>  
    </select>  

    参数名全部改为_parameter。

    2.不修改sql,只修改接口

    接口类:

    Campusinfo sel_campusinfo( int cmpid); 

    改为:

    Campusinfo sel_campusinfo(@Param(value="cmpid") int cmpid); 

    3.可以将参数包装在hashmap或者对象中作为参数

  • 相关阅读:
    我们失去了,我们又没有失去什么
    人过 40
    KPI绩效考核为何在国内不管用?
    再也不必当心我的密码了,多个SAP 客户端自动输入密码
    大器晚成
    人际能量相吸定律
    SQL SERVER函数——表值函数的处理
    MS-SQL SERVER单列合并的四种常用方法
    实战 SQL Server 2008 数据库误删除数据的恢复
    唉,怎么一眨眼就老了!
  • 原文地址:https://www.cnblogs.com/duanxz/p/5045919.html
Copyright © 2011-2022 走看看