原则坚决不改老项目的依赖和架构
将map驼峰转下划线,hutool的StrUtil
//将参数map驼峰转蛇形命名
String id = mappedStatement.getId();
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
Object parameterObject = statementHandler.getParameterHandler().getParameterObject();
if (id.contains("dataintell") && parameterObject instanceof Map) {
Map<String, Object> map = (Map<String, Object>) ((Map) parameterObject).get("map");
BoundSql boundSql = statementHandler.getBoundSql();
String sql = boundSql.getSql();
for (String k : map.keySet()) {
sql = sql.replaceAll(k, StrUtil.toUnderlineCase(k).toUpperCase());
}
//通过反射修改sql语句
Field field = boundSql.getClass().getDeclaredField("sql");
field.setAccessible(true);
field.set(boundSql, sql);
}
Mybatis动态SQL
<insert id="insert">
insert into
${tableName}
<foreach collection="map.keys" separator="," open="(" close=")" item="item" index="no">
${item}
</foreach>
values
<foreach collection="map.keys" separator="," open="(" close=")" item="item" index="no">
#{map[${item}]}
</foreach>
</insert>
<update id="update">
update
${tableName}
set
<foreach collection="map.keys" separator="," open="(" close=")" item="item" index="no">
${item}=#{map[${item}]}
</foreach>
where ID=#{map["id"]}
</update>
<delete id="deleteBatch">
delete from
${tableName}
where ID in
<foreach collection="idList" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</delete>
<select id="getOneById" resultType="java.util.Map">
select *
from ${tableName}
where ID = #{id}
</select>
<select id="findTotal" resultType="java.lang.Integer">
select count(*)
from ${tableName}
</select>
<select id="list" resultType="java.util.Map">
select * from ${tableName} where 1=1
<if test="sql!=null and sql!=null">
and ${sql}
</if>
</select>
推荐使用的方式
<insert id="insert">
insert into
<include refid="tableName"/>
<foreach collection="map" separator="," open="(" close=")" item="item" index="key">
${key}
</foreach>
values
<foreach collection="map" separator="," open="(" close=")" item="value" index="key">
#{value}
</foreach>
</insert>
<update id="update">
update
<include refid="tableName"/>
set
<foreach collection="map" separator="," item="value" index="key">
${key}=#{value}
</foreach>
where ID=#{map.id}
</update>
tableName想使用MybatisPlus注解的方式,待研究