diff --git a/ruoyi-admin/src/main/resources/application-dev.yml b/ruoyi-admin/src/main/resources/application-dev.yml new file mode 100644 index 000000000..57b36d518 --- /dev/null +++ b/ruoyi-admin/src/main/resources/application-dev.yml @@ -0,0 +1,48 @@ +# 数据源配置 +spring: + datasource: + type: com.alibaba.druid.pool.DruidDataSource + driverClassName: com.mysql.jdbc.Driver + druid: + # 主库数据源 + master: + url: jdbc:mysql://localhost:3306/ry?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true + username: root + password: root + # 从库数据源 + slave: + # 从数据源开关/默认关闭 + enabled: true + url: jdbc:mysql://localhost:3306/ry?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true + username: root + password: root + # 初始连接数 + initial-size: 10 + # 最大连接池数量 + max-active: 100 + # 最小连接池数量 + min-idle: 10 + # 配置获取连接等待超时的时间 + max-wait: 60000 + # 打开PSCache,并且指定每个连接上PSCache的大小 + pool-prepared-statements: true + max-pool-prepared-statement-per-connection-size: 20 + # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 + timeBetweenEvictionRunsMillis: 60000 + # 配置一个连接在池中最小生存的时间,单位是毫秒 + min-evictable-idle-time-millis: 300000 + validation-query: SELECT 1 FROM DUAL + test-while-idle: true + test-on-borrow: false + test-on-return: false + stat-view-servlet: + enabled: true + url-pattern: /monitor/druid/* + filter: + stat: + log-slow-sql: true + slow-sql-millis: 1000 + merge-sql: false + wall: + config: + multi-statement-allow: true \ No newline at end of file diff --git a/ruoyi-admin/src/main/resources/application-druid.yml b/ruoyi-admin/src/main/resources/application-prod.yml similarity index 95% rename from ruoyi-admin/src/main/resources/application-druid.yml rename to ruoyi-admin/src/main/resources/application-prod.yml index da7a23d52..62dc55b67 100644 --- a/ruoyi-admin/src/main/resources/application-druid.yml +++ b/ruoyi-admin/src/main/resources/application-prod.yml @@ -8,7 +8,7 @@ spring: master: url: jdbc:mysql://localhost:3306/ry?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true username: root - password: password + password: root # 从库数据源 slave: # 从数据源开关/默认关闭 diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index a1acec922..07dda1ac4 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -55,7 +55,7 @@ spring: time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss profiles: - active: druid + active: dev # 文件上传 servlet: multipart: @@ -66,6 +66,9 @@ spring: restart: # 热部署开关 enabled: true + output: + ansi: + enabled: always # MyBatis mybatis: @@ -73,8 +76,10 @@ mybatis: typeAliasesPackage: com.ruoyi # 配置mapper的扫描,找到所有的mapper.xml映射文件 mapperLocations: classpath*:mapper/**/*Mapper.xml - # 加载全局的配置文件 - configLocation: classpath:mapper/mybatis-config.xml + +mapper: + not-empty: true + identity: MYSQL # PageHelper分页插件 pagehelper: diff --git a/ruoyi-admin/src/main/resources/logback-spring.xml b/ruoyi-admin/src/main/resources/logback-spring.xml new file mode 100644 index 000000000..abdb98ba7 --- /dev/null +++ b/ruoyi-admin/src/main/resources/logback-spring.xml @@ -0,0 +1,206 @@ + + + + + + + ruoyi + + + + + + + + + + + + + + + + + + debug + + + ${CONSOLE_LOG_PATTERN} + + UTF-8 + + + + + + + + + + ${log.path}/web_debug.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - + %msg%n + UTF-8 + + + + + ${log.path}/web-debug-%d{yyyy-MM-dd}.%i.log + + + 100MB + + + 15 + + + + debug + ACCEPT + DENY + + + + + + + ${log.path}/web_info.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - + %msg%n + UTF-8 + + + + + ${log.path}/web-info-%d{yyyy-MM-dd}.%i.log + + + 100MB + + + 15 + + + + info + ACCEPT + DENY + + + + + + + ${log.path}/web_warn.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - + %msg%n + UTF-8 + + + + ${log.path}/web-warn-%d{yyyy-MM-dd}.%i.log + + + 100MB + + + 15 + + + + warn + ACCEPT + DENY + + + + + + + ${log.path}/web_error.log + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - + %msg%n + UTF-8 + + + + ${log.path}/web-error-%d{yyyy-MM-dd}.%i.log + + + 100MB + + + 15 + + + + ERROR + ACCEPT + DENY + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ruoyi-admin/src/main/resources/logback.xml b/ruoyi-admin/src/main/resources/logback.xml deleted file mode 100644 index bd468488c..000000000 --- a/ruoyi-admin/src/main/resources/logback.xml +++ /dev/null @@ -1,86 +0,0 @@ - - - - - - - - - - ${log.pattern} - - - - - - ${log.path}/sys-info.log - - - ${log.path}/sys-info.%d{yyyy-MM-dd}.log - - 60 - - - ${log.pattern} - - - INFO - ACCEPT - DENY - - - - - ${log.path}/sys-error.log - - ${log.path}/sys-error.%d{yyyy-MM-dd}.log - - 60 - - - ${log.pattern} - - - ERROR - ACCEPT - DENY - - - - - - ${log.path}/sys-user.log - - - ${log.path}/sys-user.%d{yyyy-MM-dd}.log - - 60 - - - ${log.pattern} - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index 32552e111..e37811f65 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -20,6 +20,7 @@ 1.3.3 1.11.3 3.17 + 2.0.4 @@ -70,6 +71,13 @@ snakeyaml + + + tk.mybatis + mapper-spring-boot-starter + ${mapper.starter.version} + + \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/DataSource.java b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/DataSource.java deleted file mode 100644 index 5a41a2c6d..000000000 --- a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/DataSource.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.ruoyi.common.annotation; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; -import com.ruoyi.common.enums.DataSourceType; - -/** - * 自定义多数据源切换注解 - * - * @author ruoyi - */ -@Target(ElementType.METHOD) -@Retention(RetentionPolicy.RUNTIME) -public @interface DataSource -{ - /** - * 切换数据源名称 - */ - public DataSourceType value() default DataSourceType.MASTER; -} diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/base/BaseMapper.java b/ruoyi-common/src/main/java/com/ruoyi/common/base/BaseMapper.java new file mode 100644 index 000000000..726b66392 --- /dev/null +++ b/ruoyi-common/src/main/java/com/ruoyi/common/base/BaseMapper.java @@ -0,0 +1,24 @@ +/* + * @(#)BaseMapper.java 2016-3-30 下午5:57:15 + * Copyright 2016 张孟如, Inc. All rights reserved. + * PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. + */ +package com.ruoyi.common.base; + +import tk.mybatis.mapper.common.ConditionMapper; +import tk.mybatis.mapper.common.IdsMapper; +import tk.mybatis.mapper.common.Mapper; +import tk.mybatis.mapper.common.special.InsertListMapper; + +/** + *

File:BaseMapper.java

+ *

Title:

+ *

Description:

+ *

Copyright: Copyright (c) 2016 2016-3-30 下午5:57:15

+ *

Company:

+ * @author 张孟如 + * @version 1.0 + */ +public interface BaseMapper extends Mapper, IdsMapper,InsertListMapper,ConditionMapper +{ +} \ No newline at end of file diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java index c4eab3fbc..a38c76b91 100644 --- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java +++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java @@ -1,23 +1,20 @@ package com.ruoyi.framework.aspectj; -import java.lang.reflect.Method; +import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; -import org.aspectj.lang.annotation.Pointcut; -import org.aspectj.lang.reflect.MethodSignature; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; -import com.ruoyi.common.annotation.DataSource; -import com.ruoyi.common.utils.StringUtils; + +import com.ruoyi.common.enums.DataSourceType; import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder; + /** * 多数据源处理 - * - * @author ruoyi */ @Aspect @Order(1) @@ -26,26 +23,21 @@ public class DataSourceAspect { protected Logger logger = LoggerFactory.getLogger(getClass()); - @Pointcut("@annotation(com.ruoyi.common.annotation.DataSource)") - public void dsPointCut() - { - - } - - @Around("dsPointCut()") + @Around("execution(* com.ruoyi..*ServiceImpl.*(..))") public Object around(ProceedingJoinPoint point) throws Throwable { - MethodSignature signature = (MethodSignature) point.getSignature(); - - Method method = signature.getMethod(); - - DataSource dataSource = method.getAnnotation(DataSource.class); - - if (StringUtils.isNotNull(dataSource)) + // 获取到当前执行的方法名 + String methodName = point.getSignature().getName(); + if (isSlave(methodName)) { - DynamicDataSourceContextHolder.setDateSoureType(dataSource.value().name()); + // 标记为读库,可以自定义选择数据源 + DynamicDataSourceContextHolder.setDateSoureType(DataSourceType.SLAVE.name()); + } + else + { + // 标记为写库 + DynamicDataSourceContextHolder.setDateSoureType(DataSourceType.MASTER.name()); } - try { return point.proceed(); @@ -56,4 +48,16 @@ public class DataSourceAspect DynamicDataSourceContextHolder.clearDateSoureType(); } } + + /** + * 判断是否为读库 + * + * @param methodName + * @return + */ + private boolean isSlave(String methodName) + { + // 方法名以query、find、get开头的方法名走从库 + return StringUtils.startsWithAny(methodName, new String[]{"query", "find", "get", "select"}); + } } diff --git a/ruoyi-system/src/main/resources/mapper/mybatis-config.xml b/ruoyi-system/src/main/resources/mapper/mybatis-config.xml deleted file mode 100644 index c7c36e375..000000000 --- a/ruoyi-system/src/main/resources/mapper/mybatis-config.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - - - - - -