在MyBatis中处理空值有几种方法,具体取决于你想要实现的功能和业务逻辑:
<select id="selectUsers" parameterType="java.util.Map" resultType="User">
SELECT * FROM users
<where>
<if test="username != null">
AND username = #{username}
</if>
<if test="age != null">
AND age = #{age}
</if>
</where>
</select>
<select id="selectUsers" parameterType="java.util.Map" resultType="User">
SELECT * FROM users
<where>
<choose>
<when test="username != null">
AND username = #{username}
</when>
<when test="age != null">
AND age = #{age}
</when>
</choose>
</where>
</select>
<sql id="condition">
<if test="username != null">
AND username = #{username}
</if>
<if test="age != null">
AND age = #{age}
</if>
</sql>
<select id="selectUsers" parameterType="java.util.Map" resultType="User">
SELECT * FROM users
<where>
<include refid="condition" />
</where>
</select>
通过以上方式,可以根据需要选择合适的方法来处理空值,使SQL语句更加灵活和可读。