创建sqlSession对象操作数据库
作者:互联网
1.加载核心配置文件
//加载mybatis核心配置文件,获取SqlSessionFactory String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); inputStream.close();
2.获取sqlSession对象
//获取Sqlsession对象,用它执行sql SqlSession sqlSession = sqlSessionFactory.openSession();
3.获取mapper对象,利用该对象执行相应数据库操作
UserMapper usermapper = sqlSession.getMapper(XxxMapper.class);
4.执行相应方法(如下)
//查询所有用户 List<User> users1 = usermapper.selectall(); sqlSession.close();
标签:配置文件,对象,创建,数据库,usermapper,inputStream,获取,sqlSession 来源: https://www.cnblogs.com/theng/p/16686117.html