package com.lc.mybatis.test;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lc.mybatis.entity.User;
import com.lc.mybatis.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

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

public class TestUser {




    @Test
    public void test01() throws IOException {
        //构建sqlSessionfactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(is);
        //获取 SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取接口  jdk动态代理 UserMapper 代理类 反射技术
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = new User();

        user.setUsername("zs");
        //user.setGender("男");
        user.setAddress("北京");

        List<User> userList = mapper.getByUser(user);

        System.out.println("用户信息："+userList);

        //释放
        sqlSession.close();
    }


    @Test
    public void test02() throws IOException {
        //构建sqlSessionfactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(is);
        //获取 SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取接口  jdk动态代理 UserMapper 代理类 反射技术
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = new User();

        //user.setUsername("zs");
        //user.setGender("男");
        user.setAddress("北京");

        List<User> userList = mapper.getByUserChoose(user);

        System.out.println("用户信息："+userList);

        //释放
        sqlSession.close();
    }



    @Test
    public void test03() throws IOException {
        //构建sqlSessionfactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(is);
        //获取 SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取接口  jdk动态代理 UserMapper 代理类 反射技术
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = new User();

        //user.setUsername("zs");
        user.setGender("男");
        user.setAddress("北京");

        List<User> userList = mapper.getByUserWhere(user);

        System.out.println("用户信息："+userList);

        //释放
        sqlSession.close();
    }



    @Test
    public void test04() throws IOException {
        //构建sqlSessionfactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(is);
        //获取 SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取接口  jdk动态代理 UserMapper 代理类 反射技术
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = new User();

        user.setUsername("zs");
        user.setGender("男");
        user.setAddress("北京");

        List<User> userList = mapper.getByUserTrim01(user);

        System.out.println("用户信息："+userList);

        //释放
        sqlSession.close();
    }


    @Test
    public void test05() throws IOException {
        //构建sqlSessionfactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(is);
        //获取 SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取接口  jdk动态代理 UserMapper 代理类 反射技术
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = new User();
        user.setId(1);
        //user.setUsername("zs1");
        user.setGender("女");
        user.setAddress("天津");

        mapper.updateUserSet(user);

        //释放
        sqlSession.close();
    }


    @Test
    public void test06() throws IOException {
        //构建sqlSessionfactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(is);
        //获取 SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取接口  jdk动态代理 UserMapper 代理类 反射技术
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user = new User();
        user.setId(1);
        //user.setUsername("zs1");
        user.setGender("女");
        user.setAddress("上海");

        mapper.updateUserTrim02(user);

        //释放
        sqlSession.close();
    }


    @Test
    public void test07() throws IOException {
        //构建sqlSessionfactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(is);
        //获取 SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取接口  jdk动态代理 UserMapper 代理类 反射技术
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        List<User> userList = new ArrayList<>();

        for (int i = 0; i < 3; i++) {
            User user = new User();
            user.setId(7+i);
            user.setUsername("zs1");
            user.setPassword("123456");
            user.setGender("女");
            user.setAddress("上海");

            userList.add(user);
        }

        mapper.insertBatch(userList);

        //释放
        sqlSession.close();
    }

    @Test
    public void test08() throws IOException {
        //构建sqlSessionfactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(is);
        //获取 SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取接口  jdk动态代理 UserMapper 代理类 反射技术
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);


        ArrayList<Integer> ids = new ArrayList<>();
        ids.add(7);
        ids.add(8);
        ids.add(9);
        mapper.deleteByIds(ids);

        //释放
        sqlSession.close();
    }


    @Test
    public void test09() throws IOException {
        //构建sqlSessionfactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(is);
        //获取 SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取接口  jdk动态代理 UserMapper 代理类 反射技术
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        User user1 = mapper.getById(2);
        //清空
        sqlSession.clearCache();
        System.out.println("........................");
        User user2 = mapper.getById(2);

        //释放
        sqlSession.close();
    }



    @Test
    public void test010() throws IOException {
        //构建sqlSessionfactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(is);
        //获取 SqlSession
        SqlSession sqlSession1 = sqlSessionFactory.openSession(true);
        SqlSession sqlSession2 = sqlSessionFactory.openSession(true);

        UserMapper mapper1 = sqlSession1.getMapper(UserMapper.class);
        UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class);

        User user = mapper1.getById(4);
        //释放
        sqlSession1.close();

        User user2 = mapper2.getById(4);
        User user3 = mapper2.getById(4);
        User user4 = mapper2.getById(4);

        sqlSession2.close();
    }


    @Test
    public void test011() throws IOException {
        //构建sqlSessionfactory
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory  = new SqlSessionFactoryBuilder().build(is);
        //获取 SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //获取接口  jdk动态代理 UserMapper 代理类 反射技术
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        //page 当前页
        //size 每页显示条数
        //PageHelper.startPage(page,size);
        PageHelper.startPage(1,2);
        //list
        List<User> userList = mapper.getUserList();

        PageInfo<User> pageInfo = new PageInfo<>(userList);

        List<User> list = pageInfo.getList();

        //list 集合
        list.forEach(System.out::println);

        System.out.println("集合:"+list.getClass());
        System.out.println("页码："+pageInfo.getPages());
        System.out.println("上一页："+pageInfo.getPrePage());
        System.out.println("数量："+pageInfo.getTotal());

        //释放
        sqlSession.close();
    }
}
