package transaction;

import com.Config.AppConfig1;
import com.Domain.Context;
import com.Domain.User;
import com.Mapper.UserMapper;
import com.Service.UserService;
import com.StartUp;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.TransactionIsolationLevel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * Created by hcj on 18-7-16
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {StartUp.class})
public class MybatisTest {
    private static final AnnotationConfigApplicationContext context =
            new AnnotationConfigApplicationContext(AppConfig1.class);
    @Autowired
    UserService userService;
    //=====================================mybatis 2 homework start=============================================================

    /**
     * 提供单对象，和多对象参数， sql打印功能 => 查询
     */
    @Test
    public void selectUser() {
        String name = "hcj";
        User user = userService.selectUser(name, "123");
        System.out.println(user);
    }

    /**
     * 提供单对象，， sql打印功能 => 修改删除，新增，  注意： 目前多对象参数暂不支持
     */
    @Test
    public void insertUser() {
        userService.insertUserNoObject(User.builder().username("1").password("2").enabled(true).build());
    }

    //=========================`================mybatis 2 homework end=========================================================

    /**
     * 1. 自定义TypeHandler类
     *
     * @param
     * @return void
     * @author houchunjian
     * @date 2019/12/1 0001 14:01
     */
    @Test
    public void test() {
        List<User> user = userService.findUser();
        System.out.println(user);
    }

    /**
     * 1. Mybatis 批量插入数据库代码
     *
     * @param
     * @return void
     * @author houchunjian
     * @date 2019/12/1 0001 14:01
     */
    @Test
    public void insertOptimistic() {
        SqlSessionFactory sqlSessionFactory = context
                .getBean("sqlSessionFactory", SqlSessionFactory.class);
        // sqlSession 开启事务。 ExecutorType.REUSE   保证batch 事务的合理使用，
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, TransactionIsolationLevel.READ_COMMITTED);
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        for (int i = 10; i < 20; i++) {
            User user = new User();
            user.setUsername("pop" + i);
            user.setPassword(i + "");
            user.setEnabled(true);
            if (i % 2 == 0) {
                Context build = Context.builder().username("hcj").password("123")
                        .context("hello world").enabled(true).build();
                build.setContext("hello world: " + i);
                user.setContext(build);
            } else {
                Context build = Context.builder().username("hcj").password("123")
                        .context("hello world").enabled(true).build();
                build.setContext("world: " + i);
                user.setContext(build);
            }
            mapper.insert(user);
        }
        // 刷新
        sqlSession.flushStatements();
    }
}
