MyBatis的使用二(增删改查)

本文承接上文,主要讲述mybatis的增删改查案例

public class MybatisTest {

    @Test
    // 使用mapper接口的代理实现类执行sql语句
    public void test01() throws Exception{
        // 1.获取核心配置文件的输入流
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis_config.xml");
        // 2.获取sqlSessionFactoryBuilder执行sql语句
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        // 3.创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
        // 4.获取sqlSession
        //      SqlSession sqlSession = sqlSessionFactory.openSession(); 不会自动提交事务,需要自己手动设置
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 5.获取UserMapper代理实现类对象
        //      反射机制,创建UserMapper接口的实现类,返回给mapper【代理模式】
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        // 6.调用UserMapper代理实现类对象mapper的insertUser()方法
        int i = mapper.insertUser();
        System.out.println(i);
        // 7.***手动提交事务
        sqlSession.commit();
        // 8.关闭sqlSession对象
        sqlSession.close();

    }

    @Test
    // 直接使用sqlSession执行sql语句
    public void test02() throws Exception{
        // 1.获取核心配置文件的输入流
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis_config.xml");
        // 2.获取sqlSessionFactoryBuilder执行sql语句
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        // 3.创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
        // 4.获取sqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        // 5.使用sqlSession执行sql语句
        //      参数是mapper接口绑定的xml映射文件的namespace.sqlId
        //      com.hspedu.mapper.UserMapper.insertUser
        int i = sqlSession.insert("com.hspedu.mapper.UserMapper.insertUser");
        System.out.println(i);
        // 6.***手动提交事务
        sqlSession.commit();
        // 7.关闭sqlSession对象
        sqlSession.close();
    }

    @Test
    // sqlSession自动提交事务
    public void test03() throws Exception{
        // 1.获取核心配置文件的输入流
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis_config.xml");
        // 2.获取sqlSessionFactoryBuilder执行sql语句
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        // 3.创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(resourceAsStream);
        // 4.获取sqlSession
        //      SqlSession sqlSession = sqlSessionFactory.openSession(true); 自动提交事务
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        // 5.获取UserMapper代理实现类对象
        //      反射机制,创建UserMapper接口的实现类,返回给mapper【代理模式】
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        // 6.调用UserMapper代理实现类对象mapper的insertUser()方法
        int i = mapper.insertUser();
        System.out.println(i);
        // 7.关闭sqlSession对象
        sqlSession.close();
    }

    @Test
    // 测试工具类执行DML语句
    public void test04(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//        int i = mapper.updatePwdById();
//        System.out.println(i);
//        int del = mapper.deleteUser();
//        System.out.println(del);
//        User user = mapper.getUserSingle();
//        System.out.println(user);
        ArrayList<User> list = mapper.getUserAll();
        for (User user : list) {
            System.out.println(user);
        }
        SqlSessionUtils.closeSqlSession();
    }

}

 

posted @ 2023-02-02 16:51  zwGitOne  阅读(62)  评论(0编辑  收藏  举报