java mapping.xml返回map 加if判断null
时间: 2024-03-11 12:49:55 浏览: 123
在 Java 的 Mapping XML 中,如果需要在返回 Map 的同时判断某个属性是否为 null,可以使用如下的语法:
```
<resultMap id="resultMap" type="java.util.Map">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap>
<select id="selectMap" resultMap="resultMap">
SELECT id, name, age FROM users
<where>
<if test="name != null and name != ''">
AND name = #{name}
</if>
<if test="age != null">
AND age = #{age}
</if>
</where>
</select>
```
在这个例子中,我们首先定义了一个名为 resultMap 的 resultMap,它的类型是 java.util.Map。接着,我们定义了三个映射关系,将查询结果映射到 Map 中的键值对。然后,我们在 select 标签中使用 resultMap 属性,告诉 MyBatis 使用 resultMap 来映射查询结果。
在 SQL 查询语句中,我们使用了 <where> 标签来动态生成 WHERE 子句。在 <where> 标签中,我们使用了 <if> 标签来判断属性是否为 null。如果 name 不为 null 且不为空字符串,则生成 name = #{name} 的查询条件;如果 age 不为 null,则生成 age = #{age} 的查询条件。
当我们执行 selectMap 方法时,MyBatis 会将查询结果映射成一个 Map 对象,并根据查询条件进行过滤。如果 name 或 age 为 null,则不会生成对应的查询条件。
阅读全文
相关推荐


















