mybatis在动态sql中使用map
使用mybatis动态sql时不能用万能的map去传参数:
- 数据库数据
- 运行SQL语句
- 更新成功
- xxxMapper
public int modify(Map<String, Object> map) throws Exception;
- xxxMapper.xml
<update id="modify" parameterType="map">
update smbms.bill
<set>
<if test="productName != null">
productName = #{pp},
</if>
<if test="isPayment != null">
isPayment = #{ip},
</if>
</set>
where id = #{id}
</update>
- 测试类(开启了自动提交在工具类中)
public void modify() throws Exception {
SqlSession sqlSession = MybatisUtils.getSqlSession();
BillMapper mapper = sqlSession.getMapper(BillMapper.class);
HashMap<String,Object> map = new HashMap<>();
map.put("pp","BiliBili");
map.put("bc",222);
map.put("id",1);
mapper.modify(map);
sqlSession.close();
}
- 报错
- 将xxxMapper.xml中语句去掉动态sql
<update id="modify" parameterType="map">
update smbms.bill set productName = #{pp},billCode = #{bc} where id = #{id}
<!-- <set>
<if test="productName != null">
productName = #{pp},
</if>
<if test="isPayment != null">
billCode = #{bc},
</if>
</set>
where id = #{id}-->
</update>
- 再次运行,更新成功
原因分析:
猜测动态sql<set>和<if>标签中不能使用万能map