本文承接上文,主要讲述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(); } }
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/98937.html