Mybatis下报all elements are null 解决方法

一、出现问题一

解决方法:

select返回的list,断点看不到数据,只有all elements are null 

是返回类型问题

我idea下自动生成的是resultType,值是bomidou....这个是不行的

我xml上设置好了resultMap

	<resultMap id="testMap" type="com.test.entity.Test">
		……
	</resultMap>

这个时候,将resultType改为resultMap,选择对应的id即可解决

<select id="test" resultMap="testMap">
        ……
</select>

二、出现问题二

解决方法:

在使用mybatis返回值的时候,返回值类型为List<Map<String,Object>> ,由于ArrayList允许null存在,所以返回的list无法用非空来判断,他的长度为1 ,但是在取值的的时候会报空指针异常

解决方法:将sql语句返回的字段使用驼峰命名法命名

三、出现问题三

解决方法:

mybatis中使用到了arrayList,但是我们直接传入一个字符串的值,如下代码:

        <if test="serviceId != null and serviceId != '' ">
            and (service_id in
            <foreach item="item" index="index" collection="serviceId"  open="(" separator="," close=")">
                '${item}'
            </foreach>
            )
        </if>

出现上面配置文件肯定是报错了,我们需要把当前的字符串转为一个数组在进行处理,修正后代码如下:

        <if test="serviceId != null and serviceId != '' ">
            and (service_id in
            <foreach item="item" index="index" collection="serviceId.split(',')"  open="(" separator="," close=")">
                '${item}'
            </foreach>
            )
        </if>

以上是mybatis中all elements are null错误常见的解决方案,希望能帮助到大家。

猜你喜欢

转载自blog.csdn.net/Angel_asp/article/details/129469492