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();