Browse Source

适配 Oracle 数据库
1. 修复配置中心的 DAO 查询报错
2. 适配 LIMIT 1 的查询

YunaiV 2 years ago
parent
commit
6f18adb54a

+ 2 - 2
yudao-framework/yudao-spring-boot-starter-config/src/main/java/cn/iocoder/yudao/framework/apollo/internals/ConfigFrameworkDAO.java

@@ -15,12 +15,12 @@ import java.util.List;
 public interface ConfigFrameworkDAO {
 
     /**
-     * 查询是否存在比 maxUpdateTime 更新记录更晚的配置
+     * 查询是否存在比 maxUpdateTime 的更新记录数量
      *
      * @param maxUpdateTime 最大更新时间
      * @return 是否存在
      */
-    boolean selectExistsByUpdateTimeAfter(Date maxUpdateTime);
+    int selectCountByUpdateTimeGt(Date maxUpdateTime);
 
     /**
      * 查询配置列表

+ 1 - 2
yudao-framework/yudao-spring-boot-starter-config/src/main/java/cn/iocoder/yudao/framework/apollo/internals/DBConfigRepository.java

@@ -24,7 +24,6 @@ import java.util.List;
 import java.util.Properties;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledExecutorService;
-import java.util.function.Predicate;
 
 @Slf4j
 public class DBConfigRepository extends AbstractConfigRepository {
@@ -172,7 +171,7 @@ public class DBConfigRepository extends AbstractConfigRepository {
         if (maxUpdateTime == null) { // 如果更新时间为空,说明 DB 一定有新数据
             log.info("[loadConfigIfUpdate][首次加载全量配置]");
         } else { // 判断数据库中是否有更新的配置
-            if (!configFrameworkDAO.selectExistsByUpdateTimeAfter(maxUpdateTime)) {
+            if (configFrameworkDAO.selectCountByUpdateTimeGt(maxUpdateTime) == 0) {
                 return null;
             }
             log.info("[loadConfigIfUpdate][增量加载全量配置]");

+ 4 - 0
yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/config/IdTypeEnvironmentPostProcessor.java

@@ -2,6 +2,7 @@ package cn.iocoder.yudao.framework.mybatis.config;
 
 import cn.hutool.core.util.StrUtil;
 import cn.iocoder.yudao.framework.common.util.collection.SetUtils;
+import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants;
 import cn.iocoder.yudao.framework.mybatis.core.util.JdbcUtils;
 import com.baomidou.mybatisplus.annotation.DbType;
 import com.baomidou.mybatisplus.annotation.IdType;
@@ -41,6 +42,9 @@ public class IdTypeEnvironmentPostProcessor implements EnvironmentPostProcessor
         // TODO 芋艿:暂时没有找到特别合适的地方,先放在这里
         setJobStoreDriverIfPresent(environment, dbType);
 
+        // 初始化 SQL 静态变量
+        SqlConstants.init(dbType);
+
         // 如果非 NONE,则不进行处理
         IdType idType = getIdType(environment);
         if (idType != IdType.NONE) {

+ 11 - 2
yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/enums/SqlConstants.java

@@ -1,12 +1,21 @@
 package cn.iocoder.yudao.framework.mybatis.core.enums;
 
+import com.baomidou.mybatisplus.annotation.DbType;
+
 /**
  * SQL相关常量类
  *
  * @author 芋道源码
  */
-public interface SqlConstants {
+public class SqlConstants {
+
+    /**
+     * 数据库的类型
+     */
+    public static DbType DB_TYPE;
 
-    String LIMIT1 = "LIMIT 1";
+    public static void init(DbType dbType) {
+        DB_TYPE = dbType;
+    }
 
 }

+ 22 - 0
yudao-framework/yudao-spring-boot-starter-mybatis/src/main/java/cn/iocoder/yudao/framework/mybatis/core/query/QueryWrapperX.java

@@ -1,5 +1,7 @@
 package cn.iocoder.yudao.framework.mybatis.core.query;
 
+import cn.hutool.core.lang.Assert;
+import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
@@ -124,4 +126,24 @@ public class QueryWrapperX<T> extends QueryWrapper<T> {
         return this;
     }
 
+    /**
+     * 设置只返回最后一条
+     *
+     * TODO 芋艿:不是完美解,需要在思考下。如果使用多数据源,并且数据源是多种类型时,可能会存在问题:实现之返回一条的语法不同
+     *
+     * @return this
+     */
+    public QueryWrapperX<T> limit1() {
+        Assert.notNull(SqlConstants.DB_TYPE, "获取不到数据库的类型");
+        switch (SqlConstants.DB_TYPE) {
+            case ORACLE:
+            case ORACLE_12C:
+                super.eq("ROWNUM", 1);
+                break;
+            default:
+                super.last("LIMIT 1");
+        }
+        return this;
+    }
+
 }

+ 8 - 6
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/config/ConfigDAOImpl.java

@@ -2,12 +2,10 @@ package cn.iocoder.yudao.module.infra.dal.mysql.config;
 
 import cn.iocoder.yudao.framework.apollo.internals.ConfigFrameworkDAO;
 import cn.iocoder.yudao.framework.apollo.internals.dto.ConfigRespDTO;
-import org.springframework.jdbc.core.BeanPropertyRowMapper;
 import org.springframework.jdbc.core.JdbcTemplate;
 import org.springframework.jdbc.datasource.DriverManagerDataSource;
 
 import javax.sql.DataSource;
-import java.sql.ResultSet;
 import java.util.Date;
 import java.util.List;
 
@@ -26,14 +24,18 @@ public class ConfigDAOImpl implements ConfigFrameworkDAO {
     }
 
     @Override
-    public boolean selectExistsByUpdateTimeAfter(Date maxUpdateTime) {
-        return jdbcTemplate.query("SELECT id FROM infra_config WHERE update_time > ? LIMIT 1",
-                ResultSet::next, maxUpdateTime);
+    public int selectCountByUpdateTimeGt(Date maxUpdateTime) {
+        return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM infra_config WHERE update_time > ?",
+                Integer.class, maxUpdateTime);
     }
 
     @Override
     public List<ConfigRespDTO> selectList() {
-        return jdbcTemplate.query("SELECT `key`, `value`, update_time, deleted FROM infra_config", new BeanPropertyRowMapper<>(ConfigRespDTO.class));
+        return jdbcTemplate.query("SELECT config_key, value, update_time, deleted FROM infra_config",
+                (rs, rowNum) -> new ConfigRespDTO().setKey(rs.getString("config_key"))
+                        .setValue(rs.getString("value"))
+                        .setUpdateTime(rs.getDate("update_time"))
+                        .setDeleted(rs.getBoolean("deleted")));
     }
 
 }

+ 1 - 1
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/controller/app/auth/AppAuthController.http

@@ -9,7 +9,7 @@ tenant-id: {{appTenentId}}
 }
 
 ### 请求 /send-sms-code 接口 => 成功
-POST {{appApi}}/member/send-sms-code
+POST {{appApi}}/member/auth/send-sms-code
 Content-Type: application/json
 tenant-id: {{appTenentId}}
 

+ 7 - 8
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/sms/SmsCodeMapper.java

@@ -1,8 +1,7 @@
 package cn.iocoder.yudao.module.system.dal.mysql.sms;
 
-import cn.iocoder.yudao.framework.mybatis.core.enums.SqlConstants;
 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
-import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
+import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
 import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsCodeDO;
 import org.apache.ibatis.annotations.Mapper;
 
@@ -18,12 +17,12 @@ public interface SmsCodeMapper extends BaseMapperX<SmsCodeDO> {
      * @return 手机验证码
      */
     default SmsCodeDO selectLastByMobile(String mobile, String code, Integer scene) {
-        return selectOne(new LambdaQueryWrapperX<SmsCodeDO>()
-                .eq(SmsCodeDO::getCode, mobile)
-                .eqIfPresent(SmsCodeDO::getScene, scene)
-                .eqIfPresent(SmsCodeDO::getCode, code)
-                .orderByDesc(SmsCodeDO::getId)
-                .last(SqlConstants.LIMIT1));
+        return selectOne(new QueryWrapperX<SmsCodeDO>()
+                .eq("mobile", mobile)
+                .eqIfPresent("scene", scene)
+                .eqIfPresent("code", code)
+                .orderByDesc("id")
+                .limit1());
     }
 
 }