As can be seen from the development steps of MyBatis, MyBatis mainly applies to the following classes

1, the SqlSessionFactoryBuilder is

The entry point of every MyBatis application is SqlSessionFactoryBuilder.

It creates a Configuration object from an XML Configuration file (you can create it yourself, of course), and then creates an SqlSessionFactory object from the build method. It is not necessary to create SqlSessionFactoryBuilder every time you visit Mybatis. It is more common to create a global object. The sample program is as follows:

private static SqlSessionFactoryBuilder sqlSessionFactoryBuilder;
private static SqlSessionFactory sqlSessionFactory;

private static void init() throws IOException {
    String resource = "mybatis-config.xml"; Reader reader = Resources.getResourceAsReader(resource); sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder(); sqlSessionFactory = sqlSessionFactoryBuilder.build(reader); } / / org. Apache. Ibatis. Session. The Configuration is the core of the mybatis initialization.Copy the code


The Configuration in mybatis-config. XML will parse the XML into the Configuration class.

The SqlSessionFactoryBuilder generates a Configuration object from the incoming data stream (XML) and then creates a default SqlSessionFactory instance from the Configuration object.

2, SqlSessionFactory

The SqlSessionFactory object’s main function is to create an SqlSession object. Like SqlSessionFactoryBuilder, there is no need to create an SqlSessionFactory every time Mybatis is accessed. The usual approach is to create a global object. A necessary property of the SqlSessionFactory object is the Configuration object, which is a Configuration object that holds the global Configuration of Mybatis and is typically created by SqlSessionFactoryBuilder from an XML Configuration file. Here’s a simple example:

<? xml version="1.0" encoding="UTF-8"? > <! DOCTYPE configuration PUBLIC"- / / mybatis.org//DTD Config / 3.0 / EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <! -- Enable the hump rule automatic mapping of field attribute values; Setting name= <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <! -- Custom type aliases --> <typeAliases>
		<package name="com.jourwon.mybatis.pojo" />
	</typeAliases> <! -- Configure environment variables --> <environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url"
					value="JDBC: mysql: / / 127.0.0.1:3306 / test? useSSL=false" />
				<property name="username" value="root" />
				<property name="password" value="root"/> </dataSource> </environment> </environments> <! Mappers --> <mappers> <mapper resource="mybatis/mappers/UserMapper.xml" />
	</mappers>

</configuration>Copy the code

3, SqlSession

  • The SqlSession object’s main function is to complete a database access and result mapping, it is similar to the database session concept, because it is not thread safe, so the scope of the SqlSession object must be limited within the method. The default implementation class for SqlSession is DefaultSqlSession, which has two properties that must be configured: Configuration and Executor. Configuration has been described previously and will not be discussed here. SqlSession operations to the database are performed by Executor.
  • SqlSession: DefaultSqlSession is created by default, level 1 caching is enabled, and executor creation and value assignment are enabled.
  • SqlSession has an important method called getMapper, which, as the name implies, is used to getMapper objects. What is a Mapper object? According to the official manual of Mybatis, in addition to initiating and starting Mybatis, the application needs to define some interfaces, which define the method of accessing the database, and place the XML configuration file of the same name in the package path of the interface.
  • SqlSession getMapper method is the link between application and Mybatis. When application accesses getMapper, Mybatis will generate a proxy object according to the incoming interface type and the corresponding XML configuration file. This proxy object is called Mapper object. The application should access Mybatis SqlSession object through this Mapper object, so as to achieve the purpose of inserting into Mybatis process.

UserMapper interface

Public interface UserMapper {// Query User List public List<User> selectAll(); }Copy the code

UserMapper. XML file

<? xml version="1.0" encoding="UTF-8"? > <! DOCTYPE mapper PUBLIC"- / / mybatis.org//DTD Mapper / 3.0 / EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.jourwon.mybatis.mapper.UserMapper">

	<select id="selectAll" resultType="User">
		SELECT * FROM user
	</select>

</mapper>Copy the code

The test class

public class MybatisTest {

	private UserMapper userMapper;

	@Before
	public void init() throws Exception {
		String resource = "mybatis/mybatis-config.xml"; / / to get configuration file stream InputStream InputStream = Resources. The getResourceAsStream (resource); SqlSessionFactory Factory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = factory.openSession(true);
		
		userMapper = session.getMapper(UserMapper.class);
	}

	@Test
	public void tesSelectAll() {
		List<User> userList = userMapper.selectAll();
		for(User user : userList) { System.out.println(user); }}}Copy the code

4, Executor

The Executor object is created when the Configuration object is created and cached in the Configuration object. The primary function of an Executor object is to call StatementHandler to access the database and store the query results in a cache, if configured.

5, StatementHandler

StatementHandler is where you actually access the database and call ResultSetHandler to process the query results.

6, ResultSetHandler

Process the query results.

The last

Like can pay attention to my public number: Java small melon brother sharing platform