//桥接对象,不对应数据库任何表
@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);
}