Browse Source

逻辑删除 - 自定义sql也带上逻辑删除条件 - testcase: to be completed

yuxiaobin 7 years ago
parent
commit
08728df86b

+ 65 - 0
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/test/h2/H2LogicDeleteSqlParserTest.java

@@ -0,0 +1,65 @@
+package com.baomidou.mybatisplus.extension.test.h2;
+
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import javax.sql.DataSource;
+
+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.H2Test;
+import com.baomidou.mybatisplus.extension.test.h2.config.ServiceConfig;
+import com.baomidou.mybatisplus.extension.test.h2.entity.mapper.H2PersonLogicDeleteMapper;
+import com.baomidou.mybatisplus.extension.test.h2.entity.persistent.H2PersonLogicDelete;
+
+/**
+ * <p>
+ * 逻辑删除 - 自定义sql也带上逻辑删除标识
+ * </p>
+ *
+ * @author yuxiaobin
+ * @date 2017/6/15
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(classes = {ServiceConfig.class})
+public class H2LogicDeleteSqlParserTest extends H2Test {
+
+    @BeforeClass
+    public static void init() throws SQLException, IOException {
+        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:h2/spring-test-h2-mvc-camel.xml");
+        DataSource ds = (DataSource) context.getBean("dataSource");
+        try (Connection conn = ds.getConnection()) {
+            initData(conn, "person.ddl.sql", "person.insert.sql", "h2person");
+            initData(conn, "addr.ddl.sql", "addr.insert.sql", "h2address");
+        }
+    }
+
+    @Autowired
+    private H2PersonLogicDeleteMapper personMapper;
+
+    @Test
+    public void testInsert() {
+        H2PersonLogicDelete person = new H2PersonLogicDelete();
+        person.setName("abc").setTestType(1);
+        personMapper.insert(person);
+        Long id = person.getId();
+        person = personMapper.selectById(id);
+        System.out.println(person);
+    }
+
+
+    @Test
+    public void testMySelect(){
+        personMapper.getAddrListByUserId(1L);
+        //TODO: 然并无
+    }
+
+}

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

@@ -0,0 +1,88 @@
+package com.baomidou.mybatisplus.extension.test.h2.entity.mapper;
+
+import java.util.List;
+import java.util.Map;
+
+import org.apache.ibatis.annotations.Insert;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
+import org.apache.ibatis.annotations.Update;
+
+import com.baomidou.mybatisplus.annotation.SqlParser;
+import com.baomidou.mybatisplus.core.pagination.Page;
+import com.baomidou.mybatisplus.extension.test.h2.entity.persistent.H2Addr;
+import com.baomidou.mybatisplus.extension.test.h2.entity.persistent.H2Person;
+import com.baomidou.mybatisplus.extension.test.h2.entity.persistent.H2PersonLogicDelete;
+
+
+/**
+ * <p>
+ * 这里继承自定义父类 SuperMapper
+ * </p>
+ *
+ * @author Caratacus
+ * @date 2017/4/1
+ */
+public interface H2PersonLogicDeleteMapper extends SuperMapper<H2PersonLogicDelete> {
+
+    @Select(
+            "select a.addr_id as addrId, a.addr_name as addrName from h2address a" +
+                    " join h2person u on u.testId=a.test_id and u.testId=#{userId}"
+    )
+    @SqlParser(filter = true)
+    List<H2Addr> getAddrListByUserId(@Param("userId") Long userId);
+
+    @Select(
+            "select a.addr_id as addrId, a.addr_name as addrName from h2address a" +
+                " join h2person u on u.testId=a.test_id and u.testId=#{userId}"
+    )
+    List<H2Addr> getAddrListByUserIdPage(@Param("userId") Long userId, Page<H2Addr> page);
+
+    @Insert(
+            "insert into h2person(name,version) values(#{name},#{version})"
+    )
+    int myInsertWithNameVersion(@Param("name") String name, @Param("version") int version);
+
+    @Update(
+            "update h2person set name=#{name} where testId=#{id}"
+    )
+    int myUpdateWithNameId(@Param("id") Long id, @Param("name") String name);
+
+
+    @Insert(
+            "insert into h2person(name,version) values( #{user1.name}, #{user1.version})"
+    )
+    int myInsertWithParam(@Param("user1") H2Person user1);
+
+    @Insert(
+            "insert into h2person(name,version) values( #{name}, #{version})"
+    )
+    int myInsertWithoutParam(H2Person user1);
+
+
+    @Select(" select testId as id, power(#{ageFrom},2), 'abc?zhazha', CAST(#{nameParam} AS VARCHAR) as name " +
+            " from h2person " +
+            " where age>#{ageFrom} and age<#{ageTo} ")
+    List<H2Person> selectUserWithParamInSelectStatememt(Map<String, Object> param);
+
+    @Select(" select testId as id, power(#{ageFrom},2), 'abc?zhazha', CAST(#{nameParam} AS VARCHAR) as name " +
+            " from h2person " +
+            " where age>#{ageFrom} and age<#{ageTo} ")
+    List<H2Person> selectUserWithParamInSelectStatememt4Page(Map<String, Object> param, Page<H2Person> page);
+
+    @Select(" select testId as id, power(${ageFrom},2) as age, '${nameParam}' as name " +
+            " from h2person " +
+            " where age>#{ageFrom} and age<#{ageTo} ")
+    List<H2Person> selectUserWithDollarParamInSelectStatememt4Page(Map<String, Object> param, Page<H2Person> page);
+
+
+    @Select("select count(1) from (" +
+            "select testId as id, CAST(#{nameParam} AS VARCHAR) as name" +
+            " from h2person " +
+            " where age>#{ageFrom} and age<#{ageTo} " +
+            ") a")
+    int selectCountWithParamInSelectItems(Map<String, Object> param);
+
+    @Select("select * from h2person")
+    List<Map> mySelectMaps();
+}

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

@@ -0,0 +1,104 @@
+/**
+ * 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.math.BigDecimal;
+import java.util.Date;
+
+import com.baomidou.mybatisplus.annotation.FieldStrategy;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableLogic;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.baomidou.mybatisplus.extension.test.h2.entity.SuperEntityCamel;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+/**
+ * <p>
+ * 测试用户类
+ * </p>
+ *
+ * @author hubin sjy
+ */
+/* 表名 value 注解【 驼峰命名可无 】, resultMap 注解测试【 映射 xml 的 resultMap 内容 】 */
+@Data
+@Accessors(chain = true)
+@TableName("h2person")
+public class H2PersonLogicDelete extends SuperEntityCamel {
+
+    /* 测试忽略验证 */
+    private String name;
+
+    private Integer age;
+
+    /*BigDecimal 测试*/
+    private BigDecimal price;
+
+    /* 测试下划线字段命名类型, 字段填充 */
+    @TableField(value = "testType", strategy = FieldStrategy.IGNORED)
+    private Integer testType;
+
+    private String desc;
+
+    @TableField
+	private Date testDate;
+
+    @TableLogic(value = "0", delval = "-1")
+    private Integer version;
+
+
+    public H2PersonLogicDelete() {
+
+    }
+
+    public H2PersonLogicDelete(String name) {
+        this.name = name;
+    }
+
+    public H2PersonLogicDelete(Integer testType) {
+        this.testType = testType;
+    }
+
+    public H2PersonLogicDelete(String name, Integer age) {
+        this.name = name;
+        this.age = age;
+    }
+
+    public H2PersonLogicDelete(Long id, String name) {
+        this.setId(id);
+        this.name = name;
+    }
+
+    public H2PersonLogicDelete(Long id, Integer age) {
+        this.setId(id);
+        this.age = age;
+    }
+
+    public H2PersonLogicDelete(Long id, String name, Integer age, Integer testType) {
+        this.setId(id);
+        this.name = name;
+        this.age = age;
+        this.testType = testType;
+    }
+
+    public H2PersonLogicDelete(String name, Integer age, Integer testType) {
+        this.name = name;
+        this.age = age;
+        this.testType = testType;
+    }
+
+}