Browse Source

#IGKLA activeRecord模式下,乐观锁/自动填充/逻辑删除 - testcase

yuxiaobin 7 years ago
parent
commit
98b0091e75

+ 100 - 0
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/test/h2/H2UserVersionIntWithARTest.java

@@ -0,0 +1,100 @@
+package com.baomidou.mybatisplus.extension.test.h2;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Date;
+
+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.extension.test.h2.base.AbstractH2UserTest;
+import com.baomidou.mybatisplus.extension.test.h2.entity.persistent.H2UserLogicDeleteWithAR;
+import com.baomidou.mybatisplus.extension.test.h2.entity.persistent.H2UserVersionIntWithAR;
+import com.baomidou.mybatisplus.extension.test.h2.service.IH2UserLogicDeleteService;
+import com.baomidou.mybatisplus.extension.test.h2.service.IH2UserVersionIntWithARService;
+
+/**
+ * <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 H2UserVersionIntWithARTest extends AbstractH2UserTest {
+
+    @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()) {
+            initData(conn);
+        }
+    }
+
+    @Autowired
+    IH2UserVersionIntWithARService arService;
+
+    @Autowired
+    private IH2UserLogicDeleteService logicDeleteService;
+
+    @Test
+    public void testOptLock4AR() {
+        H2UserVersionIntWithAR user = new H2UserVersionIntWithAR();
+        user.setName("ARTest").setAge(18).setPrice(BigDecimal.TEN).setTestDate(new Date()).setVersion(1);
+        user.insert();
+        Long id = user.getId();
+        H2UserVersionIntWithAR userDB = user.selectById(id);
+        Assert.assertNotNull("should insert success", userDB);
+        Assert.assertNotNull("autofill should work when do insert", userDB.getTestType());
+        Assert.assertNull("lastUpdateDt should be null", userDB.getLastUpdatedDt());
+
+        Assert.assertEquals("init version=1", 1, userDB.getVersion().intValue());
+        String updateName = "ARTestUpdate";
+        user.setName(updateName).updateById();
+
+        userDB = user.selectById(id);
+        Assert.assertNotNull(userDB);
+        Assert.assertEquals("version should updated", 2, userDB.getVersion().intValue());
+        Assert.assertNotNull("autofill should work when do update", userDB.getLastUpdatedDt());
+    }
+
+
+    @Test
+    public void testLogicDelete() {
+        H2UserLogicDeleteWithAR user = new H2UserLogicDeleteWithAR();
+        user.setName("ARLogicDelete").setAge(1).setVersion(1).setTestDate(new Date()).insert();
+        Long id = user.getId();
+        user = user.selectById(id);
+        Assert.assertNotNull("should have record for id=" + id, user);
+        /**
+         * 测试service的逻辑删除
+         */
+        logicDeleteService.deleteById(id);
+        Assert.assertNull("should logic deleted", logicDeleteService.selectById(id));
+        Assert.assertNotNull("should logic deleted, not real delete", arService.selectByIdWithoutLogicDeleteLimit(id));
+        arService.updateLogicDeletedRecord(id);
+        /**
+         * 测试AR的逻辑删除
+         */
+        user.deleteById();
+        Assert.assertNull("should be logic deleted for id=" + id, user.selectById(id));
+        Assert.assertNotNull("record is logic deleted, should not null", arService.selectByIdWithoutLogicDeleteLimit(id));
+        arService.updateLogicDeletedRecord(id);
+    }
+
+}

+ 15 - 0
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/test/h2/entity/mapper/H2UserLogicDeleteWithARMapper.java

@@ -0,0 +1,15 @@
+package com.baomidou.mybatisplus.extension.test.h2.entity.mapper;
+
+import com.baomidou.mybatisplus.extension.test.h2.entity.persistent.H2UserLogicDeleteWithAR;
+
+/**
+ * <p>
+ * 这里继承自定义父类 SuperMapper
+ * </p>
+ *
+ * @author Caratacus
+ * @date 2017/4/1
+ */
+public interface H2UserLogicDeleteWithARMapper extends SuperMapper<H2UserLogicDeleteWithAR> {
+
+}

+ 28 - 0
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/test/h2/entity/mapper/H2UserVersionIntWithARMapper.java

@@ -0,0 +1,28 @@
+package com.baomidou.mybatisplus.extension.test.h2.entity.mapper;
+
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Update;
+
+import com.baomidou.mybatisplus.extension.test.h2.entity.persistent.H2UserVersionIntWithAR;
+
+/**
+ * <p>
+ * 这里继承自定义父类 SuperMapper
+ * </p>
+ *
+ * @author Caratacus
+ * @date 2017/4/1
+ */
+public interface H2UserVersionIntWithARMapper extends SuperMapper<H2UserVersionIntWithAR> {
+
+    @Select(
+        "select test_id as id, name, age, version, test_type as testType from h2user where test_id=#{id}"
+    )
+    public H2UserVersionIntWithAR selectByIdWithoutLogicDeleteLimit(@Param("id") Long id);
+
+    @Update(
+        "update h2user set version=1 where test_id=#{id}"
+    )
+    public void updateLogicDeletedRecord(@Param("id") Long id);
+}

+ 116 - 0
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/test/h2/entity/persistent/H2UserLogicDeleteWithAR.java

@@ -0,0 +1,116 @@
+/*
+ * 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.extension.test.h2.entity.persistent;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.Date;
+
+import com.baomidou.mybatisplus.annotation.FieldFill;
+import com.baomidou.mybatisplus.annotation.FieldStrategy;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.baomidou.mybatisplus.extension.activerecord.Model;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * <p>
+ * 测试用户类
+ * </p>
+ *
+ * @author hubin sjy
+ */
+/* 表名 value 注解【 驼峰命名可无 】, resultMap 注解测试【 映射 xml 的 resultMap 内容 】 */
+@Data
+@Accessors(chain = true)
+@TableName("h2user" )
+public class H2UserLogicDeleteWithAR extends Model<H2UserLogicDeleteWithAR> {
+
+    @TableId(value = "test_id" )
+    private Long id;
+
+    @TableField(value = "last_updated_dt" , fill = FieldFill.UPDATE)
+    private Date lastUpdatedDt;
+
+    private String name;
+
+    private Integer age;
+
+    /*BigDecimal 测试*/
+    private BigDecimal price;
+
+    /* 测试下划线字段命名类型, 字段填充 */
+    @TableField(value = "test_type" , strategy = FieldStrategy.IGNORED)
+    private Integer testType;
+
+    private String desc;
+
+    @TableField
+    private Date testDate;
+
+    @TableLogic
+    private Integer version;
+
+
+    public H2UserLogicDeleteWithAR() {
+
+    }
+
+    public H2UserLogicDeleteWithAR(String name) {
+        this.name = name;
+    }
+
+    public H2UserLogicDeleteWithAR(Integer testType) {
+        this.testType = testType;
+    }
+
+    public H2UserLogicDeleteWithAR(String name, Integer age) {
+        this.name = name;
+        this.age = age;
+    }
+
+    public H2UserLogicDeleteWithAR(Long id, String name) {
+        this.setId(id);
+        this.name = name;
+    }
+
+    public H2UserLogicDeleteWithAR(Long id, Integer age) {
+        this.setId(id);
+        this.age = age;
+    }
+
+    public H2UserLogicDeleteWithAR(Long id, String name, Integer age, Integer testType) {
+        this.setId(id);
+        this.name = name;
+        this.age = age;
+        this.testType = testType;
+    }
+
+    public H2UserLogicDeleteWithAR(String name, Integer age, Integer testType) {
+        this.name = name;
+        this.age = age;
+        this.testType = testType;
+    }
+
+    @Override
+    protected Serializable pkVal() {
+        return id;
+    }
+}

+ 116 - 0
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/test/h2/entity/persistent/H2UserVersionIntWithAR.java

@@ -0,0 +1,116 @@
+/*
+ * 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.extension.test.h2.entity.persistent;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.Date;
+
+import com.baomidou.mybatisplus.annotation.FieldFill;
+import com.baomidou.mybatisplus.annotation.FieldStrategy;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.baomidou.mybatisplus.annotation.Version;
+import com.baomidou.mybatisplus.extension.activerecord.Model;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * <p>
+ * 测试用户类
+ * </p>
+ *
+ * @author hubin sjy
+ */
+/* 表名 value 注解【 驼峰命名可无 】, resultMap 注解测试【 映射 xml 的 resultMap 内容 】 */
+@Data
+@Accessors(chain = true)
+@TableName("h2user" )
+public class H2UserVersionIntWithAR extends Model<H2UserVersionIntWithAR> {
+
+    @TableId(value = "test_id" )
+    private Long id;
+
+    @TableField(value = "last_updated_dt" , fill = FieldFill.UPDATE)
+    private Date lastUpdatedDt;
+
+    private String name;
+
+    private Integer age;
+
+    /*BigDecimal 测试*/
+    private BigDecimal price;
+
+    /* 测试下划线字段命名类型, 字段填充 */
+    @TableField(value = "test_type" , strategy = FieldStrategy.IGNORED)
+    private Integer testType;
+
+    private String desc;
+
+    @TableField
+    private Date testDate;
+
+    @Version
+    private Integer version;
+
+
+    public H2UserVersionIntWithAR() {
+
+    }
+
+    public H2UserVersionIntWithAR(String name) {
+        this.name = name;
+    }
+
+    public H2UserVersionIntWithAR(Integer testType) {
+        this.testType = testType;
+    }
+
+    public H2UserVersionIntWithAR(String name, Integer age) {
+        this.name = name;
+        this.age = age;
+    }
+
+    public H2UserVersionIntWithAR(Long id, String name) {
+        this.setId(id);
+        this.name = name;
+    }
+
+    public H2UserVersionIntWithAR(Long id, Integer age) {
+        this.setId(id);
+        this.age = age;
+    }
+
+    public H2UserVersionIntWithAR(Long id, String name, Integer age, Integer testType) {
+        this.setId(id);
+        this.name = name;
+        this.age = age;
+        this.testType = testType;
+    }
+
+    public H2UserVersionIntWithAR(String name, Integer age, Integer testType) {
+        this.name = name;
+        this.age = age;
+        this.testType = testType;
+    }
+
+    @Override
+    protected Serializable pkVal() {
+        return id;
+    }
+}

+ 19 - 0
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/test/h2/service/IH2UserVersionIntWithARService.java

@@ -0,0 +1,19 @@
+package com.baomidou.mybatisplus.extension.test.h2.service;
+
+import com.baomidou.mybatisplus.extension.test.h2.entity.persistent.H2UserVersionIntWithAR;
+
+/**
+ * <p>
+ *   AR模式的service
+ * </p>
+ *
+ * @author yuxiaobin
+ * @date 2018/3/15
+ */
+public interface IH2UserVersionIntWithARService {
+
+    public void updateLogicDeletedRecord(Long id);
+
+    public H2UserVersionIntWithAR selectByIdWithoutLogicDeleteLimit(Long id);
+
+}

+ 34 - 0
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/test/h2/service/impl/H2UserVersionIntWithARServiceImpl.java

@@ -0,0 +1,34 @@
+package com.baomidou.mybatisplus.extension.test.h2.service.impl;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import com.baomidou.mybatisplus.extension.test.h2.entity.mapper.H2UserVersionIntWithARMapper;
+import com.baomidou.mybatisplus.extension.test.h2.entity.persistent.H2UserVersionIntWithAR;
+import com.baomidou.mybatisplus.extension.test.h2.service.IH2UserVersionIntWithARService;
+
+/**
+ * <p>
+ * </p>
+ *
+ * @author yuxiaobin
+ * @date 2018/3/15
+ */
+@Service
+public class H2UserVersionIntWithARServiceImpl implements IH2UserVersionIntWithARService {
+
+    @Autowired
+    H2UserVersionIntWithARMapper h2UserVersionIntWithARMapper;
+
+    @Override
+    @Transactional
+    public void updateLogicDeletedRecord(Long id) {
+        h2UserVersionIntWithARMapper.updateLogicDeletedRecord(id);
+    }
+
+    @Override
+    public H2UserVersionIntWithAR selectByIdWithoutLogicDeleteLimit(Long id) {
+        return h2UserVersionIntWithARMapper.selectByIdWithoutLogicDeleteLimit(id);
+    }
+}