• 关于mybatis中基本类型条件判断问题


    一:发现问题

    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);  

    原文:http://blog.csdn.net/cclovett/article/details/12855505

  • 相关阅读:
    day14_集合框架1(ArrayList,LinkedList,HashSet)
    day13_String、StringBuffer、StringBuilder
    初识Java_day01
    关于局部内部类访问带有final修饰符的局部变量
    day03,day04_数组,循环(上)
    day09(下)_异常(上)
    day08_多态
    day11_多线程(多线程安全问题)
    day16_集合框架3(HashMap,TreeMap)
    day09(上)_内部类
  • 原文地址:https://www.cnblogs.com/zhuhc/p/4250493.html
Copyright © 2020-2023  润新知