hubin преди 7 години
родител
ревизия
3ae70d82cd

+ 1 - 1
mybatis-plus-annotation/src/main/java/com/baomidou/mybatisplus/annotation/DbType.java

@@ -38,7 +38,7 @@ public enum DbType {
     /**
      * ORACLE
      */
-    ORACLE("oracle", null, "%s LIKE CONCAT('%%',#{%s},'%%')", " ROWNUM <= %s", "Oracle数据库"),
+    ORACLE("oracle", null, "%s LIKE CONCAT('%%',#{%s},'%%')", null, "Oracle数据库"),
     /**
      * DB2
      */

+ 11 - 2
mybatis-plus-core/src/main/java/com/baomidou/mybatisplus/core/injector/methods/SelectOne.java

@@ -18,6 +18,7 @@ package com.baomidou.mybatisplus.core.injector.methods;
 import org.apache.ibatis.mapping.MappedStatement;
 import org.apache.ibatis.mapping.SqlSource;
 
+import com.baomidou.mybatisplus.annotation.DbType;
 import com.baomidou.mybatisplus.core.enums.SqlMethod;
 import com.baomidou.mybatisplus.core.injector.AbstractMethod;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
@@ -35,8 +36,16 @@ public class SelectOne extends AbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_ONE;
-        String sql = String.format(sqlMethod.getSql(), this.sqlSelectColumns(tableInfo, false),
-            tableInfo.getTableName(), this.sqlWhereEntityWrapper(tableInfo) + getGlobalConfig().getDbConfig().getDbType().getLimit(1));
+        DbType dbType = getGlobalConfig().getDbConfig().getDbType();
+        String select = this.sqlSelectColumns(tableInfo, false);
+        if (dbType == DbType.SQL_SERVER || dbType == DbType.SQL_SERVER2005) {
+            select = "TOP 1 " + select;
+        }
+        String sql = String.format(sqlMethod.getSql(), select, tableInfo.getTableName(),
+            this.sqlWhereEntityWrapper(tableInfo) + dbType.getLimit(1));
+        if (dbType == DbType.ORACLE) {
+            sql = String.format("SELECT * FROM (%s) ROWNUM <= 1");
+        }
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return this.addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, tableInfo);
     }

+ 11 - 2
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/injector/methods/LogicSelectOne.java

@@ -18,6 +18,7 @@ package com.baomidou.mybatisplus.extension.injector.methods;
 import org.apache.ibatis.mapping.MappedStatement;
 import org.apache.ibatis.mapping.SqlSource;
 
+import com.baomidou.mybatisplus.annotation.DbType;
 import com.baomidou.mybatisplus.core.enums.SqlMethod;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import com.baomidou.mybatisplus.extension.injector.LogicAbstractMethod;
@@ -35,8 +36,16 @@ public class LogicSelectOne extends LogicAbstractMethod {
     @Override
     public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
         SqlMethod sqlMethod = SqlMethod.SELECT_ONE;
-        String sql = String.format(sqlMethod.getSql(), sqlSelectColumns(tableInfo, false),
-            tableInfo.getTableName(), this.sqlWhereEntityWrapper(tableInfo) + getGlobalConfig().getDbConfig().getDbType().getLimit(1));
+        DbType dbType = getGlobalConfig().getDbConfig().getDbType();
+        String select = this.sqlSelectColumns(tableInfo, false);
+        if (dbType == DbType.SQL_SERVER || dbType == DbType.SQL_SERVER2005) {
+            select = "TOP 1 " + select;
+        }
+        String sql = String.format(sqlMethod.getSql(), select, tableInfo.getTableName(),
+            this.sqlWhereEntityWrapper(tableInfo) + dbType.getLimit(1));
+        if (dbType == DbType.ORACLE) {
+            sql = String.format("SELECT * FROM (%s) ROWNUM <= 1");
+        }
         SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
         return addSelectMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource, modelClass, tableInfo);
     }