广告
返回顶部
首页 > 资讯 > 后端开发 > Python >mybatis 传入null值的解决方案
  • 621
分享到

mybatis 传入null值的解决方案

2024-04-02 19:04:59 621人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

目录mybatis 传入null值解决dao层xml文件内容mybatis 注入老是为null终于过了一会儿,看代码时突然顿悟mybatis 传入null值解决 前端传入两个值,如果

mybatis 传入null值解决

前端传入两个值,如果其中一个为null时,很多时候我们都很困惑,明明传入的是null,为啥mybatis 的xml文件中的if条件判断无效?

public String getPersonInfo(@PathParam("Name") String Name, @PathParam("IDCard") String IDCard)

dao层

public List<Map> getPersonInfo(@Param("name") String name, @Param("idcard") String idcard);

xml文件内容

<if test="name == null or name == '0'.toString()"> 
    <![CDATA[ and (b.identity_id = #{idcard,javaType=String,jdbcType=VARCHAR})
    group by name,id_card,birthTime,sex  ]]> 
</if>

每次执行都是失败的,网上很多都说要在dao层添加@param注解和xml文件内容中要加入jdbcType类型就能解决,最后还是没解决

其实还有一个点忽略了,就是传入时的null值其实是个字符串null,根本就不是null,它是个字符串null

        if (Name == null || "null".equalsIgnoreCase(Name)) {
            Name = null;
        }
        if (IDCard == null || "null".equalsIgnoreCase(IDCard)) {
            IDCard = null;
        }

问题解决!

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: mybatis 传入null值的解决方案

本文链接: https://www.lsjlt.com/news/142001.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作