前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, “xxxx“)] with root cause

org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, “xxxx“)] with root cause

作者头像
关忆北.
发布2020-10-15 15:59:20
5.3K0
发布2020-10-15 15:59:20
举报
文章被收录于专栏:关忆北.关忆北.

在使用MyBatis时,遇到这样的问题: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "name")] with root cause

代码语言:javascript
复制
    <select id="getTotal" resultType="java.lang.Long">
        select count(*) from employee e
        <where>
        <!--<if test="emp !=null">-->
            <if test="emp.name != null and emp.name!=''">
                and e.name like concat('%',#{emp.name},'%')
            </if>
            <if test="emp.politicId != null">
                and e.politicId =#{emp.politicId}
            </if>
            <if test="emp.jobLevelId != null">
                and e.jobLevelId =#{emp.jobLevelId}
            </if>
            <if test="emp.posId != null">
                and e.posId =#{emp.posId}
            </if>
            <if test="emp.nationId != null">
                and e.nationId =#{emp.nationId}
            </if>
            <if test="emp.departmentId != null">
                and e.departmentId =#{emp.departmentId}
            </if>
            <if test="emp.engageForm != null">
                and e.engageForm =#{emp.engageForm}
            </if>
            <if test="beginDateScope !=null">
                and emp.beginDate between #{beginDateScope[0]} and #{beginDateScope[1]}
            </if>
        <!--</if>-->
        </where>
    </select>
怎么出现问题的?

我在Mapper接口中定义了getTotal方法,在Service中调用这个方法,方法中需要传入一个Employee对象(在mapper中使用@Param注解取别名emp),我在调用时传入的值为null.

出现的什么问题?

报错:

代码语言:javascript
复制
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression 'emp.name != null and emp.name!='''. Cause: org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "name")] with root cause

org.apache.ibatis.ognl.OgnlException: source is null for getProperty(null, "name")
为什么出现这个问题?

经过谷歌发现,这个异常是MyBatis封装的,类似空指针异常,xml其实是Mapper接口的一个实现类,当一个为null的对象传入后,在xml的方法中使用这个值,就报这个错

解决:

可以在xml中增加对emp对象的判空语句,就是在上文中注释掉的xml语句,更改后,如果emp为null,就不执行where语句

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 怎么出现问题的?
  • 出现的什么问题?
  • 为什么出现这个问题?
  • 解决:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档