miemie 4 rokov pred
rodič
commit
6efc49edf1

+ 2 - 2
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/pagination/Entity.java → mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/pagecache/PageCache.java

@@ -1,4 +1,4 @@
-package com.baomidou.mybatisplus.test.pagination;
+package com.baomidou.mybatisplus.test.pagecache;
 
 
 import lombok.Data;
 import lombok.Data;
 
 
@@ -9,7 +9,7 @@ import java.io.Serializable;
  * @since 2020-06-23
  * @since 2020-06-23
  */
  */
 @Data
 @Data
-public class Entity implements Serializable {
+public class PageCache implements Serializable {
     private static final long serialVersionUID = 6962439201546719734L;
     private static final long serialVersionUID = 6962439201546719734L;
 
 
     private Long id;
     private Long id;

+ 20 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/pagecache/PageCacheMapper.java

@@ -0,0 +1,20 @@
+package com.baomidou.mybatisplus.test.pagecache;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import org.apache.ibatis.annotations.CacheNamespace;
+import org.apache.ibatis.annotations.Select;
+
+/**
+ * @author miemie
+ * @since 2020-06-23
+ */
+@CacheNamespace
+public interface PageCacheMapper extends BaseMapper<PageCache> {
+
+    @Select("<script>select * from page_cache where <if test=\"tj.name\">name is not null</if></script>")
+    Page<PageCache> otherPage(Page<?> page, PageCacheTest.Tj tj);
+
+    @Select("<script>select count(0) from page_cache where <if test=\"tj.name\">name is not null</if></script>")
+    Long otherCount(PageCacheTest.Tj tj);
+}

+ 26 - 25
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/pagination/PaginationTest.java → mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/pagecache/PageCacheTest.java

@@ -1,4 +1,4 @@
-package com.baomidou.mybatisplus.test.pagination;
+package com.baomidou.mybatisplus.test.pagecache;
 
 
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.metadata.OrderItem;
 import com.baomidou.mybatisplus.core.metadata.OrderItem;
@@ -21,58 +21,59 @@ import static org.assertj.core.api.Assertions.assertThat;
  * @author miemie
  * @author miemie
  * @since 2020-06-23
  * @since 2020-06-23
  */
  */
-class PaginationTest extends BaseDbTest<EntityMapper> {
+class PageCacheTest extends BaseDbTest<PageCacheMapper> {
 
 
     @Test
     @Test
     void page() {
     void page() {
-        Cache cache = sqlSessionFactory.getConfiguration().getCache(EntityMapper.class.getName());
+        Cache cache = sqlSessionFactory.getConfiguration().getCache(PageCacheMapper.class.getName());
         assertThat(cache).as("使用 @CacheNamespace 指定了使用缓存").isNotNull();
         assertThat(cache).as("使用 @CacheNamespace 指定了使用缓存").isNotNull();
-
+        final long total = 5;
+        final long size = 3;
         doTestAutoCommit(m -> {
         doTestAutoCommit(m -> {
-            Page<Entity> page = new Page<>(1, 5);
-            IPage<Entity> result = m.selectPage(page, null);
-            assertThat(page).isEqualTo(result);
-            assertThat(result.getTotal()).isEqualTo(2L);
-            assertThat(result.getRecords().size()).isEqualTo(2);
+            Page<PageCache> page = new Page<>(1, size);
+            IPage<PageCache> result = m.selectPage(page, null);
+            assertThat(page).as("对象是同一个").isEqualTo(result);
+            assertThat(result.getTotal()).isEqualTo(total);
+            assertThat(result.getRecords().size()).isEqualTo(size);
         });
         });
         assertThat(cache.getSize()).as("一条count缓存一条分页缓存").isEqualTo(2);
         assertThat(cache.getSize()).as("一条count缓存一条分页缓存").isEqualTo(2);
 
 
 
 
         doTestAutoCommit(m -> {
         doTestAutoCommit(m -> {
-            Page<Entity> page = new Page<>(1, 5);
-            IPage<Entity> result = m.selectPage(page, null);
+            Page<PageCache> page = new Page<>(1, size);
+            IPage<PageCache> result = m.selectPage(page, null);
             assertThat(page).isEqualTo(result);
             assertThat(page).isEqualTo(result);
-            assertThat(result.getTotal()).isEqualTo(2L);
-            assertThat(result.getRecords().size()).isEqualTo(2);
+            assertThat(result.getTotal()).isEqualTo(total);
+            assertThat(result.getRecords().size()).isEqualTo(size);
         });
         });
         assertThat(cache.getSize()).as("因为命中缓存了所以还是2条").isEqualTo(2);
         assertThat(cache.getSize()).as("因为命中缓存了所以还是2条").isEqualTo(2);
 
 
         doTestAutoCommit(m -> {
         doTestAutoCommit(m -> {
-            Page<Entity> page = new Page<>(1, 5);
+            Page<PageCache> page = new Page<>(1, size);
             page.addOrder(OrderItem.asc("id"));
             page.addOrder(OrderItem.asc("id"));
-            IPage<Entity> result = m.selectPage(page, null);
+            IPage<PageCache> result = m.selectPage(page, null);
             assertThat(page).isEqualTo(result);
             assertThat(page).isEqualTo(result);
-            assertThat(result.getTotal()).isEqualTo(2L);
-            assertThat(result.getRecords().size()).isEqualTo(2);
+            assertThat(result.getTotal()).isEqualTo(total);
+            assertThat(result.getRecords().size()).isEqualTo(size);
         });
         });
         assertThat(cache.getSize()).as("条件不一样了,缓存变为3条").isEqualTo(3);
         assertThat(cache.getSize()).as("条件不一样了,缓存变为3条").isEqualTo(3);
 
 
 
 
-        doTestAutoCommit(m -> m.insert(new Entity()));
+        doTestAutoCommit(m -> m.insert(new PageCache()));
         assertThat(cache.getSize()).as("update 操作清除了所有缓存").isEqualTo(0);
         assertThat(cache.getSize()).as("update 操作清除了所有缓存").isEqualTo(0);
 
 
 
 
         doTestAutoCommit(m -> {
         doTestAutoCommit(m -> {
-            Page<Entity> page = new Page<>(1, 5);
-            IPage<Entity> result = m.selectPage(page, null);
+            Page<PageCache> page = new Page<>(1, size);
+            IPage<PageCache> result = m.selectPage(page, null);
             assertThat(page).isEqualTo(result);
             assertThat(page).isEqualTo(result);
-            assertThat(result.getTotal()).isEqualTo(3L);
-            assertThat(result.getRecords().size()).isEqualTo(3);
+            assertThat(result.getTotal()).isEqualTo(total + 1);
+            assertThat(result.getRecords().size()).isEqualTo(size);
         });
         });
         assertThat(cache.getSize()).as("一条count缓存一条分页缓存").isEqualTo(2);
         assertThat(cache.getSize()).as("一条count缓存一条分页缓存").isEqualTo(2);
 
 
         doTest(i -> {
         doTest(i -> {
-            Page<?> page = new Page<>(1, 2);
+            Page<?> page = new Page<>(1, size);
             page.setCountId("otherCount");
             page.setCountId("otherCount");
             i.otherPage(page, new Tj());
             i.otherPage(page, new Tj());
         });
         });
@@ -87,12 +88,12 @@ class PaginationTest extends BaseDbTest<EntityMapper> {
 
 
     @Override
     @Override
     protected String tableDataSql() {
     protected String tableDataSql() {
-        return "insert into entity(id,name) values(1,'1'),(2,'2');";
+        return "insert into page_cache(id,name) values(1,'1'),(2,'2'),(3,'3'),(4,'4'),(5,'5');";
     }
     }
 
 
     @Override
     @Override
     protected List<String> tableSql() {
     protected List<String> tableSql() {
-        return Arrays.asList("drop table if exists entity", "CREATE TABLE IF NOT EXISTS entity (" +
+        return Arrays.asList("drop table if exists page_cache", "CREATE TABLE IF NOT EXISTS page_cache (" +
             "id BIGINT NOT NULL," +
             "id BIGINT NOT NULL," +
             "name VARCHAR(30) NULL DEFAULT NULL," +
             "name VARCHAR(30) NULL DEFAULT NULL," +
             "PRIMARY KEY (id))");
             "PRIMARY KEY (id))");

+ 0 - 20
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/pagination/EntityMapper.java

@@ -1,20 +0,0 @@
-package com.baomidou.mybatisplus.test.pagination;
-
-import com.baomidou.mybatisplus.core.mapper.BaseMapper;
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
-import org.apache.ibatis.annotations.CacheNamespace;
-import org.apache.ibatis.annotations.Select;
-
-/**
- * @author miemie
- * @since 2020-06-23
- */
-@CacheNamespace
-public interface EntityMapper extends BaseMapper<Entity> {
-
-    @Select("<script>select * from entity where <if test=\"tj.name\">name is not null</if></script>")
-    Page<Entity> otherPage(Page<?> page, PaginationTest.Tj tj);
-
-    @Select("<script>select count(0) from entity where <if test=\"tj.name\">name is not null</if></script>")
-    Long otherCount(PaginationTest.Tj tj);
-}