Przeglądaj źródła

Merge branch 'dev' of https://git.oschina.net/baomidou/mybatis-plus into dev

bj_renyong 8 lat temu
rodzic
commit
cc7afba585

+ 15 - 0
mybatis-plus/pom.xml

@@ -53,6 +53,7 @@
 		<mybatis-ehcache.version>1.1.0</mybatis-ehcache.version>
 		<junit.version>4.12</junit.version>
 		<velocity.version>1.7</velocity.version>
+		<h2.version>1.4.194</h2.version>
 	</properties>
 
 	<dependencies>
@@ -217,6 +218,12 @@
 			<version>${postgresql.version}</version>
 			<scope>test</scope>
 		</dependency>
+		<dependency>
+			<groupId>com.h2database</groupId>
+			<artifactId>h2</artifactId>
+			<version>${h2.version}</version>
+			<scope>test</scope>
+		</dependency>
 		<!-- test end -->
 	</dependencies>
 
@@ -236,6 +243,14 @@
 					</execution>
 				</executions>
 			</plugin>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<configuration>
+					<source>1.7</source>
+					<target>1.7</target>
+				</configuration>
+			</plugin>
 		</plugins>
 	</build>
 	<!-- 发版时注释END -->

+ 146 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/H2UserTest.java

@@ -0,0 +1,146 @@
+package com.baomidou.mybatisplus.test.h2;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.math.BigDecimal;
+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.mapper.EntityWrapper;
+import com.baomidou.mybatisplus.plugins.Page;
+import com.baomidou.mybatisplus.test.h2.entity.persistent.H2User;
+import com.baomidou.mybatisplus.test.h2.entity.service.IH2UserService;
+
+/**
+ * <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 H2UserTest {
+
+    @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();) {
+            String createTableSql = readFile("user.ddl.sql");
+            Statement stmt = conn.createStatement();
+            stmt.execute(createTableSql);
+            stmt.execute("truncate table h2user");
+            insertUsers(stmt);
+            conn.commit();
+        }
+    }
+
+    private static void insertUsers(Statement stmt) throws SQLException, IOException{
+        String filename = "user.insert.sql";
+        String filePath = H2UserTest.class.getClassLoader().getResource("").getPath()+"/h2/"+filename;
+        try (
+                BufferedReader reader = new BufferedReader(new FileReader(filePath));
+        ) {
+            String line = null;
+            while ((line = reader.readLine()) != null){
+                stmt.execute(line.replace(";", ""));
+            }
+        }
+    }
+
+    private static String readFile(String filename) {
+        StringBuilder builder = new StringBuilder();
+        String filePath = H2UserTest.class.getClassLoader().getResource("").getPath()+"/h2/"+filename;
+        try (
+                BufferedReader reader = new BufferedReader(new FileReader(filePath));
+        ) {
+            String line = null;
+            while ((line = reader.readLine()) != null)
+                builder.append(line).append(" ");
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+        return builder.toString();
+    }
+
+    @Autowired
+    private IH2UserService userService;
+
+    @Test
+    public void testInsert(){
+        H2User user = new H2User();
+        user.setAge(1);
+        user.setPrice(new BigDecimal("9.99"));
+        userService.insert(user);
+        Assert.assertNotNull(user.getId());
+        user.setDesc("Caratacus");
+        userService.insertOrUpdate(user);
+        H2User userFromDB = userService.selectById(user.getId());
+        Assert.assertEquals("Caratacus", userFromDB.getDesc());
+    }
+
+    @Test
+    public void testUpdate(){
+
+    }
+
+    @Test
+    public void testDelete(){
+        H2User user = new H2User();
+        user.setAge(1);
+        user.setPrice(new BigDecimal("9.99"));
+        userService.insert(user);
+        Long userId = user.getId();
+        Assert.assertNotNull(userId);
+        userService.deleteById(userId);
+        Assert.assertNull(userService.selectById(userId));
+    }
+
+    @Test
+    public void testSelectByid(){
+        Long userId = 101L;
+        Assert.assertNotNull(userService.selectById(userId));
+    }
+
+    @Test
+    public void testSelectOne(){
+        H2User user = new H2User();
+        user.setId(105L);
+        EntityWrapper<H2User> ew = new EntityWrapper<>(user);
+        H2User userFromDB = userService.selectOne(ew);
+        Assert.assertNotNull(userFromDB);
+    }
+
+    @Test
+    public void testSelectList(){
+        H2User user = new H2User();
+        EntityWrapper<H2User> ew = new EntityWrapper<>(user);
+        List<H2User> list = userService.selectList(ew);
+        Assert.assertNotNull(list);
+        Assert.assertNotEquals(0,list.size());
+    }
+
+    @Test
+    public void testSelectPage(){
+        Page<H2User> page = userService.selectPage(new Page<H2User>(1,3));
+        Assert.assertEquals(3,page.getRecords().size());
+    }
+}

+ 41 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/config/DBConfig.java

@@ -0,0 +1,41 @@
+package com.baomidou.mybatisplus.test.h2.config;
+
+import java.sql.SQLException;
+
+import javax.sql.DataSource;
+
+import org.h2.Driver;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.jdbc.datasource.SimpleDriverDataSource;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
+
+/**
+ * <p>
+ * H2 Memory Database config
+ * </p>
+ *
+ * @author Caratacus
+ * @date 2017/4/1
+ */
+@Configuration
+@EnableTransactionManagement
+public class DBConfig {
+
+    @Bean
+    public DataSource dataSource() throws SQLException {
+        SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
+        dataSource.setDriver(new Driver());
+        dataSource.setUrl("jdbc:h2:mem:AZ;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
+        dataSource.setUsername("sa");
+        dataSource.setPassword("");
+        return dataSource;
+    }
+
+    @Bean
+    public DataSourceTransactionManager transactionManager(DataSource ds) {
+        return new DataSourceTransactionManager(ds);
+    }
+
+}

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

@@ -0,0 +1,54 @@
+package com.baomidou.mybatisplus.test.h2.config;
+
+import javax.sql.DataSource;
+
+import org.apache.ibatis.plugin.Interceptor;
+import org.apache.ibatis.session.SqlSessionFactory;
+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 org.springframework.core.io.ResourceLoader;
+
+import com.baomidou.mybatisplus.MybatisConfiguration;
+import com.baomidou.mybatisplus.MybatisXMLLanguageDriver;
+import com.baomidou.mybatisplus.entity.GlobalConfiguration;
+import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
+import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
+
+/**
+ * <p>
+ * TODO class
+ * </p>
+ *
+ * @author Caratacus
+ * @date 2017/4/1
+ */
+@Configuration
+@MapperScan("com.baomidou.mybatisplus.test.h2.entity.mapper")
+public class MybatisPlusConfig {
+
+    @Bean("mybatisSqlSession")
+    public SqlSessionFactory sqlSessionFactory (DataSource dataSource, ResourceLoader resourceLoader, GlobalConfiguration globalConfiguration) throws Exception {
+        MybatisSqlSessionFactoryBean sqlSessionFactory = new MybatisSqlSessionFactoryBean();
+        sqlSessionFactory.setDataSource(dataSource);
+//        sqlSessionFactory.setConfigLocation(resourceLoader.getResource("classpath:mybatis-config.xml"));
+        sqlSessionFactory.setTypeAliasesPackage("com.baomidou.mybatisplus.test.h2.entity.persistent");
+        MybatisConfiguration configuration = new MybatisConfiguration();
+        configuration.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
+        configuration.setJdbcTypeForNull(JdbcType.NULL);
+        sqlSessionFactory.setConfiguration(configuration);
+        PaginationInterceptor pagination = new PaginationInterceptor();
+        pagination.setDialectType("h2");
+        sqlSessionFactory.setPlugins(new Interceptor[]{
+            pagination
+        });
+        sqlSessionFactory.setGlobalConfig(globalConfiguration);
+        return sqlSessionFactory.getObject();
+    }
+
+    @Bean
+    public GlobalConfiguration globalConfiguration(){
+        return new GlobalConfiguration();
+    }
+}

+ 16 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/entity/mapper/H2UserMapper.java

@@ -0,0 +1,16 @@
+package com.baomidou.mybatisplus.test.h2.entity.mapper;
+
+import com.baomidou.mybatisplus.mapper.BaseMapper;
+import com.baomidou.mybatisplus.test.h2.entity.persistent.H2User;
+
+/**
+ * <p>
+ * TODO class
+ * </p>
+ *
+ * @author Caratacus
+ * @date 2017/4/1
+ */
+public interface H2UserMapper extends BaseMapper<H2User> {
+
+}

+ 172 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/entity/persistent/H2User.java

@@ -0,0 +1,172 @@
+/**
+ * Copyright (c) 2011-2014, hubin (jobob@qq.com).
+ *
+ * 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
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.math.BigDecimal;
+
+import com.baomidou.mybatisplus.annotations.TableField;
+import com.baomidou.mybatisplus.annotations.TableId;
+import com.baomidou.mybatisplus.annotations.TableName;
+import com.baomidou.mybatisplus.annotations.Version;
+import com.baomidou.mybatisplus.enums.FieldStrategy;
+
+/**
+ * <p>
+ * 测试用户类
+ * </p>
+ *
+ * @author hubin sjy
+ */
+/* 表名 value 注解【 驼峰命名可无 】, resultMap 注解测试【 映射 xml 的 resultMap 内容 】 */
+@TableName
+public class H2User implements Serializable {
+
+	/* 表字段注解,false 表中不存在的字段,可无该注解 默认 true */
+	@TableField(exist = false)
+	private static final long serialVersionUID = 1L;
+
+	/* 主键ID 注解,value 字段名,type 用户输入ID */
+	@TableId(value = "test_id")
+	private Long id;
+
+	/* 测试忽略验证 */
+	private String name;
+
+	private Integer age;
+	
+	/*BigDecimal 测试*/
+	private BigDecimal price;
+
+	/* 测试下划线字段命名类型, 字段填充 */
+	@TableField(value = "test_type", validate = FieldStrategy.IGNORED)
+	private Integer testType;
+
+	private String desc;
+	
+	@Version
+	private Integer version;
+
+
+	public H2User() {
+
+	}
+
+	public H2User(String name) {
+		this.name = name;
+	}
+
+	public H2User(Integer testType) {
+		this.testType = testType;
+	}
+
+	public H2User(String name, Integer age) {
+		this.name = name;
+		this.age = age;
+	}
+
+	public H2User(Long id, String name) {
+		this.id = id;
+		this.name = name;
+	}
+
+	public H2User(Long id, Integer age) {
+		this.id = id;
+		this.age = age;
+	}
+
+	public H2User(Long id, String name, Integer age, Integer testType) {
+		this.id = id;
+		this.name = name;
+		this.age = age;
+		this.testType = testType;
+	}
+
+	public H2User(String name, Integer age, Integer testType) {
+		this.name = name;
+		this.age = age;
+		this.testType = testType;
+	}
+
+	public Long getId() {
+		return id;
+	}
+
+	public void setId(Long id) {
+		this.id = id;
+	}
+
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public BigDecimal getPrice() {
+		return price;
+	}
+
+	public void setPrice(BigDecimal price) {
+		this.price = price;
+	}
+
+	public Integer getAge() {
+		return age;
+	}
+
+	public void setAge(Integer age) {
+		this.age = age;
+	}
+
+	public Integer getTestType() {
+		return testType;
+	}
+
+	public void setTestType(Integer testType) {
+		this.testType = testType;
+	}
+
+	public String getDesc() {
+		return desc;
+	}
+
+	public void setDesc(String desc) {
+		this.desc = desc;
+	}
+
+	public Integer getVersion() {
+		return version;
+	}
+
+	public void setVersion(Integer version) {
+		this.version = version;
+	}
+
+	@Override
+	public String toString() {
+		return "H2User{" +
+				"id=" + id +
+				", name='" + name + '\'' +
+				", age=" + age +
+				", price=" + price +
+				", testType=" + testType +
+				", desc='" + desc + '\'' +
+				", version=" + version +
+				'}';
+	}
+}

+ 31 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/entity/service/IH2UserService.java

@@ -0,0 +1,31 @@
+/**
+ * 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.service;
+
+import com.baomidou.mybatisplus.service.IService;
+import com.baomidou.mybatisplus.test.h2.entity.persistent.H2User;
+
+/**
+ * <p>
+ * Service层测试
+ * </p>
+ * 
+ * @author hubin
+ * @date 2017-01-30
+ */
+public interface IH2UserService extends IService<H2User> {
+
+}

+ 36 - 0
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/h2/entity/service/impl/H2UserServiceImpl.java

@@ -0,0 +1,36 @@
+/**
+ * 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.service.impl;
+
+import org.springframework.stereotype.Service;
+
+import com.baomidou.mybatisplus.service.impl.ServiceImpl;
+import com.baomidou.mybatisplus.test.h2.entity.mapper.H2UserMapper;
+import com.baomidou.mybatisplus.test.h2.entity.persistent.H2User;
+import com.baomidou.mybatisplus.test.h2.entity.service.IH2UserService;
+
+/**
+ * <p>
+ * Service层测试
+ * </p>
+ * 
+ * @author hubin
+ * @date 2017-01-30
+ */
+@Service
+public class H2UserServiceImpl extends ServiceImpl<H2UserMapper, H2User> implements IH2UserService {
+
+}

+ 12 - 0
mybatis-plus/src/test/resources/h2/spring-test-h2.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
+	xmlns:context="http://www.springframework.org/schema/context"
+	xmlns:mvc="http://www.springframework.org/schema/mvc"
+	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  
+           http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
+
+	<context:component-scan base-package="com.baomidou.mybatisplus.test.h2" />
+
+</beans>

+ 12 - 0
mybatis-plus/src/test/resources/h2/user.ddl.sql

@@ -0,0 +1,12 @@
+CREATE TABLE IF NOT EXISTS  h2user (
+	test_id BIGINT(20) NOT NULL ,
+	name VARCHAR(30) NULL DEFAULT NULL ,
+	age INT(11) NULL DEFAULT NULL ,
+	test_type INT(11) NULL ,
+	test_date DATETIME NULL DEFAULT NULL,
+	price DECIMAL(10,2) NULL DEFAULT NULL,
+	desc VARCHAR(30) NULL DEFAULT NULL ,
+	version INT(5) NULL DEFAULT NULL,
+	PRIMARY KEY (test_id)
+)
+

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

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