1.创建插件类
2.注解类,标志该类是否需要切换数据源
3.实体类使用注解标志
4.创建配置插件类
1.
package com.upmsproj.wp.common.plugin;
import com.upmsproj.wp.common.ChangeDataSource;
import com.upmsproj.wp.common.config.datasource.DynamicDataSourceContextHolder;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.springframework.beans.factory.annotation.Value;
import java.util.Properties;
@Intercepts({
@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}
)
})
public class ChangeDataSourceHandler implements Interceptor {
@Value("${customer.sysDataBase}")
private String sysDataBase;
private Boolean needChangeDataBase(Invocation invocation){
Object[] args = invocation.getArgs();
Class<?> entityClass = args[1].getClass(); //实体类对象
ChangeDataSource declaredAnnotation = entityClass.getDeclaredAnnotation(ChangeDataSource.class);
return "YES".equals(declaredAnnotation.value());
}
@Override
public Object intercept(Invocation invocation) throws Throwable {
if(needChangeDataBase(invocation)){
DynamicDataSourceContextHolder.setDataSourceRouterKey(sysDataBase);
}
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target,this);
}
@Override
public void setProperties(Properties properties) {
}
}
2.
package com.upmsproj.wp.common;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ChangeDataSource {
String value() default "NO" ;
}
3.
package com.upmsproj.wp.basicdata.entity;
import com.upmsproj.wp.common.ChangeDataSource;
import com.upmsproj.wp.common.config.datasource.DataBase;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonProperty;
@ApiModel
@Table(name = "dbo.PMS_MESSAGESET")
@ChangeDataSource("YES")
public class PMS_MESSAGESET {
@ApiModelProperty(value="主键ID")
@Id
@JsonProperty("kID")
@Column(name = "KID")
private String kID;
@ApiModelProperty(value="消息类型 生产准备 生产逾期 工序开始 工序结束 工序检查完成 问题发现 问题整改 任务完工 质检资料填报完成 质检资料填报逾期 移梁完成 出厂 安装完成 ")
@JsonProperty("messageType")
@Column(name = "MessageType")
private String messageType;
@ApiModelProperty(value="提前或延后提醒间隔时间(小时)")
@JsonProperty("remainHours")
@Column(name = "RemainHours")
private Integer remainHours;
@ApiModelProperty(value="RecieverType")
@JsonProperty("recieverType")
@Column(name = "RecieverType")
private String recieverType;
@ApiModelProperty(value="RecieverID")
@JsonProperty("recieverID")
@Column(name = "RecieverID")
private Integer recieverID;
@ApiModelProperty(value="RecieverName")
@JsonProperty("recieverName")
@Column(name = "RecieverName")
private String recieverName;
@ApiModelProperty(value="新增时间")
@JsonProperty("addDate")
@Column(name = "add_date")
private Date addDate;
@ApiModelProperty(value="新增用户ID")
@JsonProperty("addUserID")
@Column(name = "add_UserID")
private Integer addUserID;
@ApiModelProperty(value="新增用户名称")
@JsonProperty("addUserName")
@Column(name = "add_UserName")
private String addUserName;
@ApiModelProperty(value="新增单位ID")
@JsonProperty("addUnitID")
@Column(name = "add_UnitID")
private Integer addUnitID;
@ApiModelProperty(value="新增单位名称")
@JsonProperty("addUnitName")
@Column(name = "add_UnitName")
private String addUnitName;
@ApiModelProperty(value="新增项目部ID")
@JsonProperty("addBranchID")
@Column(name = "add_BranchID")
private Integer addBranchID;
@ApiModelProperty(value="新增项目部")
@JsonProperty("addBranchName")
@Column(name = "add_BranchName")
private String addBranchName;
@ApiModelProperty(value="新增部门ID")
@JsonProperty("addDepartID")
@Column(name = "add_DepartID")
private Integer addDepartID;
@ApiModelProperty(value="新增部门")
@JsonProperty("addDepartName")
@Column(name = "add_DepartName")
private String addDepartName;
@ApiModelProperty(value="是否删除")
@JsonProperty("isDel")
@Column(name = "IsDel")
private Integer isDel;
@ApiModelProperty(value="删除时间")
@JsonProperty("delDate")
@Column(name = "DelDate")
private Date delDate;
@ApiModelProperty(value="删除用户ID")
@JsonProperty("delUserID")
@Column(name = "Del_UserID")
private Integer delUserID;
@ApiModelProperty(value="删除用户名称")
@JsonProperty("delUserName")
@Column(name = "Del_UserName")
private String delUserName;
@ApiModelProperty(value="项目ID")
@JsonProperty("projId")
@Column(name = "proj_id")
private Integer projId;
@ApiModelProperty(value="暂停")
@JsonProperty("isStop")
@Column(name = "IsStop")
private Integer isStop;
@ApiModelProperty(value="报警值")
@JsonProperty("warnValue")
@Column(name = "WarnValue")
private Double warnValue;
@ApiModelProperty(value="标段ID")
@JsonProperty("pactId")
@Column(name = "pact_id")
private Integer pactId;
@ApiModelProperty(value="通知类型(0通知(不需处理),1预警(需处理))")
@JsonProperty("remindType")
@Column(name = "Remind_Type")
private Integer remindType;
@ApiModelProperty(value="消息等级Kid")
@JsonProperty("levelKid")
@Column(name = "levelKid")
private String levelKid;
@ApiModelProperty(value="AlterLevel")
@JsonProperty("alterLevel")
@Column(name = "AlterLevel")
private Integer alterLevel;
@ApiModelProperty(value="Type_ID")
@JsonProperty("typeID")
@Column(name = "Type_ID")
private Integer typeID;
@ApiModelProperty(value="Type_Name")
@JsonProperty("typeName")
@Column(name = "Type_Name")
private String typeName;
@ApiModelProperty(value="备注")
@JsonProperty("remark")
@Column(name = "remark")
private String remark;
@ApiModelProperty(value="样本数量")
@JsonProperty("sampleNum")
@Column(name = "sampleNum")
private Integer sampleNum;
@ApiModelProperty(value="最小值")
@JsonProperty("minValue")
@Column(name = "minValue")
private Double minValue;
@ApiModelProperty(value="最大值")
@JsonProperty("maxValue")
@Column(name = "maxValue")
private Double maxValue;
@ApiModelProperty(value="数据查看URL")
@JsonProperty("dataUrl")
@Column(name = "dataUrl")
private String dataUrl;
public String getKID() {
return kID;
}
public void setKID(String kID) {
this.kID = kID;
}
public String getMessageType() {
return messageType;
}
public void setMessageType(String messageType) {
this.messageType = messageType;
}
public Integer getRemainHours() {
return remainHours;
}
public void setRemainHours(Integer remainHours) {
this.remainHours = remainHours;
}
public String getRecieverType() {
return recieverType;
}
public void setRecieverType(String recieverType) {
this.recieverType = recieverType;
}
public Integer getRecieverID() {
return recieverID;
}
public void setRecieverID(Integer recieverID) {
this.recieverID = recieverID;
}
public String getRecieverName() {
return recieverName;
}
public void setRecieverName(String recieverName) {
this.recieverName = recieverName;
}
public Date getAddDate() {
return addDate;
}
public void setAddDate(Date addDate) {
this.addDate = addDate;
}
public Integer getAddUserID() {
return addUserID;
}
public void setAddUserID(Integer addUserID) {
this.addUserID = addUserID;
}
public String getAddUserName() {
return addUserName;
}
public void setAddUserName(String addUserName) {
this.addUserName = addUserName;
}
public Integer getAddUnitID() {
return addUnitID;
}
public void setAddUnitID(Integer addUnitID) {
this.addUnitID = addUnitID;
}
public String getAddUnitName() {
return addUnitName;
}
public void setAddUnitName(String addUnitName) {
this.addUnitName = addUnitName;
}
public Integer getAddBranchID() {
return addBranchID;
}
public void setAddBranchID(Integer addBranchID) {
this.addBranchID = addBranchID;
}
public String getAddBranchName() {
return addBranchName;
}
public void setAddBranchName(String addBranchName) {
this.addBranchName = addBranchName;
}
public Integer getAddDepartID() {
return addDepartID;
}
public void setAddDepartID(Integer addDepartID) {
this.addDepartID = addDepartID;
}
public String getAddDepartName() {
return addDepartName;
}
public void setAddDepartName(String addDepartName) {
this.addDepartName = addDepartName;
}
public Integer getIsDel() {
return isDel;
}
public void setIsDel(Integer isDel) {
this.isDel = isDel;
}
public Date getDelDate() {
return delDate;
}
public void setDelDate(Date delDate) {
this.delDate = delDate;
}
public Integer getDelUserID() {
return delUserID;
}
public void setDelUserID(Integer delUserID) {
this.delUserID = delUserID;
}
public String getDelUserName() {
return delUserName;
}
public void setDelUserName(String delUserName) {
this.delUserName = delUserName;
}
public Integer getProjId() {
return projId;
}
public void setProjId(Integer projId) {
this.projId = projId;
}
public Integer getIsStop() {
return isStop;
}
public void setIsStop(Integer isStop) {
this.isStop = isStop;
}
public Double getWarnValue() {
return warnValue;
}
public void setWarnValue(Double warnValue) {
this.warnValue = warnValue;
}
public Integer getPactId() {
return pactId;
}
public void setPactId(Integer pactId) {
this.pactId = pactId;
}
public Integer getRemindType() {
return remindType;
}
public void setRemindType(Integer remindType) {
this.remindType = remindType;
}
public String getLevelKid() {
return levelKid;
}
public void setLevelKid(String levelKid) {
this.levelKid = levelKid;
}
public Integer getAlterLevel() {
return alterLevel;
}
public void setAlterLevel(Integer alterLevel) {
this.alterLevel = alterLevel;
}
public Integer getTypeID() {
return typeID;
}
public void setTypeID(Integer typeID) {
this.typeID = typeID;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Integer getSampleNum() {
return sampleNum;
}
public void setSampleNum(Integer sampleNum) {
this.sampleNum = sampleNum;
}
public Double getMinValue() {
return minValue;
}
public void setMinValue(Double minValue) {
this.minValue = minValue;
}
public Double getMaxValue() {
return maxValue;
}
public void setMaxValue(Double maxValue) {
this.maxValue = maxValue;
}
public String getDataUrl() {
return dataUrl;
}
public void setDataUrl(String dataUrl) {
this.dataUrl = dataUrl;
}
}
4.
package com.upmsproj.wp.common.config;
import com.upmsproj.wp.common.plugin.ChangeDataSourceHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisConfig {
@Bean
ChangeDataSourceHandler changeDataSourceHandler(){
return new ChangeDataSourceHandler();
}
}