zoukankan      html  css  js  c++  java
  • Mybatis 注入老是为null

    今天遇到个很弱智的问题,以此记录!时刻提醒自己

        public int delExhibion(List<String> ids){
            Integer result = null;
            ExhibitionManager exhibitionManager = new ExhibitionManager();
            for (String id : ids){
                exhibitionManager.setId(id);
                exhibitionManager.setDelFlag("1");
                 result += exhibitionManagerMapper.delete(id);
            }
            return result;
        }

    发现老是执行 delete 的时候 ,老是报 空指针异常

    然后尝试使用:  @Param  给参数加上命令     

    int delete(@Param("id") String id);  

    结果还是不行,

    然后在尝试使用:对象传参 ,  这样总该注入进去了吧 

            int delete(Object dx);

    结果还是不行,

    然后在尝试使用:Mybatis 单个参数动态语句引用:

    是当我们的参数为String时,在sql语句中#{id} 会去我们传进来的参数调getId()方法获取参数,很明显,String没有对应的方法,所以报错了,那我们这里要如何引用id呢,需要采用下面的写法:

    <delete id="delete" parameterType="java.lang.String" >  
             SELECT * FROM table
             <where>  
                       <if test="_parameter != null">  
                                AND id= #{id}  
                       </if>  
             </where>  
    </select>

    单Mybatis传参为单个参数时,在sql语句中需要使用  _parameter 来引用这个参数

    结果还是不行。  

    终于过了一会儿,看代码时突然顿悟。

        public int delExhibion(List<String> ids){
            Integer result = null;
            for (String id : ids){
                 result += exhibitionManagerMapper.delete(id);
            }
            return result;
        }

    Integer  result  我给他设置默认值  为null,   并且还让  reuslt  这个对象   result +=   

    加等于在执行数据库操作返回结果时 用 result 去接收。 这不就一定是空指针了吗。

    我给 Integer result = 0;  设置个默认值为0   那也不会出现这种情况!

    或者 result =   给result  初始化赋值     也不会报  空指针异常!  不过这样做在我的业务中是不对的哦。  只是不会报错. 但是获取不到我想要的执行成功条数

    Integer result = null;
    result =  exhibitionManagerMapper.delete(id);

    真的是被自己气到了。以此记录!时刻提醒自己   

     public int delExhibion(List<String> ids){
            Integer result = 0;
            for (String id : ids){
                 result += exhibitionManagerMapper.delete(id);
            }
            return result;
        }
  • 相关阅读:
    python下RSA 加密/解密,签名/验证
    python字符串str和字节数组相互转化
    代码存档
    windows 7 安装 scrapy
    scrapy 爬取自己的博客
    win32api 找不到指定的模块
    SQLite3日期与时间,常见函数
    sqlite3日期数据类型
    myeclipse集成maven
    UIKit class hierarchy
  • 原文地址:https://www.cnblogs.com/blogspring/p/10123264.html
Copyright © 2011-2022 走看看