mybatis.xml里面存放:
1、数据库的环境
2、定义各种mapper.xml的位置
一、直接使用MyBatis
1)定义一个全局的配置文件mybatis.xml
2)将配置文件翻译成输入流
3)解析配置文件,生成数据库连接工厂
4)通过数据库连接工厂生成数据库会话
// Session:会话 (连接数据库后就建立了一次会话,有了会话就可以操作数据库)
5)通过数据库会话生成对应的mapper
6)通过mapper调用对应mapper中的方法,对应mapper.xml文件中的id
String resource = "mybatis.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); // Session:会话 (连接数据库后就建立了一次会话,有了会话就可以操作数据库) SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); Student student = mapper.selectById(1);



