diff --git a/ruoyi-admin/src/main/resources/application-druid.yml b/ruoyi-admin/src/main/resources/application-druid.yml index 5a74ed1..56ce492 100644 --- a/ruoyi-admin/src/main/resources/application-druid.yml +++ b/ruoyi-admin/src/main/resources/application-druid.yml @@ -12,10 +12,10 @@ spring: # 从库数据源 slave: # 从数据源开关/默认关闭 - enabled: true - url: - username: - password: + enabled: false + url: jdbc:mysql://127.0.0.1/ruoyi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8 + username: root + password: 123456 # 初始连接数 initialSize: 5 diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java index ec3bad0..ad2ac46 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/config/MyBatisConfig.java @@ -150,7 +150,7 @@ public class MyBatisConfig { Map sqlSessionFactoryMap) { try { SqlSessionFactory factorySlave = SpringUtils.getBean(sqlSessionFactoryName); - sqlSessionFactoryMap.put(dataSourceType, factorySlave); + sqlSessionFactoryMap.put(dataSourceType.name(), factorySlave); } catch (Exception e) { logger.error("Failed to register a SqlSessionFactory:{}", sqlSessionFactoryName); } diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/datasource/DynamicSqlSessionTemplate.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/datasource/DynamicSqlSessionTemplate.java index 814d9d8..5f3b893 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/datasource/DynamicSqlSessionTemplate.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/datasource/DynamicSqlSessionTemplate.java @@ -27,8 +27,7 @@ import org.springframework.dao.support.PersistenceExceptionTranslator; * * @author ruoyi */ -public class DynamicSqlSessionTemplate extends SqlSessionTemplate -{ +public class DynamicSqlSessionTemplate extends SqlSessionTemplate { private final SqlSessionFactory sqlSessionFactory; private final ExecutorType executorType; private final SqlSession sqlSessionProxy; @@ -36,20 +35,17 @@ public class DynamicSqlSessionTemplate extends SqlSessionTemplate private Map targetSqlSessionFactorys; private SqlSessionFactory defaultTargetSqlSessionFactory; - public DynamicSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) - { + public DynamicSqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { this(sqlSessionFactory, sqlSessionFactory.getConfiguration().getDefaultExecutorType()); } - public DynamicSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) - { + public DynamicSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType) { this(sqlSessionFactory, executorType, new MyBatisExceptionTranslator( sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), true)); } public DynamicSqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType, - PersistenceExceptionTranslator exceptionTranslator) - { + PersistenceExceptionTranslator exceptionTranslator) { super(sqlSessionFactory, executorType, exceptionTranslator); this.sqlSessionFactory = sqlSessionFactory; this.executorType = executorType; @@ -59,118 +55,100 @@ public class DynamicSqlSessionTemplate extends SqlSessionTemplate this.defaultTargetSqlSessionFactory = sqlSessionFactory; } - public void setTargetSqlSessionFactorys(Map targetSqlSessionFactorys) - { + public void setTargetSqlSessionFactorys(Map targetSqlSessionFactorys) { this.targetSqlSessionFactorys = targetSqlSessionFactorys; } - public void setDefaultTargetSqlSessionFactory(SqlSessionFactory defaultTargetSqlSessionFactory) - { + public void setDefaultTargetSqlSessionFactory(SqlSessionFactory defaultTargetSqlSessionFactory) { this.defaultTargetSqlSessionFactory = defaultTargetSqlSessionFactory; } @Override - public SqlSessionFactory getSqlSessionFactory() - { + public SqlSessionFactory getSqlSessionFactory() { SqlSessionFactory targetSqlSessionFactory = targetSqlSessionFactorys .get(DynamicDataSourceContextHolder.getDataSourceType()); - if (targetSqlSessionFactory != null) - { + if (targetSqlSessionFactory != null) { return targetSqlSessionFactory; - } - else if (defaultTargetSqlSessionFactory != null) - { + } else if (defaultTargetSqlSessionFactory != null) { return defaultTargetSqlSessionFactory; } return this.sqlSessionFactory; } @Override - public Configuration getConfiguration() - { + public Configuration getConfiguration() { return this.getSqlSessionFactory().getConfiguration(); } - public ExecutorType getExecutorType() - { + public ExecutorType getExecutorType() { return this.executorType; } - public PersistenceExceptionTranslator getPersistenceExceptionTranslator() - { + public PersistenceExceptionTranslator getPersistenceExceptionTranslator() { return this.exceptionTranslator; } /** * {@inheritDoc} */ - public T selectOne(String statement) - { - return this.sqlSessionProxy. selectOne(statement); + public T selectOne(String statement) { + return this.sqlSessionProxy.selectOne(statement); } /** * {@inheritDoc} */ - public T selectOne(String statement, Object parameter) - { - return this.sqlSessionProxy. selectOne(statement, parameter); + public T selectOne(String statement, Object parameter) { + return this.sqlSessionProxy.selectOne(statement, parameter); } /** * {@inheritDoc} */ - public Map selectMap(String statement, String mapKey) - { - return this.sqlSessionProxy. selectMap(statement, mapKey); + public Map selectMap(String statement, String mapKey) { + return this.sqlSessionProxy.selectMap(statement, mapKey); } /** * {@inheritDoc} */ - public Map selectMap(String statement, Object parameter, String mapKey) - { - return this.sqlSessionProxy. selectMap(statement, parameter, mapKey); + public Map selectMap(String statement, Object parameter, String mapKey) { + return this.sqlSessionProxy.selectMap(statement, parameter, mapKey); } /** * {@inheritDoc} */ - public Map selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) - { - return this.sqlSessionProxy. selectMap(statement, parameter, mapKey, rowBounds); + public Map selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) { + return this.sqlSessionProxy.selectMap(statement, parameter, mapKey, rowBounds); } /** * {@inheritDoc} */ - public List selectList(String statement) - { - return this.sqlSessionProxy. selectList(statement); + public List selectList(String statement) { + return this.sqlSessionProxy.selectList(statement); } /** * {@inheritDoc} */ - public List selectList(String statement, Object parameter) - { - return this.sqlSessionProxy. selectList(statement, parameter); + public List selectList(String statement, Object parameter) { + return this.sqlSessionProxy.selectList(statement, parameter); } /** * {@inheritDoc} */ - public List selectList(String statement, Object parameter, RowBounds rowBounds) - { - return this.sqlSessionProxy. selectList(statement, parameter, rowBounds); + public List selectList(String statement, Object parameter, RowBounds rowBounds) { + return this.sqlSessionProxy.selectList(statement, parameter, rowBounds); } /** * {@inheritDoc} */ @SuppressWarnings("rawtypes") - public void select(String statement, ResultHandler handler) - { + public void select(String statement, ResultHandler handler) { this.sqlSessionProxy.select(statement, handler); } @@ -178,8 +156,7 @@ public class DynamicSqlSessionTemplate extends SqlSessionTemplate * {@inheritDoc} */ @SuppressWarnings("rawtypes") - public void select(String statement, Object parameter, ResultHandler handler) - { + public void select(String statement, Object parameter, ResultHandler handler) { this.sqlSessionProxy.select(statement, parameter, handler); } @@ -187,120 +164,105 @@ public class DynamicSqlSessionTemplate extends SqlSessionTemplate * {@inheritDoc} */ @SuppressWarnings("rawtypes") - public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) - { + public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) { this.sqlSessionProxy.select(statement, parameter, rowBounds, handler); } /** * {@inheritDoc} */ - public int insert(String statement) - { + public int insert(String statement) { return this.sqlSessionProxy.insert(statement); } /** * {@inheritDoc} */ - public int insert(String statement, Object parameter) - { + public int insert(String statement, Object parameter) { return this.sqlSessionProxy.insert(statement, parameter); } /** * {@inheritDoc} */ - public int update(String statement) - { + public int update(String statement) { return this.sqlSessionProxy.update(statement); } /** * {@inheritDoc} */ - public int update(String statement, Object parameter) - { + public int update(String statement, Object parameter) { return this.sqlSessionProxy.update(statement, parameter); } /** * {@inheritDoc} */ - public int delete(String statement) - { + public int delete(String statement) { return this.sqlSessionProxy.delete(statement); } /** * {@inheritDoc} */ - public int delete(String statement, Object parameter) - { + public int delete(String statement, Object parameter) { return this.sqlSessionProxy.delete(statement, parameter); } /** * {@inheritDoc} */ - public T getMapper(Class type) - { + public T getMapper(Class type) { return getConfiguration().getMapper(type, this); } /** * {@inheritDoc} */ - public void commit() - { + public void commit() { throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession"); } /** * {@inheritDoc} */ - public void commit(boolean force) - { + public void commit(boolean force) { throw new UnsupportedOperationException("Manual commit is not allowed over a Spring managed SqlSession"); } /** * {@inheritDoc} */ - public void rollback() - { + public void rollback() { throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession"); } /** * {@inheritDoc} */ - public void rollback(boolean force) - { + public void rollback(boolean force) { throw new UnsupportedOperationException("Manual rollback is not allowed over a Spring managed SqlSession"); } /** * {@inheritDoc} */ - public void close() - { + public void close() { throw new UnsupportedOperationException("Manual close is not allowed over a Spring managed SqlSession"); } /** * {@inheritDoc} */ - public void clearCache() - { + public void clearCache() { this.sqlSessionProxy.clearCache(); } /** * {@inheritDoc} */ - public Connection getConnection() - { + public Connection getConnection() { return this.sqlSessionProxy.getConnection(); } @@ -309,48 +271,39 @@ public class DynamicSqlSessionTemplate extends SqlSessionTemplate * * @since 1.0.2 */ - public List flushStatements() - { + public List flushStatements() { return this.sqlSessionProxy.flushStatements(); } /** - * Proxy needed to route MyBatis method calls to the proper SqlSession got from Spring's Transaction Manager It also - * unwraps exceptions thrown by {@code Method#invoke(Object, Object...)} to pass a {@code PersistenceException} to + * Proxy needed to route MyBatis method calls to the proper SqlSession got from + * Spring's Transaction Manager It also + * unwraps exceptions thrown by {@code Method#invoke(Object, Object...)} to pass + * a {@code PersistenceException} to * the {@code PersistenceExceptionTranslator}. */ - private class SqlSessionInterceptor implements InvocationHandler - { - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable - { + private class SqlSessionInterceptor implements InvocationHandler { + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { final SqlSession sqlSession = getSqlSession(DynamicSqlSessionTemplate.this.getSqlSessionFactory(), DynamicSqlSessionTemplate.this.executorType, DynamicSqlSessionTemplate.this.exceptionTranslator); - try - { + try { Object result = method.invoke(sqlSession, args); - if (!isSqlSessionTransactional(sqlSession, DynamicSqlSessionTemplate.this.getSqlSessionFactory())) - { + if (!isSqlSessionTransactional(sqlSession, DynamicSqlSessionTemplate.this.getSqlSessionFactory())) { sqlSession.commit(true); } return result; - } - catch (Throwable t) - { + } catch (Throwable t) { Throwable unwrapped = unwrapThrowable(t); if (DynamicSqlSessionTemplate.this.exceptionTranslator != null - && unwrapped instanceof PersistenceException) - { + && unwrapped instanceof PersistenceException) { Throwable translated = DynamicSqlSessionTemplate.this.exceptionTranslator .translateExceptionIfPossible((PersistenceException) unwrapped); - if (translated != null) - { + if (translated != null) { unwrapped = translated; } } throw unwrapped; - } - finally - { + } finally { closeSqlSession(sqlSession, DynamicSqlSessionTemplate.this.getSqlSessionFactory()); } }