<select id="findByMap" parameterType="java.util.Map" resultMap="baseResultMap">
select * from t_user t where
<choose>
<when test="map.keys.size > 0">
<foreach collection="map.keys" item="key"
separator="AND">
${key} = #{map[${key}]}
</foreach>
</when>
<otherwise>
1=2
</otherwise>
</choose>
</select>
DAO层:
List<TUser> findByMap(@Param("map") Map<String, Object> map);
Test:
@Test
public void testMap() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 3);
map.put("username", "李四");
List<TUser> list = dao.findByMap(map);
print(list);
}
SQL日志:
Preparing: select * from t_user t where id = ? AND username = ?
Parameters: 3(Integer), 李四(String)