From 9a885639ae38844c073aac2ed596939385cb296b Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 22 Sep 2024 12:10:57 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=8A=9F=E8=83=BD=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E3=80=91=E5=8D=95=E6=B5=8B=E6=89=A7=E8=A1=8C=E6=8A=A5=E9=94=99?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatis/core/enums/DbTypeEnum.java | 9 ++++++++- .../mybatis/core/util/JdbcUtils.java | 10 ++++++++-- .../mybatis/core/util/MyBatisUtils.java | 1 - .../test/core/ut/BaseDbAndRedisUnitTest.java | 4 ++++ .../test/core/ut/BaseDbUnitTest.java | 4 ++++ .../test/core/ut/BaseRedisUnitTest.java | 4 ++++ .../notify/NotifyMessageServiceImplTest.java | 6 +----- .../service/sms/SmsCodeServiceImplTest.java | 20 +------------------ 8 files changed, 30 insertions(+), 28 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/DbTypeEnum.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/DbTypeEnum.java index cecc12d46..3929b7106 100644 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/DbTypeEnum.java +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/DbTypeEnum.java @@ -18,10 +18,17 @@ import java.util.stream.Collectors; @AllArgsConstructor public enum DbTypeEnum { + /** + * H2 + * + * 注意:H2 不支持 find_in_set 函数 + */ + H2(DbType.H2, "H2", ""), + /** * MySQL */ - MY_SQL( DbType.MYSQL, "MySQL", "FIND_IN_SET('#{value}', #{column}) <> 0"), + MY_SQL(DbType.MYSQL, "MySQL", "FIND_IN_SET('#{value}', #{column}) <> 0"), /** * Oracle diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/JdbcUtils.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/JdbcUtils.java index 1e7e805c4..c3a6eff70 100644 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/JdbcUtils.java +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/JdbcUtils.java @@ -5,6 +5,7 @@ import cn.iocoder.yudao.framework.common.util.spring.SpringUtils; import cn.iocoder.yudao.framework.mybatis.core.enums.DbTypeEnum; import com.baomidou.dynamic.datasource.DynamicRoutingDataSource; import com.baomidou.mybatisplus.annotation.DbType; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; import javax.sql.DataSource; import java.sql.Connection; @@ -50,8 +51,13 @@ public class JdbcUtils { * @return DB 类型 */ public static DbType getDbType() { - DynamicRoutingDataSource dynamicRoutingDataSource = SpringUtils.getBean(DynamicRoutingDataSource.class); - DataSource dataSource = dynamicRoutingDataSource.determineDataSource(); + DataSource dataSource; + try { + DynamicRoutingDataSource dynamicRoutingDataSource = SpringUtils.getBean(DynamicRoutingDataSource.class); + dataSource = dynamicRoutingDataSource.determineDataSource(); + } catch (NoSuchBeanDefinitionException e) { + dataSource = SpringUtils.getBean(DataSource.class); + } try (Connection conn = dataSource.getConnection()) { return DbTypeEnum.find(conn.getMetaData().getDatabaseProductName()); } catch (SQLException e) { diff --git a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/MyBatisUtils.java b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/MyBatisUtils.java index 611b71565..0f9351791 100644 --- a/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/MyBatisUtils.java +++ b/yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/util/MyBatisUtils.java @@ -96,7 +96,6 @@ public class MyBatisUtils { * @return sql */ public static String findInSet(String column, Object value) { - // 这里不用SqlConstants.DB_TYPE,因为它是使用 primary 数据源的 url 推断出来的类型 DbType dbType = JdbcUtils.getDbType(); return DbTypeEnum.getFindInSetTemplate(dbType) .replace("#{column}", column) diff --git a/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseDbAndRedisUnitTest.java b/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseDbAndRedisUnitTest.java index d30cf6b3e..46a6927d6 100644 --- a/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseDbAndRedisUnitTest.java +++ b/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseDbAndRedisUnitTest.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.framework.test.core.ut; +import cn.hutool.extra.spring.SpringUtil; import cn.iocoder.yudao.framework.datasource.config.YudaoDataSourceAutoConfiguration; import cn.iocoder.yudao.framework.mybatis.config.YudaoMybatisAutoConfiguration; import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration; @@ -44,6 +45,9 @@ public class BaseDbAndRedisUnitTest { YudaoRedisAutoConfiguration.class, // 自己的 Redis 配置类 RedisAutoConfiguration.class, // Spring Redis 自动配置类 RedissonAutoConfiguration.class, // Redisson 自动配置类 + + // 其它配置类 + SpringUtil.class }) public static class Application { } diff --git a/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseDbUnitTest.java b/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseDbUnitTest.java index ee43fcb43..98b06f95f 100644 --- a/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseDbUnitTest.java +++ b/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseDbUnitTest.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.framework.test.core.ut; +import cn.hutool.extra.spring.SpringUtil; import cn.iocoder.yudao.framework.datasource.config.YudaoDataSourceAutoConfiguration; import cn.iocoder.yudao.framework.mybatis.config.YudaoMybatisAutoConfiguration; import cn.iocoder.yudao.framework.test.config.SqlInitializationTestConfiguration; @@ -36,6 +37,9 @@ public class BaseDbUnitTest { YudaoMybatisAutoConfiguration.class, // 自己的 MyBatis 配置类 MybatisPlusAutoConfiguration.class, // MyBatis 的自动配置类 MybatisPlusJoinAutoConfiguration.class, // MyBatis 的Join配置类 + + // 其它配置类 + SpringUtil.class }) public static class Application { } diff --git a/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseRedisUnitTest.java b/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseRedisUnitTest.java index 7b84003d1..ff6315a2d 100644 --- a/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseRedisUnitTest.java +++ b/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/ut/BaseRedisUnitTest.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.framework.test.core.ut; +import cn.hutool.extra.spring.SpringUtil; import cn.iocoder.yudao.framework.redis.config.YudaoRedisAutoConfiguration; import cn.iocoder.yudao.framework.test.config.RedisTestConfiguration; import org.redisson.spring.starter.RedissonAutoConfiguration; @@ -25,6 +26,9 @@ public class BaseRedisUnitTest { RedisAutoConfiguration.class, // Spring Redis 自动配置类 YudaoRedisAutoConfiguration.class, // 自己的 Redis 配置类 RedissonAutoConfiguration.class, // Redisson 自动配置类 + + // 其它配置类 + SpringUtil.class }) public static class Application { } diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageServiceImplTest.java index 48a68e43f..cfd193b65 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageServiceImplTest.java +++ b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageServiceImplTest.java @@ -3,18 +3,16 @@ package cn.iocoder.yudao.module.system.service.notify; import cn.hutool.core.map.MapUtil; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants; import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessageMyPageReqVO; import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO; import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyMessageDO; import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyTemplateDO; import cn.iocoder.yudao.module.system.dal.mysql.notify.NotifyMessageMapper; -import com.baomidou.mybatisplus.annotation.DbType; +import jakarta.annotation.Resource; import org.junit.jupiter.api.Test; import org.springframework.context.annotation.Import; -import jakarta.annotation.Resource; import java.util.Arrays; import java.util.Collection; import java.util.List; @@ -158,7 +156,6 @@ public class NotifyMessageServiceImplTest extends BaseDbUnitTest { @Test public void testGetUnreadNotifyMessageList() { - SqlConstants.init(DbType.MYSQL); // mock 数据 NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到 o.setUserId(1L); @@ -187,7 +184,6 @@ public class NotifyMessageServiceImplTest extends BaseDbUnitTest { @Test public void testGetUnreadNotifyMessageCount() { - SqlConstants.init(DbType.MYSQL); // mock 数据 NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到 o.setUserId(1L); diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsCodeServiceImplTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsCodeServiceImplTest.java index ef9d2ff23..093a5aeff 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsCodeServiceImplTest.java +++ b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sms/SmsCodeServiceImplTest.java @@ -1,7 +1,6 @@ package cn.iocoder.yudao.module.system.service.sms; import cn.hutool.core.map.MapUtil; -import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants; import cn.iocoder.yudao.framework.test.core.ut.BaseDbUnitTest; import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeSendReqDTO; import cn.iocoder.yudao.module.system.api.sms.dto.code.SmsCodeUseReqDTO; @@ -10,13 +9,12 @@ import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsCodeDO; import cn.iocoder.yudao.module.system.dal.mysql.sms.SmsCodeMapper; import cn.iocoder.yudao.module.system.enums.sms.SmsSceneEnum; import cn.iocoder.yudao.module.system.framework.sms.config.SmsCodeProperties; -import com.baomidou.mybatisplus.annotation.DbType; +import jakarta.annotation.Resource; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; -import jakarta.annotation.Resource; import java.time.Duration; import java.time.LocalDateTime; @@ -61,8 +59,6 @@ public class SmsCodeServiceImplTest extends BaseDbUnitTest { o.setMobile("15601691300"); o.setScene(SmsSceneEnum.MEMBER_LOGIN.getScene()); }); - // mock 方法 - SqlConstants.init(DbType.MYSQL); // 调用 smsCodeService.sendSmsCode(reqDTO); @@ -88,8 +84,6 @@ public class SmsCodeServiceImplTest extends BaseDbUnitTest { o.setMobile("15601691300"); o.setScene(SmsSceneEnum.MEMBER_LOGIN.getScene()); }); - // mock 方法 - SqlConstants.init(DbType.MYSQL); // 调用,并断言异常 assertServiceException(() -> smsCodeService.sendSmsCode(reqDTO), @@ -107,8 +101,6 @@ public class SmsCodeServiceImplTest extends BaseDbUnitTest { o.setMobile("15601691300"); o.setScene(SmsSceneEnum.MEMBER_LOGIN.getScene()); }); - // mock 方法 - SqlConstants.init(DbType.MYSQL); when(smsCodeProperties.getSendFrequency()).thenReturn(Duration.ofMillis(0)); // 调用,并断言异常 @@ -123,8 +115,6 @@ public class SmsCodeServiceImplTest extends BaseDbUnitTest { o.setMobile("15601691300"); o.setScene(randomEle(SmsSceneEnum.values()).getScene()); }); - // mock 数据 - SqlConstants.init(DbType.MYSQL); smsCodeMapper.insert(randomPojo(SmsCodeDO.class, o -> { o.setMobile(reqDTO.getMobile()).setScene(reqDTO.getScene()) .setCode(reqDTO.getCode()).setUsed(false); @@ -146,8 +136,6 @@ public class SmsCodeServiceImplTest extends BaseDbUnitTest { o.setMobile("15601691300"); o.setScene(randomEle(SmsSceneEnum.values()).getScene()); }); - // mock 数据 - SqlConstants.init(DbType.MYSQL); smsCodeMapper.insert(randomPojo(SmsCodeDO.class, o -> o.setMobile(reqDTO.getMobile()) .setScene(reqDTO.getScene()).setCode(reqDTO.getCode()).setUsed(false))); @@ -162,8 +150,6 @@ public class SmsCodeServiceImplTest extends BaseDbUnitTest { o.setMobile("15601691300"); o.setScene(randomEle(SmsSceneEnum.values()).getScene()); }); - // mock 数据 - SqlConstants.init(DbType.MYSQL); // 调用,并断言异常 assertServiceException(() -> smsCodeService.validateSmsCode(reqDTO), @@ -177,8 +163,6 @@ public class SmsCodeServiceImplTest extends BaseDbUnitTest { o.setMobile("15601691300"); o.setScene(randomEle(SmsSceneEnum.values()).getScene()); }); - // mock 数据 - SqlConstants.init(DbType.MYSQL); smsCodeMapper.insert(randomPojo(SmsCodeDO.class, o -> o.setMobile(reqDTO.getMobile()) .setScene(reqDTO.getScene()).setCode(reqDTO.getCode()).setUsed(false) .setCreateTime(LocalDateTime.now().minusMinutes(6)))); @@ -195,8 +179,6 @@ public class SmsCodeServiceImplTest extends BaseDbUnitTest { o.setMobile("15601691300"); o.setScene(randomEle(SmsSceneEnum.values()).getScene()); }); - // mock 数据 - SqlConstants.init(DbType.MYSQL); smsCodeMapper.insert(randomPojo(SmsCodeDO.class, o -> o.setMobile(reqDTO.getMobile()) .setScene(reqDTO.getScene()).setCode(reqDTO.getCode()).setUsed(true) .setCreateTime(LocalDateTime.now())));