Bladeren bron

Merge branch '3.0' into 3.gay

miemie 3 jaren geleden
bovenliggende
commit
c61a7d0ad0

+ 12 - 3
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/PaginationInnerInterceptor.java

@@ -331,13 +331,22 @@ public class PaginationInnerInterceptor implements InnerInterceptor {
                         }
                         // 不区分大小写
                         str = str.toLowerCase();
-                        String onExpressionS = join.getOnExpression().toString();
-                        /* 如果 join 里包含 ?(代表有入参) 或者 where 条件里包含使用 join 的表的字段作条件,就不移除 join */
-                        if (onExpressionS.contains(StringPool.QUESTION_MARK) || whereS.contains(str)) {
+
+                        if (whereS.contains(str)) {
+                            /* 如果 where 条件里包含使用 join 的表的字段作条件,就不移除 join */
                             canRemoveJoin = false;
                             break;
                         }
+
+                        for (Expression expression : join.getOnExpressions()) {
+                            if (expression.toString().contains(StringPool.QUESTION_MARK)) {
+                                /* 如果 join 里包含 ?(代表有入参) 就不移除 join */
+                                canRemoveJoin = false;
+                                break;
+                            }
+                        }
                     }
+
                     if (canRemoveJoin) {
                         plainSelect.setJoins(null);
                     }

+ 7 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/BaseDbTest.java

@@ -27,6 +27,7 @@ import org.springframework.jdbc.datasource.SimpleDriverDataSource;
 import javax.sql.DataSource;
 import java.io.IOException;
 import java.io.InputStream;
+import java.util.Collections;
 import java.util.List;
 import java.util.function.Consumer;
 
@@ -83,6 +84,8 @@ public abstract class BaseDbTest<T> extends TypeReference<T> {
             }
         }
         configuration.addMapper(mapper);
+        otherMapper().forEach(configuration::addMapper);
+
         if (CollectionUtils.isNotEmpty(interceptors)) {
             interceptors.forEach(configuration::addInterceptor);
         }
@@ -155,4 +158,8 @@ public abstract class BaseDbTest<T> extends TypeReference<T> {
     protected Consumer<Configuration> consumer() {
         return null;
     }
+
+    protected List<Class<?>> otherMapper() {
+        return Collections.emptyList();
+    }
 }

+ 17 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/puginsome/A.java

@@ -0,0 +1,17 @@
+package com.baomidou.mybatisplus.test.puginsome;
+
+import com.baomidou.mybatisplus.annotation.TableField;
+import lombok.Data;
+
+/**
+ * @author miemie
+ * @since 2022-05-11
+ */
+@Data
+public class A {
+    private int id;
+    private String name;
+
+    @TableField(exist = false)
+    private B b;
+}

+ 14 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/puginsome/AMapper.java

@@ -0,0 +1,14 @@
+package com.baomidou.mybatisplus.test.puginsome;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+import java.util.List;
+
+/**
+ * @author miemie
+ * @since 2022-05-11
+ */
+public interface AMapper extends BaseMapper<A> {
+
+    List<A> list();
+}

+ 14 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/puginsome/B.java

@@ -0,0 +1,14 @@
+package com.baomidou.mybatisplus.test.puginsome;
+
+import lombok.Data;
+
+/**
+ * @author miemie
+ * @since 2022-05-11
+ */
+@Data
+public class B {
+    private int id;
+    private int aId;
+    private String name;
+}

+ 10 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/puginsome/BMapper.java

@@ -0,0 +1,10 @@
+package com.baomidou.mybatisplus.test.puginsome;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+
+/**
+ * @author miemie
+ * @since 2022-05-11
+ */
+public interface BMapper extends BaseMapper<B> {
+}

+ 100 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/puginsome/PluginSomeTest.java

@@ -0,0 +1,100 @@
+package com.baomidou.mybatisplus.test.puginsome;
+
+import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
+import com.baomidou.mybatisplus.test.BaseDbTest;
+import org.apache.ibatis.executor.Executor;
+import org.apache.ibatis.executor.statement.StatementHandler;
+import org.apache.ibatis.mapping.BoundSql;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.plugin.Interceptor;
+import org.apache.ibatis.session.ResultHandler;
+import org.apache.ibatis.session.RowBounds;
+import org.junit.jupiter.api.Test;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @author miemie
+ * @since 2020-06-24
+ */
+public class PluginSomeTest extends BaseDbTest<AMapper> {
+
+    @Test
+    void test() {
+        doTest(m -> {
+            List<A> a = m.list();
+            a.forEach(System.out::println);
+        });
+    }
+
+    @Override
+    protected List<Interceptor> interceptors() {
+        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
+        interceptor.addInnerInterceptor(new InnerInterceptor() {
+            @Override
+            public boolean willDoQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
+                System.out.println("willDoQuery");
+                return true;
+            }
+
+            @Override
+            public void beforeQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
+                System.out.println("beforeQuery");
+            }
+
+            @Override
+            public boolean willDoUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
+                System.out.println("willDoUpdate");
+                return true;
+            }
+
+            @Override
+            public void beforeUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
+                System.out.println("beforeUpdate");
+            }
+
+            @Override
+            public void beforePrepare(StatementHandler sh, Connection connection, Integer transactionTimeout) {
+                System.out.println("beforePrepare");
+            }
+
+            @Override
+            public void beforeGetBoundSql(StatementHandler sh) {
+                System.out.println("beforeGetBoundSql");
+            }
+        });
+        return Collections.singletonList(interceptor);
+    }
+
+    @Override
+    protected List<Class<?>> otherMapper() {
+        return Collections.singletonList(BMapper.class);
+    }
+
+    @Override
+    protected String tableDataSql() {
+        return "insert into a (id,name) values (1,'1'),(2,'2');" +
+            "insert into b (id,a_id,name) values (1,'1','1-1'),(2,'2','2-2')";
+    }
+
+    @Override
+    protected List<String> tableSql() {
+        return Arrays.asList("drop table if exists a",
+            "CREATE TABLE IF NOT EXISTS a (\n" +
+                "id integer NOT NULL,\n" +
+                "name VARCHAR(30) NULL DEFAULT NULL,\n" +
+                "PRIMARY KEY (id)" +
+                ")", "drop table if exists b",
+            "CREATE TABLE IF NOT EXISTS b (\n" +
+                "id integer NOT NULL,\n" +
+                "a_id integer NOT NULL,\n" +
+                "name VARCHAR(30) NULL DEFAULT NULL,\n" +
+                "PRIMARY KEY (id)" +
+                ")");
+    }
+}

+ 14 - 0
mybatis-plus/src/test/resources/com/baomidou/mybatisplus/test/puginsome/AMapper.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
+<mapper namespace="com.baomidou.mybatisplus.test.puginsome.AMapper">
+
+    <resultMap id="xxp" type="com.baomidou.mybatisplus.test.puginsome.A" autoMapping="true">
+        <association property="b" column="id" javaType="com.baomidou.mybatisplus.test.puginsome.B"
+                     select="com.baomidou.mybatisplus.test.puginsome.BMapper.selectById"/>
+    </resultMap>
+
+    <select id="list" resultMap="xxp">
+        select *
+        from a
+    </select>
+</mapper>

+ 6 - 0
mybatis-plus/src/test/resources/com/baomidou/mybatisplus/test/puginsome/BMapper.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
+<mapper namespace="com.baomidou.mybatisplus.test.puginsome.BMapper">
+
+
+</mapper>