Ver código fonte

单表操作自动切换数据源。如果没有指定数据源,使用默认数据源

shuzheng 9 anos atrás
pai
commit
259a1780ac

+ 32 - 16
zheng-common/src/main/java/com/zheng/common/base/BaseServiceImpl.java

@@ -21,98 +21,105 @@ public abstract class BaseServiceImpl<Mapper, Record, Example> implements BaseSe
 	@Override
 	public int countByExample(Example example) {
 		try {
-			DynamicDataSource.setDataSource(DataSourceEnum.SLAVE);
+			DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
 			Method countByExample = mapper.getClass().getDeclaredMethod("countByExample", example.getClass());
 			Object result = countByExample.invoke(mapper, example);
 			return Integer.parseInt(String.valueOf(result));
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return 0;
 	}
 
 	@Override
 	public int deleteByExample(Example example) {
 		try {
-			DynamicDataSource.setDataSource(DataSourceEnum.MASTER);
+			DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
 			Method deleteByExample = mapper.getClass().getDeclaredMethod("deleteByExample", example.getClass());
 			Object result = deleteByExample.invoke(mapper, example);
 			return Integer.parseInt(String.valueOf(result));
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return 0;
 	}
 
 	@Override
 	public int deleteByPrimaryKey(Integer id) {
 		try {
-			DynamicDataSource.setDataSource(DataSourceEnum.MASTER);
+			DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
 			Method deleteByPrimaryKey = mapper.getClass().getDeclaredMethod("deleteByPrimaryKey", id.getClass());
 			Object result = deleteByPrimaryKey.invoke(mapper, id);
 			return Integer.parseInt(String.valueOf(result));
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return 0;
 	}
 
 	@Override
 	public int insert(Record record) {
 		try {
-			DynamicDataSource.setDataSource(DataSourceEnum.MASTER);
+			DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
 			Method insert = mapper.getClass().getDeclaredMethod("insert", record.getClass());
 			Object result = insert.invoke(mapper, record);
 			return Integer.parseInt(String.valueOf(result));
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return 0;
 	}
 
 	@Override
 	public int insertSelective(Record record) {
 		try {
-			DynamicDataSource.setDataSource(DataSourceEnum.MASTER);
+			DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
 			Method insertSelective = mapper.getClass().getDeclaredMethod("insertSelective", record.getClass());
 			Object result = insertSelective.invoke(mapper, record);
 			return Integer.parseInt(String.valueOf(result));
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return 0;
 	}
 
 	@Override
 	public List<Record> selectByExampleWithBLOBs(Example example) {
 		try {
-			DynamicDataSource.setDataSource(DataSourceEnum.SLAVE);
+			DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
 			Method selectByExampleWithBLOBs = mapper.getClass().getDeclaredMethod("selectByExampleWithBLOBs", example.getClass());
 			Object result = selectByExampleWithBLOBs.invoke(mapper, example);
 			return (List<Record>) result;
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return null;
 	}
 
 	@Override
 	public List<Record> selectByExample(Example example) {
 		try {
-			DynamicDataSource.setDataSource(DataSourceEnum.SLAVE);
+			DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
 			Method selectByExample = mapper.getClass().getDeclaredMethod("selectByExample", example.getClass());
 			Object result = selectByExample.invoke(mapper, example);
 			return (List<Record>) result;
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return null;
 	}
 
 	@Override
 	public Record selectFirstByExample(Example example) {
 		try {
-			DynamicDataSource.setDataSource(DataSourceEnum.SLAVE);
+			DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
 			Method selectByExample = mapper.getClass().getDeclaredMethod("selectByExample", example.getClass());
 			List<Record> result = (List<Record>) selectByExample.invoke(mapper, example);
 			if (null != result && result.size() > 0) {
@@ -121,97 +128,105 @@ public abstract class BaseServiceImpl<Mapper, Record, Example> implements BaseSe
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return null;
 	}
 
 	@Override
 	public Record selectByPrimaryKey(Integer id) {
 		try {
-			DynamicDataSource.setDataSource(DataSourceEnum.SLAVE);
+			DynamicDataSource.setDataSource(DataSourceEnum.SLAVE.getName());
 			Method selectByPrimaryKey = mapper.getClass().getDeclaredMethod("selectByPrimaryKey", id.getClass());
 			Object result = selectByPrimaryKey.invoke(mapper, id);
 			return (Record) result;
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return null;
 	}
 
 	@Override
 	public int updateByExampleSelective(@Param("record") Record record, @Param("example") Example example) {
 		try {
-			DynamicDataSource.setDataSource(DataSourceEnum.MASTER);
+			DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
 			Method updateByExampleSelective = mapper.getClass().getDeclaredMethod("updateByExampleSelective", record.getClass(), example.getClass());
 			Object result = updateByExampleSelective.invoke(mapper, record, example);
 			return Integer.parseInt(String.valueOf(result));
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return 0;
 	}
 
 	@Override
 	public int updateByExampleWithBLOBs(@Param("record") Record record, @Param("example") Example example) {
 		try {
-			DynamicDataSource.setDataSource(DataSourceEnum.MASTER);
+			DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
 			Method updateByExampleWithBLOBs = mapper.getClass().getDeclaredMethod("updateByExampleWithBLOBs", record.getClass(), example.getClass());
 			Object result = updateByExampleWithBLOBs.invoke(mapper, record, example);
 			return Integer.parseInt(String.valueOf(result));
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return 0;
 	}
 
 	@Override
 	public int updateByExample(@Param("record") Record record, @Param("example") Example example) {
 		try {
-			DynamicDataSource.setDataSource(DataSourceEnum.MASTER);
+			DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
 			Method updateByExample = mapper.getClass().getDeclaredMethod("updateByExample", record.getClass(), example.getClass());
 			Object result = updateByExample.invoke(mapper, record, example);
 			return Integer.parseInt(String.valueOf(result));
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return 0;
 	}
 
 	@Override
 	public int updateByPrimaryKeySelective(Record record) {
 		try {
-			DynamicDataSource.setDataSource(DataSourceEnum.MASTER);
+			DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
 			Method updateByPrimaryKeySelective = mapper.getClass().getDeclaredMethod("updateByPrimaryKeySelective", record.getClass());
 			Object result = updateByPrimaryKeySelective.invoke(mapper, record);
 			return Integer.parseInt(String.valueOf(result));
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return 0;
 	}
 
 	@Override
 	public int updateByPrimaryKeyWithBLOBs(Record record) {
 		try {
-			DynamicDataSource.setDataSource(DataSourceEnum.MASTER);
+			DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
 			Method updateByPrimaryKeyWithBLOBs = mapper.getClass().getDeclaredMethod("updateByPrimaryKeyWithBLOBs", record.getClass());
 			Object result = updateByPrimaryKeyWithBLOBs.invoke(mapper, record);
 			return Integer.parseInt(String.valueOf(result));
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return 0;
 	}
 
 	@Override
 	public int updateByPrimaryKey(Record record) {
 		try {
-			DynamicDataSource.setDataSource(DataSourceEnum.MASTER);
+			DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
 			Method updateByPrimaryKey = mapper.getClass().getDeclaredMethod("updateByPrimaryKey", record.getClass());
 			Object result = updateByPrimaryKey.invoke(mapper, record);
 			return Integer.parseInt(String.valueOf(result));
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return 0;
 	}
 
@@ -221,7 +236,7 @@ public abstract class BaseServiceImpl<Mapper, Record, Example> implements BaseSe
 			if (StringUtils.isBlank(ids)) {
 				return 0;
 			}
-			DynamicDataSource.setDataSource(DataSourceEnum.MASTER);
+			DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
 			String[] idArray = ids.split("-");
 			int count = 0;
 			for (String idStr : idArray) {
@@ -237,6 +252,7 @@ public abstract class BaseServiceImpl<Mapper, Record, Example> implements BaseSe
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
+		DynamicDataSource.clearDataSource();
 		return 0;
 	}
 

+ 42 - 3
zheng-common/src/main/java/com/zheng/common/db/DataSourceEnum.java

@@ -4,11 +4,50 @@ package com.zheng.common.db;
  * 多数据源枚举
  * Created by ZhangShuzheng on 2017/1/15.
  */
-public class DataSourceEnum {
+public enum DataSourceEnum {
 
 	// 主库
-	public static final String MASTER = "masterDataSource";
+	MASTER("masterDataSource", true),
 	// 从库
-	public static final String SLAVE = "slaveDataSource";
+	SLAVE("slaveDataSource", false),;
+
+	// 数据源名称
+	private String name;
+	// 是否是默认数据源
+	private boolean master;
+
+	DataSourceEnum(String name, boolean master) {
+		this.name = name;
+		this.master = master;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public boolean isMaster() {
+		return master;
+	}
+
+	public void setMaster(boolean master) {
+		this.master = master;
+	}
+
+	public String getDefault() {
+		String defaultDataSource = "";
+		for (DataSourceEnum dataSourceEnum : DataSourceEnum.values()) {
+			if (!"".equals(defaultDataSource)) {
+				break;
+			}
+			if (dataSourceEnum.master) {
+				defaultDataSource = dataSourceEnum.getName();
+			}
+		}
+		return defaultDataSource;
+	}
 
 }

+ 5 - 1
zheng-common/src/main/java/com/zheng/common/db/DynamicDataSource.java

@@ -21,7 +21,6 @@ public class DynamicDataSource extends AbstractRoutingDataSource {
 		return dataSource;
 	}
 
-
 	/**
 	 * 设置数据源
 	 * @param dataSource
@@ -35,6 +34,11 @@ public class DynamicDataSource extends AbstractRoutingDataSource {
 	 * @return
 	 */
 	public static String getDataSource() {
+		String dataSource = contextHolder.get();
+		// 如果没有指定数据源,使用默认数据源
+		if (null == dataSource) {
+			DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getDefault());
+		}
 		return contextHolder.get();
 	}