Переглянути джерело

驼峰命名字段 - 测试用例

yuxiaobin 7 роки тому
батько
коміт
c20b4b073c

+ 61 - 0
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/test/h2/H2PersonCamelTest.java

@@ -0,0 +1,61 @@
+package com.baomidou.mybatisplus.extension.test.h2;
+
+import java.io.IOException;
+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.mapper.H2PersonCamelMapper;
+import com.baomidou.mybatisplus.extension.test.h2.entity.persistent.H2Person;
+
+
+/**
+ * <p>
+ * Mybatis Plus H2 Junit Test
+ * </p>
+ *
+ * @author Caratacus
+ * @date 2017/4/1
+ */
+@RunWith(SpringJUnit4ClassRunner.class)
+@ContextConfiguration(locations = {"classpath:h2/spring-test-h2-mvc-camel.xml"})
+public class H2PersonCamelTest extends AbstractH2UserTest {
+
+    @Autowired
+    H2PersonCamelMapper personCamelMapper;
+
+    @BeforeClass
+    public static void initDB() throws SQLException, IOException {
+        @SuppressWarnings("resource")
+        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");
+        }
+    }
+
+    @Test
+    public void testInsert() {
+        H2Person person = new H2Person();
+        person.setName("person");
+        person.setTestType(1);
+        person.setLastUpdatedDt(new Date());
+        Assert.assertEquals(1, personCamelMapper.insert(person).intValue());
+        Long id = person.getId();
+        Assert.assertNotNull(personCamelMapper.selectById(id));
+    }
+
+}

+ 13 - 3
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/test/h2/base/H2Test.java

@@ -36,11 +36,21 @@ public class H2Test {
     }
 
     public static void initData(Connection conn) throws SQLException, IOException {
-        String createTableSql = readFile("user.ddl.sql" );
+//        String createTableSql = readFile("user.ddl.sql");
+//        Statement stmt = conn.createStatement();
+//        stmt.execute(createTableSql);
+//        stmt.execute("truncate table h2user");
+//        executeSql(stmt, "user.insert.sql");
+//        conn.commit();
+        initData(conn, "user.ddl.sql", "user.insert.sql", "h2user");
+    }
+
+    public static void initData(Connection conn, String ddlFileName, String insertFileName, String tableName) throws SQLException, IOException {
+        String createTableSql = readFile(ddlFileName);
         Statement stmt = conn.createStatement();
         stmt.execute(createTableSql);
-        stmt.execute("truncate table h2user" );
-        executeSql(stmt, "user.insert.sql" );
+        stmt.execute("truncate table "+tableName);
+        executeSql(stmt, insertFileName);
         conn.commit();
     }
 

+ 30 - 0
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/test/h2/entity/SuSuperEntityCamel.java

@@ -0,0 +1,30 @@
+package com.baomidou.mybatisplus.extension.test.h2.entity;
+
+import java.util.Date;
+
+import com.baomidou.mybatisplus.annotation.FieldFill;
+import com.baomidou.mybatisplus.annotation.TableField;
+
+
+/**
+ * <p>
+ * 多层集成测试
+ * github #170
+ * </p>
+ *
+ * @author yuxiaobin
+ * @date 2017/12/7
+ */
+public abstract class SuSuperEntityCamel {
+
+    @TableField(value = "lastUpdatedDt", fill = FieldFill.UPDATE)
+    private Date lastUpdatedDt;
+
+    public Date getLastUpdatedDt() {
+        return lastUpdatedDt;
+    }
+
+    public void setLastUpdatedDt(Date lastUpdatedDt) {
+        this.lastUpdatedDt = lastUpdatedDt;
+    }
+}

+ 45 - 0
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/test/h2/entity/SuperEntityCamel.java

@@ -0,0 +1,45 @@
+/**
+ * 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;
+
+import java.io.Serializable;
+
+import com.baomidou.mybatisplus.annotation.TableId;
+
+
+/**
+ * <p>
+ * 测试父类情况
+ * </p>
+ *
+ * @author hubin
+ * @Date 2016-06-26
+ */
+public class SuperEntityCamel extends SuSuperEntityCamel implements Serializable {
+
+    /* 主键ID 注解,value 字段名,type 用户输入ID */
+    @TableId(value = "testId")
+    private Long id;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+}
+

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

@@ -0,0 +1,85 @@
+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.core.pagination.Page;
+import com.baomidou.mybatisplus.extension.test.h2.entity.persistent.H2Addr;
+import com.baomidou.mybatisplus.extension.test.h2.entity.persistent.H2Person;
+
+
+/**
+ * <p>
+ * 这里继承自定义父类 SuperMapper
+ * </p>
+ *
+ * @author Caratacus
+ * @date 2017/4/1
+ */
+public interface H2PersonCamelMapper extends SuperMapper<H2Person> {
+
+    @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> 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/H2Person.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.TableName;
+import com.baomidou.mybatisplus.annotation.Version;
+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 H2Person 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;
+
+    @Version
+    private Integer version;
+
+
+    public H2Person() {
+
+    }
+
+    public H2Person(String name) {
+        this.name = name;
+    }
+
+    public H2Person(Integer testType) {
+        this.testType = testType;
+    }
+
+    public H2Person(String name, Integer age) {
+        this.name = name;
+        this.age = age;
+    }
+
+    public H2Person(Long id, String name) {
+        this.setId(id);
+        this.name = name;
+    }
+
+    public H2Person(Long id, Integer age) {
+        this.setId(id);
+        this.age = age;
+    }
+
+    public H2Person(Long id, String name, Integer age, Integer testType) {
+        this.setId(id);
+        this.name = name;
+        this.age = age;
+        this.testType = testType;
+    }
+
+    public H2Person(String name, Integer age, Integer testType) {
+        this.name = name;
+        this.age = age;
+        this.testType = testType;
+    }
+
+}

+ 13 - 0
mybatis-plus-extension/src/test/resources/h2/person.ddl.sql

@@ -0,0 +1,13 @@
+CREATE TABLE IF NOT EXISTS  h2person (
+	testId BIGINT(20) NOT NULL AUTO_INCREMENT,
+	name VARCHAR(30) NULL DEFAULT NULL ,
+	age INT(11) NULL DEFAULT NULL ,
+	testType INT(11) NULL ,
+	testDate DATETIME NULL DEFAULT NULL,
+	price DECIMAL(10,2) NULL DEFAULT NULL,
+	desc VARCHAR(30) NULL DEFAULT NULL ,
+	version INT(5) NULL DEFAULT NULL,
+	lastUpdatedDt TIMESTAMP NULL,
+	PRIMARY KEY (testId)
+)
+

+ 5 - 0
mybatis-plus-extension/src/test/resources/h2/person.insert.sql

@@ -0,0 +1,5 @@
+insert into h2person(testId, name, testDate, age, price, testType, version) values (101,'Tomcat', '2017-1-1 1:1:1', 3, 9.99, 1, 1),
+insert into h2person(testId, name, testDate, age, price, testType, version) values (102,'Jerry', '2017-3-1 1:1:1', 2, 19.99, 2, 2);
+insert into h2person(testId, name, testDate, age, price, testType, version) values (103,'Bob', '2017-4-1 1:1:1', 13, 99.99, 3, 3);
+insert into h2person(testId, name, testDate, age, price, testType, version) values (104,'Joe', '2017-2-1 1:1:1', 18, 1.99, 1, 4);
+insert into h2person(testId, name, testDate, age, price, testType, version) values (105,'Tony', '2017-2-1 1:1:1', 18, 1.99, 1, 5);

+ 79 - 0
mybatis-plus-extension/src/test/resources/h2/spring-test-h2-mvc-camel.xml

@@ -0,0 +1,79 @@
+<?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"
+       xmlns:util="http://www.springframework.org/schema/util"
+       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
+           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
+
+    <context:component-scan base-package="com.baomidou.mybatisplus.extension.test.h2.service"/>
+
+    <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
+        <property name="driverClass" value="org.h2.Driver"/>
+        <property name="url" value="jdbc:h2:mem:test;MODE=mysql;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"/>
+        <property name="username" value="sa"/>
+        <property name="password" value=""/>
+    </bean>
+
+    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
+        <property name="dataSource" ref="dataSource"/>
+        <property name="typeAliasesPackage" value="com.baomidou.mybatisplus.test.h2.entity"/>
+        <property name="configuration" ref="mybatisConfig"/>
+        <!-- MP 全局配置注入 -->
+        <property name="globalConfig" ref="globalConfig"/>
+        <property name="plugins">
+            <array>
+                <!-- 分页插件配置 -->
+                <bean id="paginationInterceptor"
+                      class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"/>
+                <bean id="optimisticLockerInterceptor"
+                      class="com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor">
+                </bean>
+                <bean id="performanceInterceptor"
+                      class="com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor"/>
+            </array>
+        </property>
+    </bean>
+
+	<bean id="mybatisMapWrapperFactory" class="com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory"/>
+
+    <bean id="mybatisConfig" class="com.baomidou.mybatisplus.core.MybatisConfiguration">
+        <property name="mapUnderscoreToCamelCase" value="true"/>
+        <property name="jdbcTypeForNull">
+            <util:constant static-field="org.apache.ibatis.type.JdbcType.NULL"/>
+        </property>
+		<property name="objectWrapperFactory" ref="mybatisMapWrapperFactory"/>
+    </bean>
+
+    <bean id="globalConfig" class="com.baomidou.mybatisplus.core.metadata.GlobalConfiguration">
+        <!-- 逻辑删除 定义下面3个参数-->
+        <property name="sqlInjector" ref="logicSqlInjector"/>
+        <property name="logicDeleteValue" value="-1"/>
+        <property name="logicNotDeleteValue" value="1"/>
+        <!-- 全局ID类型: 0, "数据库ID自增", 1, "用户输入ID", 2, "全局唯一ID", 3, "全局唯一ID"-->
+        <property name="idType" value="2"/>
+        <property name="dbColumnUnderline" value="false"/>
+        <!-- 2.1-gamma+ 数据库自动识别,无需配置数据库类型
+        <property name="dbType" value="mysql" />
+        -->
+        <!--主键Sequence-->
+        <!--<property name="keyGenerator" ref="keyGenerator"/>-->
+        <!-- 公共字段填充处理器 -->
+        <property name="metaObjectHandler" ref="myMetaObjectHandler"/>
+        <!--数据库关键字转义符,'desc', "desc" 2.1-gamma+不需要制定-->
+        <!--<property name="identifierQuote" value="'" />-->
+    </bean>
+
+    <!-- 配置oracle主键Sequence, 其他类型数据库,请配置相应的类型-->
+    <!--<bean id="keyGenerator" class="com.baomidou.mybatisplus.incrementer.OracleKeyGenerator"/>-->
+
+    <!-- 自定义处理器 -->
+    <bean id="myMetaObjectHandler" class="com.baomidou.mybatisplus.extension.test.h2.H2MetaObjectHandler"/>
+    <bean id="logicSqlInjector" class="com.baomidou.mybatisplus.extension.injector.LogicSqlInjector"/>
+
+    <!-- 配置mybatis 扫描mapper接口的路径-->
+    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
+        <property name="basePackage" value="com.baomidou.mybatisplus.extension.test.h2.entity.mapper"/>
+    </bean>
+</beans>