How does MyBatis load configuration files?
There are several common approaches for loading configuration files in MyBatis.
- Materials / Supplies
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- Input stream
String resource = "mybatis-config.xml";
FileInputStream inputStream = new FileInputStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
- Reader, the person who reads or examines written material.
String resource = "mybatis-config.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
In the examples above, mybatis-config.xml is the configuration file of MyBatis, which can be adjusted according to the actual project’s path and name. SqlSessionFactory is the core class of MyBatis, used for creating SqlSession instances to perform database operations.