之前使用mybatis-plus框架,已经完成了整个项目基本代码的编写.
结果,现在突闻噩耗......
由于某种不可抗力的原因,要把数据同步到两个库里.
就是用户进来以后,调接口,比如新增xxx,我们这里一个库,对方那里一个库.都要有这个新增的数据.
两方各拿一份相同的数据.
好办啊,就再写一遍CRUD.
问题是 ,我们的主键叫id,对方表里面也有我们的所有字段,自然包含id,但他们自带了一个表主键,叫ep_id.
然鹅我们,并没有让对方更改主键的办法(因为他们所有的库表都是这个字段作为id,好像是规定的)
我们也不能更改自己的id(因为第三方从数据库里拿id作为数据,他们不拿ep_id)
这就有问题了,plus是用@TableId标识的,你现在只能标识一个主键,但实际是有两个.
会报错的,GG.
有办法吗,当然有,很多办法都可以解决.
我不想写两份实体,太麻烦了,写两份实体就要写两份mapper,两份service和两份impl
所以我采用的是sql拦截来更改,xdm有更好的办法,可以在评论区告诉我
import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.util.Properties;
/**
* mybatis-plus 自定义sql拦截器
*
* @author ml
*/
@Slf4j
@Component
//拦截StatementHandler类中参数类型为Statement的prepare方法(prepare=在预编译SQL前加入修改的逻辑)
@Intercepts({
@Signature(type = StatementHandler.class, method = "prepare", args = {
Connection.class, Integer.class})})
public class Interceptor implements org.apache.ibatis.plugin.Interceptor {
/**
* 拦截sql
*
* @param invocation
*/
@Override
public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
// 通过MetaObject优雅访问对象的属性,这里是访问statementHandler的属性;:MetaObject是Mybatis提供的一个用于方便、
// 优雅访问对象属性的对象,通过它可以简化代码、不需要try/catch各种reflect异常,同时它支持对JavaBean、Collection、Map三种类型对象的操作。
//实际执行的sql是经过层层封装,无法利用简单的一层反射获取到需要使用提供的快捷方法或者对获取到关键数据进行拼装
MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY,
new DefaultReflectorFactory());
// 先拦截到RoutingStatementHandler,里面有个StatementHandler类型的delegate变量,其实现类是BaseStatementHandler,然后就到BaseStatementHandler的成员变量mappedStatement
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
// id为执行的mapper方法的全路径名,如com.cq.UserMapper.insertUser, 便于后续使用反射
String id = mappedStatement.getId();
// sql语句类型 select、delete、insert、update
String sqlCommandType = mappedStatement.getSqlCommandType().toString();
// 数据库连接信息
BoundSql boundSql = statementHandler.getBoundSql();
// 获取到原始sql语句
String sql = boundSql.getSql();
// 根据条件修改及增强sql
if ("INSERT".equals(sqlCommandType) && sql.contains("ep_id")) {
Object parameterObject = boundSql.getParameterObject();
BaseEntity baseEntity = BeanUtil.copyProperties(parameterObject, BaseEntity.class);
Integer id1 = baseEntity.getId();
sql = sql.replace(") VALUES (", ",id) VALUES (");
sql = sql.substring(0, sql.lastIndexOf(")"));
sql = sql + "," + id1 + ")";
}
//通过反射修改sql语句
Field field = boundSql.getClass().getDeclaredField("sql");
field.setAccessible(true);
field.set(boundSql, sql);
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return org.apache.ibatis.plugin.Interceptor.super.plugin(target);
}
@Override
public void setProperties(Properties properties) {
org.apache.ibatis.plugin.Interceptor.super.setProperties(properties);
}
}
我拦截了新增时候且有ep_id的sql,将id和对应的值塞进去.
然后就可以这么写了,要写的地方瞬间少了一大半.
//自己的表新增
shipService.saveOrUpdate(ship1);
//新增以后,plus框架会把id插进来,对另一个id赋值
ship1.setEpDefaultRowId(ship1.getId());
//plus调用,这个会走拦截,更改sql,上面哪个不会,因为上面epId为null
shipService_b.saveOrUpdate(ship1, new LambdaUpdateWrapper<Ship>().eq(Ship::getEpDefaultRowId, ship1.getId()));
这里只用写两个impl就行了,公用一个实体和mappery
完工