/**
 * 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;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.baomidou.mybatisplus.MybatisSessionFactoryBuilder;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.baomidou.mybatisplus.test.entity.User;
import com.baomidou.mybatisplus.test.mapper.UserMapper;
import com.baomidou.mybatisplus.toolkit.IdWorker;

/**
 * <p>
 * MybatisPlus 测试类
 * </p>
 * http://git.oschina.net/jrl/mybatis-mapper
 * 
 * @author hubin
 * @Date 2016-01-23
 */
public class UserMapperTest {
	private static final String RESOURCE = "mybatis-config.xml";

	/**
	 * RUN 测试
	 */
	public static void main(String[] args) {
		InputStream in = UserMapperTest.class.getClassLoader().getResourceAsStream(RESOURCE);

		/*
		 * 此处采用 MybatisSessionFactoryBuilder 构建
		 * SqlSessionFactory，目的是引入AutoMapper功能
		 */
		SqlSessionFactory sessionFactory = new MybatisSessionFactoryBuilder().build(in);
		SqlSession session = sessionFactory.openSession();
		UserMapper userMapper = session.getMapper(UserMapper.class);
		
		int result = userMapper.deleteByName("test");
		System.out.println("\n------------------deleteByName----------------------\n result=" + result);
		sleep();
		
		userMapper.insert(new User(IdWorker.getId(), "test", 18));
		System.out.println("\n------------------insert----------------------\n name=test, age=18");
		sleep();

		/*
		 * 此处的 selectById 被UserMapper.xml中的 selectById 覆盖了
		 */
		System.err.println("\n------------------selectById----------------------");
		User user = userMapper.selectById(2L);
		print(user);


		/*
		 * updateById 是从 AutoMapper 中继承而来的，UserMapper.xml中并没有申明改sql
		 */
		System.err.println("\n------------------updateById----------------------");
		user.setName("MybatisPlus_" + System.currentTimeMillis());
		userMapper.updateById(user);
		sleep();
		
		/*
		 * 此处的 selectById 被UserMapper.xml中的 selectById 覆盖了
		 */
		user = userMapper.selectById(user.getId());
		print(user);

		System.err.println("\n------------------selectAll----------------------");
		List<User> userList = userMapper.selectAll();
		for (int i = 0; i < userList.size(); i++) {
			print(userList.get(i));
		}
		
		System.err.println("\n------------------list 分页查询，不查询总数（此时可自定义 count 查询）----------------------");
		List<User> rowList = userMapper.list(new RowBounds(0, 2));
		for (int i = 0; i < rowList.size(); i++) {
			print(rowList.get(i));
		}
		
		System.err.println("\n------------------list 分页查询，查询总数----------------------");
		Pagination pagination = new Pagination(0, 2);
		List<User> paginList = userMapper.list(pagination);
		for (int i = 0; i < paginList.size(); i++) {
			print(paginList.get(i));
		}
		System.err.println(pagination.toString());
		
		// 提交
		session.commit();
	}

	/*
	 * 打印测试信息
	 */
	private static void print(User user) {
		sleep();
		if (user != null) {
			System.out.println("\n user: id=" + user.getId() + ", name=" + user.getName() + ", age=" + user.getAge());
		} else {
			System.out.println("\n user is null.");
		}
	}
	
	/*
	 * 慢点打印 
	 */
	private static void sleep() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

@@ -1197,59 +1197,13 @@
 p%3E%0D%0A
- * http://git.oschina.net/jrl/mybatis-mapper%0D%0A
  * %0D%0A
+
  * @
@@ -1965,16 +1965,47 @@
 );%0D%0A%09%09%0D%0A
+%09%09Long id = IdWorker.getId();%0D%0A
 %09%09userMa
@@ -2025,32 +2025,18 @@
 ew User(
-IdWorker.getId()
+id
 , %22test%22
@@ -2359,18 +2359,16 @@
 er);%0D%0A%0D%0A
-%0D%0A
 %09%09/*%0D%0A%09%09
@@ -3481,16 +3481,16 @@
 ;%0D%0A%09%09%7D%0D%0A
-
 %09%09System
@@ -3531,16 +3531,216 @@
 );%0D%0A%09%09%0D%0A
+%09%09System.out.println(%22%5Cn%5Cn------------------deleteById----------------------%22);%0D%0A%09%09sleep();%0D%0A%09%09int del = userMapper.deleteById(id);%0D%0A%09%09System.err.println(%22 delete id=%22 + id + %22 ,result=%22 + del);%0D%0A%09%09%0D%0A
 %09%09// %E6%8F%90%E4%BA%A4%0D

add test