Browse Source

新增 uuid 主键测试用例

jobob 8 years ago
parent
commit
e514cb8cb2

+ 62 - 0
src/test/java/com/baomidou/mybatisplus/test/h2/H2uuidTest.java

@@ -0,0 +1,62 @@
+package com.baomidou.mybatisplus.test.h2;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.List;
+
+import javax.sql.DataSource;
+
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import com.baomidou.mybatisplus.plugins.Page;
+import com.baomidou.mybatisplus.test.h2.entity.mapper.H2UserMapper;
+import com.baomidou.mybatisplus.test.h2.entity.mapper.H2uuidMapper;
+import com.baomidou.mybatisplus.test.h2.entity.persistent.H2Addr;
+import com.baomidou.mybatisplus.test.h2.entity.persistent.H2uuid;
+
+/**
+ * <p>
+ * Mybatis Plus H2 Junit Test
+ * </p>
+ *
+ * @author Caratacus
+ * @date 2017/4/1
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(locations = {"classpath:h2/spring-test-h2.xml"})
+public class H2uuidTest extends H2Test {
+
+    @Autowired
+    private H2uuidMapper uuidMapper;
+
+    @BeforeClass
+    public static void initDB() throws SQLException, IOException {
+        @SuppressWarnings("resource")
+        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:h2/spring-test-h2.xml");
+        DataSource ds = (DataSource) context.getBean("dataSource");
+        try (Connection conn = ds.getConnection()) {
+            Statement stmt = conn.createStatement();
+            stmt.execute(readFile("uuid.sql"));
+            stmt.execute("truncate table h2uuid");
+            conn.commit();
+        }
+    }
+
+    @Test
+    public void testUuid() {
+        H2uuid h2uuid = new H2uuid("3");
+        Assert.assertEquals(1, uuidMapper.insert(h2uuid).intValue());
+        Assert.assertTrue(h2uuid.getId().length() == 32);
+    }
+
+}

+ 16 - 0
src/test/java/com/baomidou/mybatisplus/test/h2/entity/mapper/H2uuidMapper.java

@@ -0,0 +1,16 @@
+package com.baomidou.mybatisplus.test.h2.entity.mapper;
+
+import com.baomidou.mybatisplus.test.h2.entity.SuperMapper;
+import com.baomidou.mybatisplus.test.h2.entity.persistent.H2uuid;
+
+/**
+ * <p>
+ * 测试 uuid 主键
+ * </p>
+ *
+ * @author hubin
+ * @date 2017-06-28
+ */
+public interface H2uuidMapper extends SuperMapper<H2uuid> {
+
+}

+ 61 - 0
src/test/java/com/baomidou/mybatisplus/test/h2/entity/persistent/H2uuid.java

@@ -0,0 +1,61 @@
+/**
+ * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.baomidou.mybatisplus.test.h2.entity.persistent;
+
+import java.io.Serializable;
+import java.util.Date;
+
+import com.baomidou.mybatisplus.annotations.TableField;
+import com.baomidou.mybatisplus.annotations.TableId;
+import com.baomidou.mybatisplus.annotations.TableName;
+import com.baomidou.mybatisplus.enums.IdType;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * <p>
+ * 测试 UUID 主键
+ * </p>
+ *
+ * @author hubin
+ * @Date 2017-06-28
+ */
+@Data
+@Accessors(chain = true)
+@TableName("h2uuid")
+public class H2uuid implements Serializable {
+
+    // 静态属性会自动忽略
+    private static final long serialVersionUID = 1L;
+
+    /**
+     * 主键
+     */
+    @TableId(type = IdType.UUID)
+    private String id;
+
+    private String type;
+
+    public H2uuid() {
+
+    }
+
+    public H2uuid(String type) {
+        this.type = type;
+    }
+
+}

+ 5 - 0
src/test/resources/h2/uuid.sql

@@ -0,0 +1,5 @@
+CREATE TABLE IF NOT EXISTS  h2uuid (
+	id VARCHAR(32) NOT NULL ,
+	type VARCHAR(3) NULL DEFAULT NULL ,
+	PRIMARY KEY (id)
+)