yonge 4 jaren geleden
bovenliggende
commit
942775d3ec
21 gewijzigde bestanden met toevoegingen van 476 en 215 verwijderingen
  1. 0 20
      edu-auth/edu-auth-api/src/main/java/com/keao/edu/auth/api/entity/LoginEntity.java
  2. 3 3
      edu-auth/edu-auth-server/src/main/java/com/keao/edu/auth/AuthServerApplication.java
  3. 0 1
      edu-auth/edu-auth-server/src/main/java/com/keao/edu/auth/config/WebSecurityConfig.java
  4. 1 30
      edu-auth/edu-auth-server/src/main/java/com/keao/edu/auth/core/filter/PhoneLoginAuthenticationFilter.java
  5. 0 6
      edu-auth/edu-auth-server/src/main/java/com/keao/edu/auth/core/provider/PhoneAuthenticationProvider.java
  6. 0 17
      edu-auth/edu-auth-server/src/main/java/com/keao/edu/auth/service/SysUserService.java
  7. 0 61
      edu-auth/edu-auth-server/src/main/java/com/keao/edu/auth/service/impl/SysUserServiceImpl.java
  8. 2 4
      edu-cms/src/main/java/com/keao/edu/cms/CmsServerApplication.java
  9. 0 22
      edu-common/src/main/java/com/keao/edu/common/filters/TenantIdThreadLocal.java
  10. 35 0
      edu-datasource/pom.xml
  11. 44 0
      edu-datasource/src/main/java/com/keao/edu/datasource/DataSourceContextHolder.java
  12. 22 0
      edu-datasource/src/main/java/com/keao/edu/datasource/DynamicRoutingDataSource.java
  13. 61 0
      edu-datasource/src/main/java/com/keao/edu/datasource/MybatisConfig.java
  14. 9 0
      edu-datasource/src/main/java/com/keao/edu/datasource/dal/dao/DatabaseSourceDao.java
  15. 125 0
      edu-datasource/src/main/java/com/keao/edu/datasource/dal/entity/DatabaseSource.java
  16. 8 0
      edu-datasource/src/main/java/com/keao/edu/datasource/service/DatabaseSourceService.java
  17. 68 0
      edu-datasource/src/main/java/com/keao/edu/datasource/service/impl/DatabaseSourceServiceImpl.java
  18. 97 0
      edu-datasource/src/main/resources/config/mybatis/DatabaseSourceMapper.xml
  19. 0 13
      edu-thirdparty/src/main/java/com/keao/edu/App.java
  20. 0 38
      edu-thirdparty/src/test/java/com/keao/edu/AppTest.java
  21. 1 0
      pom.xml

+ 0 - 20
edu-auth/edu-auth-api/src/main/java/com/keao/edu/auth/api/entity/LoginEntity.java

@@ -3,24 +3,12 @@ package com.keao.edu.auth.api.entity;
 public class LoginEntity {
     private String smsCode;
 
-    private String isLessee;
-
     private String clientId;
 
     private String clientSecret;
 
-    private String isRegister;
-
     private String phone;
 
-    public String getIsRegister() {
-        return isRegister;
-    }
-
-    public void setIsRegister(String isRegister) {
-        this.isRegister = isRegister;
-    }
-
     public String getSmsCode() {
         return smsCode;
     }
@@ -29,14 +17,6 @@ public class LoginEntity {
         this.smsCode = smsCode;
     }
 
-    public String getIsLessee() {
-        return isLessee;
-    }
-
-    public void setIsLessee(String isLessee) {
-        this.isLessee = isLessee;
-    }
-
     public String getClientId() {
         return clientId;
     }

+ 3 - 3
edu-auth/edu-auth-server/src/main/java/com/keao/edu/auth/UserApplication.java → edu-auth/edu-auth-server/src/main/java/com/keao/edu/auth/AuthServerApplication.java

@@ -17,15 +17,15 @@ import com.spring4all.swagger.EnableSwagger2Doc;
 @SpringBootApplication
 @EnableDiscoveryClient
 @EnableFeignClients({"com.ym.mec"})
-@MapperScan("com.keao.edu.user.dal.dao")
+@MapperScan("com.keao.edu.*.dal.dao")
 @ComponentScan(basePackages="com.keao.edu")
 @Configuration
 @EnableSwagger2Doc
 @EnableAsync
-public class UserApplication {
+public class AuthServerApplication {
 
 	public static void main(String[] args) {
-		SpringApplication.run(UserApplication.class, args);
+		SpringApplication.run(AuthServerApplication.class, args);
 	}
 	
 	@Bean

+ 0 - 1
edu-auth/edu-auth-server/src/main/java/com/keao/edu/auth/config/WebSecurityConfig.java

@@ -125,7 +125,6 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
 		filter.setAuthenticationManager(authenticationManagerBean());
 		filter.setAuthenticationSuccessHandler(successEventHandler);
 		filter.setAuthenticationFailureHandler(failureEvenHandler);
-		filter.setSysUserService(sysUserService);
 		return filter;
 	}
 

+ 1 - 30
edu-auth/edu-auth-server/src/main/java/com/keao/edu/auth/core/filter/PhoneLoginAuthenticationFilter.java

@@ -7,19 +7,15 @@ import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
-import org.apache.commons.lang3.StringUtils;
 import org.springframework.security.authentication.AbstractAuthenticationToken;
 import org.springframework.security.authentication.AuthenticationServiceException;
-import org.springframework.security.authentication.LockedException;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.AuthenticationException;
 import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
 import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
 
-import com.keao.edu.auth.api.dto.SysUserInfo;
 import com.keao.edu.auth.api.entity.LoginEntity;
 import com.keao.edu.auth.config.token.PhoneAuthenticationToken;
-import com.keao.edu.auth.service.SysUserService;
 import com.keao.edu.common.security.SecurityConstants;
 
 public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
@@ -27,14 +23,10 @@ public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProces
 	private static final String SPRING_SECURITY_RESTFUL_PHONE_KEY = "phone";
 	private static final String SPRING_SECURITY_RESTFUL_VERIFY_CODE_KEY = "smsCode";
 	private static final String clientIdParameter = "clientId";
-	private static final String IS_REGISTER_PARAMETER = "register";
-	private static final String IS_LESSEE = "isLessee";
 
 	private static final String SPRING_SECURITY_RESTFUL_LOGIN_URL = "/smsLogin";
 	private boolean postOnly = true;
 
-	private SysUserService sysUserService;
-
 	public PhoneLoginAuthenticationFilter() {
 		super(new AntPathRequestMatcher(SPRING_SECURITY_RESTFUL_LOGIN_URL, "POST"));
 	}
@@ -49,32 +41,15 @@ public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProces
 		// 手机验证码登陆
 		String principal = obtainParameter(request, SPRING_SECURITY_RESTFUL_PHONE_KEY);
 		String credentials = obtainParameter(request, SPRING_SECURITY_RESTFUL_VERIFY_CODE_KEY);
-		String isRegister = obtainParameter(request, IS_REGISTER_PARAMETER);
-		// 是否是租户
-		String isLessee = obtainParameter(request, IS_LESSEE);
 
 		String clientId = request.getParameter(clientIdParameter).toUpperCase();
-
-//		SysUserInfo userInfo = sysUserService.queryUserInfoByPhone(principal);
-		SysUserInfo userInfo = sysUserService.queryUserInfoByPhoneAndClient(principal,clientId);
-		if (userInfo != null && StringUtils.isNotEmpty(isLessee)) {
-			throw new LockedException("用户已存在");
-		}
-		if ("EDUCATION".equals(clientId)) {
-			clientId = "SYSTEM";
-		}
-		if (userInfo != null && !userInfo.getSysUser().getUserType().contains(clientId)) {
-			throw new LockedException("用户不存在,请联系教务老师");
-		}
-
+		
 		principal = principal.trim();
 
 		LoginEntity loginEntity = new LoginEntity();
 		loginEntity.setClientId(clientId);
-		loginEntity.setIsLessee(isLessee);
 		loginEntity.setPhone(principal);
 		loginEntity.setSmsCode(credentials);
-		loginEntity.setIsRegister(isRegister);
 
 		authRequest = new PhoneAuthenticationToken(SecurityConstants.PHONE_PRINCIPAL_PREFIX + principal, loginEntity);
 
@@ -99,8 +74,4 @@ public class PhoneLoginAuthenticationFilter extends AbstractAuthenticationProces
 		return result == null ? "" : result;
 	}
 
-	public void setSysUserService(SysUserService sysUserService) {
-		this.sysUserService = sysUserService;
-	}
-
 }

+ 0 - 6
edu-auth/edu-auth-server/src/main/java/com/keao/edu/auth/core/provider/PhoneAuthenticationProvider.java

@@ -10,7 +10,6 @@ import org.springframework.security.core.userdetails.UserDetailsService;
 import org.springframework.security.core.userdetails.UsernameNotFoundException;
 import org.springframework.transaction.annotation.Transactional;
 
-import com.keao.edu.auth.api.dto.SysUserInfo;
 import com.keao.edu.auth.api.entity.LoginEntity;
 import com.keao.edu.auth.config.token.PhoneAuthenticationToken;
 import com.keao.edu.auth.service.SysUserService;
@@ -50,11 +49,6 @@ public class PhoneAuthenticationProvider extends AbstractAuthenticationProvider
 			throw new BadCredentialsException("验证码校验失败");
 		}
 
-		SysUserInfo userInfo = sysUserService.queryUserInfoByPhone(loginEntity.getPhone());
-		if (userInfo == null && StringUtils.isNotEmpty(loginEntity.getIsLessee())) {
-			sysUserService.initUser(loginEntity.getPhone(), loginEntity.getClientId(), loginEntity.getIsRegister(), loginEntity.getIsLessee());
-		}
-
 		UserDetails loadedUser;
 		try {
 			loadedUser = userDetailsService.loadUserByUsername(phone);

+ 0 - 17
edu-auth/edu-auth-server/src/main/java/com/keao/edu/auth/service/SysUserService.java

@@ -1,7 +1,5 @@
 package com.keao.edu.auth.service;
 
-import org.springframework.security.core.Authentication;
-
 import com.keao.edu.auth.api.dto.SysUserInfo;
 import com.keao.edu.auth.api.entity.SysUser;
 import com.keao.edu.common.service.BaseService;
@@ -93,21 +91,6 @@ public interface SysUserService extends BaseService<Integer, SysUser> {
 	SysUser queryUserInfo(Integer userId);
 
 	/**
-	 * 上线时初始化用户数据
-	 * @param phone
-	 * @param clientId
-	 * @return
-	 */
-	SysUserInfo initUser(String phone,String clientId,String isRegister,String isLessee);
-
-	/**
-	 * 获取老师的分部编号
-	 * @param phone
-	 * @return
-	 */
-	void retrieveUser(String phone, Authentication authentication);
-
-	/**
 	 * 刷新token
 	 * @param sysUser
 	 */

+ 0 - 61
edu-auth/edu-auth-server/src/main/java/com/keao/edu/auth/service/impl/SysUserServiceImpl.java

@@ -3,18 +3,12 @@ package com.keao.edu.auth.service.impl;
 import java.util.Date;
 import java.util.List;
 
-import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
-import org.springframework.security.authentication.BadCredentialsException;
-import org.springframework.security.authentication.LockedException;
-import org.springframework.security.core.Authentication;
-import org.springframework.security.core.userdetails.UsernameNotFoundException;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import com.keao.edu.auth.api.dto.SysUserInfo;
-import com.keao.edu.auth.api.entity.LoginEntity;
 import com.keao.edu.auth.api.entity.SysUser;
 import com.keao.edu.auth.dal.dao.SysUserDao;
 import com.keao.edu.auth.service.SysRoleMenuService;
@@ -152,61 +146,6 @@ public class SysUserServiceImpl extends BaseServiceImpl<Integer, SysUser> implem
 
 	@Override
 	@Transactional(rollbackFor = Exception.class)
-	public SysUserInfo initUser(String phone, String clientId,String isRegister,String isLessee) {
-		if("register".equals(isRegister)){
-			throw new UsernameNotFoundException("404.9");
-		}
-		if(StringUtils.isNotEmpty(isLessee) || autoRegister){
-			if(StringUtils.isNotEmpty(isLessee) && StringUtils.equalsIgnoreCase(clientId,"TEACHER")){
-				SysUser sysUser = new SysUser();
-				sysUser.setPhone(phone);
-				sysUser.setUserType("TEACHER");
-				sysUserDao.insert(sysUser);
-				//添加用户现金账户
-				imFeignService.register(new ImUserModel(sysUser.getId().toString(),phone,null));
-				//sysTenantAccount
-				sysUserDao.insertSysTenantAccount(sysUser.getId());
-				//创建teacher表
-				sysUserDao.insertTeacher(sysUser.getId(),lesseeOrganId);
-				return queryUserInfoByPhone(phone);
-			}else if(StringUtils.isNotEmpty(isLessee) && StringUtils.equalsIgnoreCase(clientId,"STUDENT")){
-				SysUser sysUser = new SysUser();
-				sysUser.setPhone(phone);
-				sysUser.setUserType("STUDENT");
-				sysUser.setOrganId(lesseeOrganId);
-				sysUserDao.insert(sysUser);
-				sysUserService.saveStudent(sysUser.getId());
-				//添加用户现金账户
-				imFeignService.register(new ImUserModel(sysUser.getId().toString(),phone,null));
-				return queryUserInfoByPhone(phone);
-			}
-		}
-		throw new UsernameNotFoundException("404.9");
-	}
-
-	@Override
-	@Transactional(rollbackFor = Exception.class)
-	public void retrieveUser(String phone, Authentication authentication) {
-		LoginEntity loginEntity = (LoginEntity) authentication.getCredentials();
-		SysUserInfo userInfo = sysUserService.queryUserInfoByPhone(loginEntity.getPhone());
-		if (userInfo == null) {
-			userInfo = sysUserService.initUser(loginEntity.getPhone(),loginEntity.getClientId(),loginEntity.getIsRegister(),loginEntity.getIsLessee());
-		}else if(StringUtils.isNotEmpty(loginEntity.getIsLessee())){
-			throw new LockedException("用户已存在");
-		}
-		if("EDUCATION".equals(loginEntity.getClientId())){
-			loginEntity.setClientId("SYSTEM");
-		}
-		if (!userInfo.getSysUser().getUserType().contains(loginEntity.getClientId())) {
-			throw new LockedException("用户不存在,请联系教务老师");
-		}
-//			 验证码验证
-		boolean b = smsCodeService.verifyValidCode(loginEntity.getPhone(), loginEntity.getSmsCode());
-		if(!b) throw new BadCredentialsException("验证码校验失败");
-	}
-
-	@Override
-	@Transactional(rollbackFor = Exception.class)
 	public void refreshImToken(SysUser sysUser) {
 		sysUserDao.refreshImToken(sysUser.getId(),sysUser.getImToken());
 	}

+ 2 - 4
edu-cms/src/main/java/com/keao/edu/cms/CmsServerApplication.java

@@ -1,6 +1,5 @@
 package com.keao.edu.cms;
 
-import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@@ -15,9 +14,8 @@ import com.spring4all.swagger.EnableSwagger2Doc;
 
 @SpringBootApplication
 @EnableDiscoveryClient
-@EnableFeignClients("com.ym.mec")
-@MapperScan("com.ym.mec.cms.dal.dao")
-@ComponentScan(basePackages = { "com.ym.mec.cms", "com.ym.mec.im", "com.ym.mec.auth", "com.ym.mec.common.config", "com.ym.mec.common.security" })
+@EnableFeignClients("com.keao.edu")
+@ComponentScan(basePackages = { "com.keao.edu.cms", "com.keao.edu.im", "com.keao.edu.auth", "com.keao.edu.common.config", "com.keao.edu.common.security" })
 @Configuration
 @EnableSwagger2Doc
 public class CmsServerApplication {

+ 0 - 22
edu-common/src/main/java/com/keao/edu/common/filters/TenantIdThreadLocal.java

@@ -1,22 +0,0 @@
-package com.keao.edu.common.filters;
-
-public class TenantIdThreadLocal {
-    private static final ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>() {
-        @Override
-        protected Integer initialValue() {
-            return null;
-        }
-    };
-
-    public static Integer get(){
-        return threadLocal.get();
-    }
-
-    public static void set(Integer tenantId){
-        threadLocal.set(tenantId);
-    }
-
-    public static void remove(){
-        threadLocal.remove();
-    }
-}

+ 35 - 0
edu-datasource/pom.xml

@@ -0,0 +1,35 @@
+<?xml version="1.0"?>
+<project
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
+	xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+	<modelVersion>4.0.0</modelVersion>
+	<parent>
+		<groupId>com.keao.edu</groupId>
+		<artifactId>edu-saas</artifactId>
+		<version>1.0</version>
+	</parent>
+	<artifactId>edu-datasource</artifactId>
+	<name>edu-datasource</name>
+	<url>http://maven.apache.org</url>
+
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+	</properties>
+
+	<dependencies>
+		<dependency>
+			<groupId>com.keao.edu</groupId>
+			<artifactId>edu-common</artifactId>
+		</dependency>
+
+		<dependency>
+			<groupId>org.mybatis.spring.boot</groupId>
+			<artifactId>mybatis-spring-boot-starter</artifactId>
+		</dependency>
+
+		<dependency>
+			<groupId>com.alibaba</groupId>
+			<artifactId>druid-spring-boot-starter</artifactId>
+		</dependency>
+	</dependencies>
+</project>

+ 44 - 0
edu-datasource/src/main/java/com/keao/edu/datasource/DataSourceContextHolder.java

@@ -0,0 +1,44 @@
+package com.keao.edu.datasource;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+public class DataSourceContextHolder {
+
+	private static final ThreadLocal<Object> contextHolder = new ThreadLocal<Object>() {
+		@Override
+		protected String initialValue() {
+			return "master";
+		}
+	};
+
+	public static List<Object> dataSourceKeys = new ArrayList<Object>();
+
+	public static void setDataSourceKey(String key) {
+		contextHolder.set(key);
+	}
+
+	/**
+	 * 获取数据源
+	 * @return
+	 */
+	public static Object getDataSourceKey() {
+		return contextHolder.get();
+	}
+
+	/**
+	 * 重置数据源
+	 */
+	public static void clearDataSourceKey() {
+		contextHolder.remove();
+	}
+
+	public static boolean containDataSourceKey(Object key) {
+		return dataSourceKeys.contains(key);
+	}
+
+	public static boolean addDataSourceKeys(Collection<? extends Object> keys) {
+		return dataSourceKeys.addAll(keys);
+	}
+}

+ 22 - 0
edu-datasource/src/main/java/com/keao/edu/datasource/DynamicRoutingDataSource.java

@@ -0,0 +1,22 @@
+package com.keao.edu.datasource;
+
+import java.util.Map;
+
+import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
+
+public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
+
+	@Override
+	protected Object determineCurrentLookupKey() {
+		return DataSourceContextHolder.getDataSourceKey();
+	}
+
+	@Override
+	public void setTargetDataSources(Map<Object, Object> targetDataSources) {
+		super.setTargetDataSources(targetDataSources);
+		
+		// 将数据源的 key 放到数据源上下文的 key 集合中
+		DataSourceContextHolder.addDataSourceKeys(targetDataSources.keySet());
+	}
+
+}

+ 61 - 0
edu-datasource/src/main/java/com/keao/edu/datasource/MybatisConfig.java

@@ -0,0 +1,61 @@
+package com.keao.edu.datasource;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.sql.DataSource;
+
+import org.mybatis.spring.SqlSessionFactoryBean;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Primary;
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.transaction.PlatformTransactionManager;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
+
+import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
+
+@EnableTransactionManagement
+@Configuration
+@MapperScan("com.keao.edu.*.dal.dao")
+public class MybatisConfig {
+
+	@Bean("master")
+	@Primary
+	@ConfigurationProperties(prefix = "spring.datasource") // application.yml中对应属性的前缀
+	public DataSource master() {
+		return DruidDataSourceBuilder.create().build();
+	}
+
+	@Bean("dynamicDataSource")
+	public DataSource dynamicDataSource() {
+		DynamicRoutingDataSource dynamicDataSource = new DynamicRoutingDataSource();
+		Map<Object, Object> dataSourceMap = new HashMap<>();
+		dataSourceMap.put("master", master());
+		// 将 master 数据源作为默认指定的数据源
+		dynamicDataSource.setDefaultTargetDataSource(master());
+		dynamicDataSource.setTargetDataSources(dataSourceMap);
+		return dynamicDataSource;
+	}
+
+	@Bean
+	public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
+		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
+		// 配置动态数据源,如果没有将 dynamicDataSource 作为数据源则不能实现切换
+		sqlSessionFactoryBean.setDataSource(dynamicDataSource());
+		sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:config/mybatis/*.xml"));
+		sqlSessionFactoryBean.setTypeAliasesPackage("com.keao.edu.*.dal.entity");
+
+		return sqlSessionFactoryBean;
+	}
+
+	@Bean
+	public PlatformTransactionManager transactionManager() {
+		// 配置事务管理, 使用事务时在方法头部添加@Transactional注解即可
+		return new DataSourceTransactionManager(dynamicDataSource());
+	}
+
+}

+ 9 - 0
edu-datasource/src/main/java/com/keao/edu/datasource/dal/dao/DatabaseSourceDao.java

@@ -0,0 +1,9 @@
+package com.keao.edu.datasource.dal.dao;
+
+import com.keao.edu.common.dal.BaseDAO;
+import com.keao.edu.datasource.dal.entity.DatabaseSource;
+
+public interface DatabaseSourceDao extends BaseDAO<Integer, DatabaseSource> {
+
+	
+}

+ 125 - 0
edu-datasource/src/main/java/com/keao/edu/datasource/dal/entity/DatabaseSource.java

@@ -0,0 +1,125 @@
+package com.keao.edu.datasource.dal.entity;
+
+import org.apache.commons.lang3.builder.ToStringBuilder;
+
+/**
+ * 对应数据库表(database_source):
+ */
+public class DatabaseSource {
+
+	/**  */
+	private Integer id;
+	
+	/** 租户ID */
+	private String tenantId;
+	
+	/** 驱动 */
+	private String driver;
+	
+	/** 连接信息 */
+	private String url;
+	
+	/** 用户名 */
+	private String username;
+	
+	/** 密码 */
+	private String password;
+	
+	/** 数据库类型 */
+	private String type;
+	
+	/**  */
+	private String memo;
+	
+	/**  */
+	private java.util.Date createTime;
+	
+	/**  */
+	private java.util.Date updateTime;
+	
+	public void setId(Integer id){
+		this.id = id;
+	}
+	
+	public Integer getId(){
+		return this.id;
+	}
+			
+	public void setTenantId(String tenantId){
+		this.tenantId = tenantId;
+	}
+	
+	public String getTenantId(){
+		return this.tenantId;
+	}
+			
+	public void setDriver(String driver){
+		this.driver = driver;
+	}
+	
+	public String getDriver(){
+		return this.driver;
+	}
+			
+	public void setUrl(String url){
+		this.url = url;
+	}
+	
+	public String getUrl(){
+		return this.url;
+	}
+			
+	public void setUsername(String username){
+		this.username = username;
+	}
+	
+	public String getUsername(){
+		return this.username;
+	}
+			
+	public void setPassword(String password){
+		this.password = password;
+	}
+	
+	public String getPassword(){
+		return this.password;
+	}
+			
+	public void setType(String type){
+		this.type = type;
+	}
+	
+	public String getType(){
+		return this.type;
+	}
+			
+	public void setMemo(String memo){
+		this.memo = memo;
+	}
+	
+	public String getMemo(){
+		return this.memo;
+	}
+			
+	public void setCreateTime(java.util.Date createTime){
+		this.createTime = createTime;
+	}
+	
+	public java.util.Date getCreateTime(){
+		return this.createTime;
+	}
+			
+	public void setUpdateTime(java.util.Date updateTime){
+		this.updateTime = updateTime;
+	}
+	
+	public java.util.Date getUpdateTime(){
+		return this.updateTime;
+	}
+			
+	@Override
+	public String toString() {
+		return ToStringBuilder.reflectionToString(this);
+	}
+
+}

+ 8 - 0
edu-datasource/src/main/java/com/keao/edu/datasource/service/DatabaseSourceService.java

@@ -0,0 +1,8 @@
+package com.keao.edu.datasource.service;
+
+import com.keao.edu.common.service.BaseService;
+import com.keao.edu.datasource.dal.entity.DatabaseSource;
+
+public interface DatabaseSourceService extends BaseService<Integer, DatabaseSource> {
+
+}

+ 68 - 0
edu-datasource/src/main/java/com/keao/edu/datasource/service/impl/DatabaseSourceServiceImpl.java

@@ -0,0 +1,68 @@
+package com.keao.edu.datasource.service.impl;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.springframework.beans.BeansException;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.stereotype.Service;
+
+import com.alibaba.druid.pool.DruidDataSource;
+import com.keao.edu.common.dal.BaseDAO;
+import com.keao.edu.common.service.impl.BaseServiceImpl;
+import com.keao.edu.datasource.DynamicRoutingDataSource;
+import com.keao.edu.datasource.dal.dao.DatabaseSourceDao;
+import com.keao.edu.datasource.dal.entity.DatabaseSource;
+import com.keao.edu.datasource.service.DatabaseSourceService;
+
+@Service
+public class DatabaseSourceServiceImpl extends BaseServiceImpl<Integer, DatabaseSource> implements DatabaseSourceService, ApplicationContextAware,
+		InitializingBean {
+
+	@Autowired
+	private DatabaseSourceDao databaseSourceDao;
+
+	private ApplicationContext applicationContext;
+
+	@Override
+	public BaseDAO<Integer, DatabaseSource> getDAO() {
+		return databaseSourceDao;
+	}
+
+	@Override
+	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+		this.applicationContext = applicationContext;
+	}
+
+	@Override
+	public void afterPropertiesSet() throws Exception {
+
+		DynamicRoutingDataSource dynamicDataSource = (DynamicRoutingDataSource) applicationContext.getBean("dynamicDataSource");
+		DruidDataSource master = (DruidDataSource) applicationContext.getBean("master");
+		Map<Object, Object> dataSourceMap = new HashMap<>();
+		dataSourceMap.put("master", master);
+
+		List<DatabaseSource> dbSourceList = databaseSourceDao.findAll(null);
+		for (DatabaseSource ds : dbSourceList) {
+			DruidDataSource dataSource = new DruidDataSource();
+
+			dataSource.setDriverClassName(ds.getDriver());
+			dataSource.setUrl(ds.getUrl());
+			dataSource.setUsername(ds.getUsername());
+			dataSource.setPassword(ds.getPassword());
+			dataSource.setConnectProperties(master.getConnectProperties());
+
+			dataSourceMap.put(ds.getTenantId(), dataSource);
+		}
+
+		dynamicDataSource.setTargetDataSources(dataSourceMap);
+
+		// 必须执行此操作,才会重新初始化AbstractRoutingDataSource 中的 resolvedDataSources,也只有这样,动态切换才会起效
+		dynamicDataSource.afterPropertiesSet();
+	}
+
+}

+ 97 - 0
edu-datasource/src/main/resources/config/mybatis/DatabaseSourceMapper.xml

@@ -0,0 +1,97 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<!-- 这个文件是自动生成的。 不要修改此文件。所有改动将在下次重新自动生成时丢失。 -->
+<mapper namespace="com.keao.edu.datasource.dal.dao.DatabaseSourceDao">
+
+	<resultMap type="com.keao.edu.datasource.dal.entity.DatabaseSource"
+		id="DatabaseSource">
+		<result column="id_" property="id" />
+		<result column="tenant_id_" property="tenantId" />
+		<result column="driver_" property="driver" />
+		<result column="url_" property="url" />
+		<result column="username_" property="username" />
+		<result column="password_" property="password" />
+		<result column="type_" property="type" />
+		<result column="memo_" property="memo" />
+		<result column="create_time_" property="createTime" />
+		<result column="update_time_" property="updateTime" />
+	</resultMap>
+
+	<!-- 根据主键查询一条记录 -->
+	<select id="get" resultMap="DatabaseSource">
+		SELECT * FROM
+		database_source WHERE id_ = #{id}
+	</select>
+
+	<!-- 全查询 -->
+	<select id="findAll" resultMap="DatabaseSource">
+		SELECT * FROM database_source
+		ORDER BY id_
+	</select>
+
+	<!-- 向数据库增加一条记录 -->
+	<insert id="insert" parameterType="com.keao.edu.datasource.dal.entity.DatabaseSource"
+		useGeneratedKeys="true" keyColumn="id" keyProperty="id">
+		<!-- <selectKey resultClass="int" keyProperty="id" > SELECT SEQ_WSDEFINITION_ID.nextval 
+			AS ID FROM DUAL </selectKey> -->
+		INSERT INTO database_source
+		(id_,tenant_id_,driver_,url_,username_,password_,type_,memo_,create_time_,update_time_)
+		VALUES(#{id},#{tenantId},#{driver},#{url},#{username},#{password},#{type},#{memo},#{createTime},#{updateTime})
+	</insert>
+
+	<!-- 根据主键查询一条记录 -->
+	<update id="update" parameterType="com.keao.edu.datasource.dal.entity.DatabaseSource">
+		UPDATE database_source
+		<set>
+			<if test="driver != null">
+				driver_ = #{driver},
+			</if>
+			<if test="id != null">
+				id_ = #{id},
+			</if>
+			<if test="tenantId != null">
+				tenant_id_ = #{tenantId},
+			</if>
+			<if test="url != null">
+				url_ = #{url},
+			</if>
+			<if test="updateTime != null">
+				update_time_ = #{updateTime},
+			</if>
+			<if test="password != null">
+				password_ = #{password},
+			</if>
+			<if test="memo != null">
+				memo_ = #{memo},
+			</if>
+			<if test="username != null">
+				username_ = #{username},
+			</if>
+			<if test="type != null">
+				type_ = #{type},
+			</if>
+			<if test="createTime != null">
+				create_time_ = #{createTime},
+			</if>
+		</set>
+		WHERE id_ = #{id}
+	</update>
+
+	<!-- 根据主键删除一条记录 -->
+	<delete id="delete">
+		DELETE FROM database_source WHERE id_ =
+		#{id}
+	</delete>
+
+	<!-- 分页查询 -->
+	<select id="queryPage" resultMap="DatabaseSource" parameterType="map">
+		SELECT * FROM database_source ORDER BY id_
+		<include refid="global.limit" />
+	</select>
+
+	<!-- 查询当前表的总记录数 -->
+	<select id="queryCount" resultType="int">
+		SELECT COUNT(*) FROM
+		database_source
+	</select>
+</mapper>

+ 0 - 13
edu-thirdparty/src/main/java/com/keao/edu/App.java

@@ -1,13 +0,0 @@
-package com.keao.edu;
-
-/**
- * Hello world!
- *
- */
-public class App 
-{
-    public static void main( String[] args )
-    {
-        System.out.println( "Hello World!" );
-    }
-}

+ 0 - 38
edu-thirdparty/src/test/java/com/keao/edu/AppTest.java

@@ -1,38 +0,0 @@
-package com.keao.edu;
-
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-
-/**
- * Unit test for simple App.
- */
-public class AppTest 
-    extends TestCase
-{
-    /**
-     * Create the test case
-     *
-     * @param testName name of the test case
-     */
-    public AppTest( String testName )
-    {
-        super( testName );
-    }
-
-    /**
-     * @return the suite of tests being tested
-     */
-    public static Test suite()
-    {
-        return new TestSuite( AppTest.class );
-    }
-
-    /**
-     * Rigourous Test :-)
-     */
-    public void testApp()
-    {
-        assertTrue( true );
-    }
-}

+ 1 - 0
pom.xml

@@ -297,5 +297,6 @@
 	  <module>edu-auth</module>
     <module>edu-im</module>
     <module>edu-thirdparty</module>
+    <module>edu-datasource</module>
   </modules>
 </project>