Mybatis Plus动态表名及动态参数(binlog同步等可用)

//桥接对象,不对应数据库任何表
@Data
public class SyncData implements Serializable {

    private String tableName;

    private List<SyncDataColumn> columns;

    //当前暂时不支持多个,后续可以考虑多个group by查询情况
    private Pair<String, String> group;


    @Data
    @AllArgsConstructor
    public static class SyncDataColumn{
        private String column;

        private String value;
    }
}

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface SyncDataMapper extends BaseMapper<SyncData> {

    @Insert({
            "<script>",
            "INSERT INTO ${tableName} ",
            "<foreach collection='columns' item='item' separator=',' open='(' close=')' index='index'> ",
            "${item.column}",
            " </foreach> ",
            " values ",
            "<foreach collection='columns' item='item' separator=',' open='(' close=')' index='index'> ",
            "<choose>",
            "  <when test='item.value != null'>",
            "'${item.value}'",
            "  </when>",
            "  <otherwise>",
            "null",
            "  </otherwise>",
            "</choose>",
            " </foreach> ",
            " ON DUPLICATE KEY UPDATE ",
            "<foreach collection='columns' item='item' separator=',' index='index'> ",
            "<choose>",
            "  <when test='item.value != null'>",
            "${item.column} = '${item.value}'",
            "  </when>",
            "  <otherwise>",
            "${item.column} = null",
            "  </otherwise>",
            "</choose>",
            " </foreach> ",
            "</script>"
    })
    int insert(SyncData data);

    @Update({
            "<script>",
            "update ${tableName} set ",
            "<foreach collection='columns' item='item' separator=',' index='index'> ",
            "<choose>",
            "  <when test='item.value != null'>",
            "${item.column} = '${item.value}'",
            "  </when>",
            "  <otherwise>",
            "${item.column} = null",
            "  </otherwise>",
            "</choose>",
            " </foreach> ",
            " where ",
            "${group.left} = '${group.right}'",
            "</script>"
    })
    int updateByGroup(SyncData data);

    @Select({
            "<script>",
            "select t.* from ${tableName} t where ",
            "<foreach collection='columns' item='item' separator=' and '  index='index'> ",
            " ${item.column} = '${item.value}' ",
            " </foreach> ",
            "</script>"
    })
    List<Map<String, Object>> select(SyncData data);

    @Select({
            "<script>",
            "select t.* from ${tableName} t where ",
            " ${group.key} = '${group.value}' ",
            "</script>"
    })
    List<Map<String, Object>> selectByGroup(SyncData data);


}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值