1. read the configuration file: use is parsed Xml technical documents 2.mybatis support himself wrote dao layer, but not necessary. mybatis do: first create a proxy object, the second call the method in the proxy object. 3. The same comments if and profile appear together, then will complain, even in Sql ***. Xml comment out a make mistakes profile. I do not know, but I Sql ***. Xml configuration file method he commented the profile error, do not know to leave a comment to annotate the profile method will not complain after learning of the teacher to explain a bit parsing xml source code, it is determined that the operation is to have no resource to determine if a mapper.xml if the judgment is not only a way of comment, I do not know why this error is due to repeat it, but I put it comment ah. 4.mybatis during operation: the configuration file is executed, based on the use of java reflection mechanism to return the desired result set findAll query : mechanism 1. Create a Connection profile based on the information object registration drive to get a connection 2. target acquisition and processing of PreparedSatement need SQL statement 3.
ResultSet 4. execute the query results returned profile Mapper object contains: Sql statement domainClassPath (fully qualified class name of the package) 5. The class : Resources the SqlSessionFactoryBuilder Interface: SqlSessionFactory SqlSession four role see source code comments 6.xml manner annotations to differentiate the way: ' Easy to use annotation complex with xml xml can use custom annotation, through reflection, spell out SQL statements, and notes how dynamically it? Summary: mybatis parse xml acquiring various properties file includes Driver , url , account password, mappers information and mapper information, the use of java to generate reflection mechanism to generate a proxy object factory work
// reads the configuration file // absolute path and relative path // read the configuration file method: // first: using a class loader, he can only read the classpath configuration file // 2nd: ServletContext object using the getRealPath () method // Create factories mybatis use the builder pattern InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml"); // Create SqlSessionFactory factory System.out.println(inputStream); // builder builder SqlSessionFactoryBuilder builder =new SqlSessionFactoryBuilder(); SqlSessionFactory factory =builder.build(inputStream); // use the factory model of production SqlSession // objects using factory production SqlSession SqlSession session =factory.openSession(); // use the interface proxy object SqlSession create Dao // use a proxy mode IUserDao userDao =session.getMapper(IUserDao.class); // use the proxy object List<User> users =userDao.findAll(); System.out.println(users); for(User user: users) { System.out.println(user.toString()); } // release resources session.close(); inputStream.close();