Browse Source

增加泛型主键测试用例.

https://gitee.com/baomidou/mybatis-plus/issues/I171CQ
聂秋秋 5 years ago
parent
commit
46bff08214

+ 8 - 8
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/plugins/pagination/SelectBodyToPlainSelectTest.java

@@ -62,34 +62,34 @@ class SelectBodyToPlainSelectTest {
         String actualSql = PaginationInterceptor
             .concatOrderBy("select * from test", page);
 
-        assertThat(actualSql).isEqualTo("SELECT * FROM test ORDER BY column");
+        assertThat(actualSql).isEqualTo("SELECT * FROM test ORDER BY column ASC");
 
         String actualSqlWhere = PaginationInterceptor
             .concatOrderBy("select * from test where 1 = 1", page);
 
-        assertThat(actualSqlWhere).isEqualTo("SELECT * FROM test WHERE 1 = 1 ORDER BY column");
+        assertThat(actualSqlWhere).isEqualTo("SELECT * FROM test WHERE 1 = 1 ORDER BY column ASC");
     }
 
     @Test
     void testPaginationInterceptorConcatOrderByFix() {
         String actualSql = PaginationInterceptor
             .concatOrderBy("select * from test union select * from test2", page);
-        assertThat(actualSql).isEqualTo("SELECT * FROM test UNION SELECT * FROM test2 ORDER BY column");
+        assertThat(actualSql).isEqualTo("SELECT * FROM test UNION SELECT * FROM test2 ORDER BY column ASC");
 
         String actualSqlUnionAll = PaginationInterceptor
             .concatOrderBy("select * from test union all select * from test2", page);
-        assertThat(actualSqlUnionAll).isEqualTo("SELECT * FROM test UNION ALL SELECT * FROM test2 ORDER BY column");
+        assertThat(actualSqlUnionAll).isEqualTo("SELECT * FROM test UNION ALL SELECT * FROM test2 ORDER BY column ASC");
     }
 
     @Test
     void testPaginationInterceptorConcatOrderByFixWithWhere() {
         String actualSqlWhere = PaginationInterceptor
             .concatOrderBy("select * from test where 1 = 1 union select * from test2 where 1 = 1", page);
-        assertThat(actualSqlWhere).isEqualTo("SELECT * FROM test WHERE 1 = 1 UNION SELECT * FROM test2 WHERE 1 = 1 ORDER BY column");
+        assertThat(actualSqlWhere).isEqualTo("SELECT * FROM test WHERE 1 = 1 UNION SELECT * FROM test2 WHERE 1 = 1 ORDER BY column ASC");
 
         String actualSqlUnionAll = PaginationInterceptor
             .concatOrderBy("select * from test where 1 = 1 union all select * from test2 where 1 = 1 ", page);
-        assertThat(actualSqlUnionAll).isEqualTo("SELECT * FROM test WHERE 1 = 1 UNION ALL SELECT * FROM test2 WHERE 1 = 1 ORDER BY column");
+        assertThat(actualSqlUnionAll).isEqualTo("SELECT * FROM test WHERE 1 = 1 UNION ALL SELECT * FROM test2 WHERE 1 = 1 ORDER BY column ASC");
     }
 
     @Test
@@ -97,12 +97,12 @@ class SelectBodyToPlainSelectTest {
         String actualSql = PaginationInterceptor
             .concatOrderBy("select * from test", page);
 
-        assertThat(actualSql).isEqualTo("SELECT * FROM test ORDER BY column");
+        assertThat(actualSql).isEqualTo("SELECT * FROM test ORDER BY column ASC");
 
         String actualSqlWhere = PaginationInterceptor
             .concatOrderBy("select * from test where 1 = 1", page);
 
-        assertThat(actualSqlWhere).isEqualTo("SELECT * FROM test WHERE 1 = 1 ORDER BY column");
+        assertThat(actualSqlWhere).isEqualTo("SELECT * FROM test WHERE 1 = 1 ORDER BY column ASC");
     }
 
 }

+ 0 - 1
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/config/MybatisPlusConfig.java

@@ -35,7 +35,6 @@ import net.sf.jsqlparser.statement.delete.Delete;
 import net.sf.jsqlparser.statement.insert.Insert;
 import net.sf.jsqlparser.statement.select.SelectBody;
 import net.sf.jsqlparser.statement.update.Update;
-import org.apache.ibatis.plugin.Interceptor;
 import org.apache.ibatis.session.ExecutorType;
 import org.apache.ibatis.session.SqlSessionFactory;
 import org.apache.ibatis.type.EnumOrdinalTypeHandler;

+ 39 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/issues/genericid/GenericIdTest.java

@@ -0,0 +1,39 @@
+package com.baomidou.mybatisplus.test.h2.issues.genericid;
+
+import com.baomidou.mybatisplus.test.h2.issues.genericid.entity.LongEntity;
+import com.baomidou.mybatisplus.test.h2.issues.genericid.entity.StringEntity;
+import com.baomidou.mybatisplus.test.h2.issues.genericid.mapper.LongEntityMapper;
+import com.baomidou.mybatisplus.test.h2.issues.genericid.mapper.StringEntityMapper;
+import org.junit.jupiter.api.MethodOrderer;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestMethodOrder;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+
+/**
+ * 泛型主键问题
+ * https://gitee.com/baomidou/mybatis-plus/issues/I171CQ
+ *
+ * @author nieqiuqiu
+ * @date 2019-12-20
+ */
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+@ExtendWith(SpringExtension.class)
+@ContextConfiguration(locations = {"classpath:issues/genericid/spring.xml"})
+class GenericIdTest {
+
+    @Autowired
+    private LongEntityMapper longEntityMapper;
+
+    @Autowired
+    private StringEntityMapper stringEntityMapper;
+
+    @Test
+    void test() {
+        longEntityMapper.insert(new LongEntity("testLong"));
+        stringEntityMapper.insert(new StringEntity("testString"));
+    }
+
+}

+ 41 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/issues/genericid/MybatisPlusConfig.java

@@ -0,0 +1,41 @@
+package com.baomidou.mybatisplus.test.h2.issues.genericid;
+
+import com.baomidou.mybatisplus.core.MybatisConfiguration;
+import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
+import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
+import org.apache.ibatis.session.ExecutorType;
+import org.apache.ibatis.session.SqlSessionFactory;
+import org.apache.ibatis.type.EnumOrdinalTypeHandler;
+import org.apache.ibatis.type.JdbcType;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import javax.sql.DataSource;
+
+/**
+ * config
+ *
+ * @author nieqiuqiu
+ * @date 2019-12-20
+ */
+@Configuration
+@MapperScan("com.baomidou.mybatisplus.test.h2.issues.genericid.mapper")
+public class MybatisPlusConfig {
+
+    @Bean
+    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
+        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
+        sqlSessionFactory.setDataSource(dataSource);
+        MybatisConfiguration configuration = new MybatisConfiguration();
+        configuration.setJdbcTypeForNull(JdbcType.NULL);
+        configuration.setMapUnderscoreToCamelCase(true);
+        configuration.setDefaultExecutorType(ExecutorType.REUSE);
+        configuration.setDefaultEnumTypeHandler(EnumOrdinalTypeHandler.class);
+        sqlSessionFactory.setConfiguration(configuration);
+        PaginationInterceptor pagination = new PaginationInterceptor();
+        sqlSessionFactory.setPlugins(pagination);
+        return sqlSessionFactory.getObject();
+    }
+
+}

+ 18 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/issues/genericid/entity/LongEntity.java

@@ -0,0 +1,18 @@
+package com.baomidou.mybatisplus.test.h2.issues.genericid.entity;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+@TableName("t_i171cq_long")
+@EqualsAndHashCode(callSuper = true)
+public class LongEntity extends SuperEntity<Long> {
+
+    private String name;
+
+}

+ 18 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/issues/genericid/entity/StringEntity.java

@@ -0,0 +1,18 @@
+package com.baomidou.mybatisplus.test.h2.issues.genericid.entity;
+
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+import lombok.NoArgsConstructor;
+
+@Data
+@NoArgsConstructor
+@AllArgsConstructor
+@TableName("t_i171cq_string")
+@EqualsAndHashCode(callSuper = true)
+public class StringEntity extends SuperEntity<String> {
+
+    private String name;
+
+}

+ 15 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/issues/genericid/entity/SuperEntity.java

@@ -0,0 +1,15 @@
+package com.baomidou.mybatisplus.test.h2.issues.genericid.entity;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableId;
+import lombok.Data;
+
+import java.io.Serializable;
+
+@Data
+class SuperEntity<ID extends Serializable> {
+
+    @TableId(type = IdType.ASSIGN_ID)
+    private ID id;
+
+}

+ 14 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/issues/genericid/mapper/LongEntityMapper.java

@@ -0,0 +1,14 @@
+package com.baomidou.mybatisplus.test.h2.issues.genericid.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.test.h2.issues.genericid.entity.LongEntity;
+
+/**
+ * long mapper
+ *
+ * @author nieqiuqiu
+ * @date 2019-12-20
+ */
+public interface LongEntityMapper extends BaseMapper<LongEntity> {
+
+}

+ 14 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/issues/genericid/mapper/StringEntityMapper.java

@@ -0,0 +1,14 @@
+package com.baomidou.mybatisplus.test.h2.issues.genericid.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.test.h2.issues.genericid.entity.StringEntity;
+
+/**
+ * string mapper
+ *
+ * @author nieqiuqiu
+ * @date 2019-12-20
+ */
+public interface StringEntityMapper extends BaseMapper<StringEntity> {
+
+}

+ 14 - 0
mybatis-plus/src/test/resources/issues/genericid/spring.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:context="http://www.springframework.org/schema/context"
+       xmlns="http://www.springframework.org/schema/beans"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
+           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
+
+    <context:component-scan base-package="com.baomidou.mybatisplus.test.h2.issues.genericid"/>
+
+    <bean class="com.baomidou.mybatisplus.test.h2.config.DBConfig">
+        <property name="locationPattern" value="classpath:/issues/genericid/sql/*.sql"/>
+    </bean>
+
+</beans>

+ 12 - 0
mybatis-plus/src/test/resources/issues/genericid/sql/init.ddl.sql

@@ -0,0 +1,12 @@
+CREATE TABLE IF NOT EXISTS  t_i171cq_long (
+	id BIGINT(20) NOT NULL,
+	name VARCHAR(30) NULL DEFAULT NULL ,
+	PRIMARY KEY (id)
+);
+
+CREATE TABLE IF NOT EXISTS  t_i171cq_string (
+	id varchar NOT NULL,
+	name VARCHAR(30) NULL DEFAULT NULL ,
+	PRIMARY KEY (id)
+);
+