例如:
···
insert into TEST1(
<if test="base_id!=null and base_id!=''">
base_id,
</if>
<if test="name!=null and name!=''">
name,
</if>
<if test="age!=null and age!=''">
age,
</if>
<if test="address!=null and address!=''">
address,
</if>
<if test="year!=null and year!=''">
year
</if>
) VALUES (
<if test="base_id!=null and base_id!=''">
#{base_id},
</if>
<if test="name!=null and name!=''">
#{name},
</if>
<if test="age!=null and age!=''">
#{age},
</if>
<if test="address!=null and address!=''">
#{address},
</if>
<if test="year!=null and year!=''">
to_date(#{year},'yyyy-MM-dd HH24:mi:ss')
</if>
)
···
这样的代码可以用如下的方式在mybatis的Provider里面使用:
InsProvider.kt:
fun insertCommonByParamMap(paramMap: HashMap<String, *>): String {
return "<script>" + paramMap.get("sql").toString() + "</script>"
}
把SQL和参数全部放到Map里面,mybatis会自动解析出来
Mapper文件里如下方式调用:
@InsertProvider(type = InsProvider::class, method = "insertCommonByParamMap")
fun insertCommonByParamMap(paramMap: HashMap<String, *>): Int
java代码与kotlin相似,这里不再赘述