问题举例:
有一个带占位符的模板sql,语法跟mybatis的XML是类似的,如
select period, sum(amount) as sum from abc where type = '${type}'
<if test='period != null'>
and period = '${period}'
</if>
<if test='debtType != null'>
and debt_type = '${debtType}'
</if>
和 动态的参数,如:
{"type": "fruit", "period": "2021-01"}
参数是动态的,因为有可能是实时从页面上传过来的。
希望生成的sql是:
select period, sum(amount) as sum from abc where type = 'fruit' and period = '2021-01';
那么,如果想通过这样的模板sql,加上动态参数,来动态生成sql,应该怎么做呢?
(这里感谢聪明帅气的师兄:哲哥(elon_wen) 的帮助,查遍stackoverflow都没有方案,但是他却找到了方案!)
MyBatis提供了这样的方案:
public stati