Don't be afraid to look at the source code, a map to figure out Mybatis's Mapper Principle

Links to the original text:


Simple use

general talk of the topic

Look at the picture

Speaking of looking at the source code, many students'inner fear is very normal from the perspective of human nature, because people are very afraid of the unknown things. Secondly, you may always feel that you can work without principle. There is no strong desire in your subconscious. From the experience of reading the source code. Said that Mybatis is the most suitable source code for introducing among the three frameworks of Java SSM.

Simple use

This is a simple example of Mybatis saving objects

public void testSave() throws Exception {
    //Create sessionFactory objects
    SqlSessionFactory sf = new SqlSessionFactoryBuilder().
    //Get the session object
    SqlSession session = sf.openSession();
    //Create entity objects
    User user = new User();
    //Save the data to the database
    session.insert("com.toby.mybatis.domain.UserMapper.add", user);
    //Submit transactions, which is necessary, otherwise even if sql is sent, it will not be saved in the database.
    //close resource
<mapper namespace="com.toby.mybatis.domain.UserMapper">
    <!--#{} Find the corresponding attribute value in the incoming object - >
    <!--parameterType What type of parameter is passed in?-->
    <insert id="add" parameterType="com.toby.mybatis.domain.User">
        INSERT INTO USER (username,password,age) VALUES (#{username},#{password},#{age})

general talk of the topic

But in practice, we do not operate in this way. We can implement CRUD operation by calling interface method through Mapper interface. The key is what this interface does, which is what we care about.

Just figure out what happened to the following code and see what the Mapper interface did.

public void testGetObject() throws Exception {
    SqlSession session = MybatisUtil.openSession();

    UserMapper mapper = session.getMapper(UserMapper.class);
    User user = mapper.get(5L);

public interface UserMapper {

    public void add(User user);
    public User get(Long id);

Look at the picture

But I think a flow chart can show what happened during this period.


1. When did the invoke method execute?
When creating proxy objects, jdk dynamic proxy needs to pass in three parameters, namely (1) class loader, (2) which interfaces to proxy (what methods to intercept), (3) where to intercept these methods. From the graph, we know that he is to intercept the execution method to invoke method in MapperProxy class, in other words, the dynamic. The invoke method of MapperProxy class is called to process the methods in the proxy object execution interface, which is why invoke is called when the get method is called.
2. Implementation process
In order to locate sql in mapper file, two conditions are needed, one is namespace and the other is sql id. To invoke the mapper interface in this way, we must follow a convention that namespace is equal to the fully qualified name of the interface. The method name of the interface is equal to the full qualified name of the interface. The sql id in the xml file is the reason why we need to pass these two in when encapsulating MapperMethod in the graph. The sql is determined, and the input parameter mapperMethod.execute(args) is spliced into one to complete sql and execute it.

Adding a sequence chart to the list of cardiac impulses

Want to know the step of Mybatis cache Hibernate and Myaitis caches

Tags: Session Mybatis SQL xml

Posted on Mon, 02 Sep 2019 22:43:36 -0700 by balacay