This is the third day of my participation in the August Text Challenge.More challenges in August

download

Github searches mybatis to find the project

$ git clone https://github.com/mybatis/mybatis-3.git
Copy the code

build

Because Mybatis is a maven project written in pure Java, there is no need to manually compile, just need to open the project using IDEA, IDEA will automatically help us build.

The project directory after the build is complete is as follows:

Since the current downloaded code is the master branch code, is the latest version. If you need to debug another version of the code, you can use the following steps to switch.

  • Look at the tag
$ git tagMybatis -3.0.1 mybatis-3.0.2 mybatis-3.0.3 mybatis-3.0.4...... Mybatis - 3.5.4 mybatis - 2.6.2 mybatis - 3.5.6 mybatis - 3.5.7Copy the code
  • Pull a tag locally
#Pull version 3.4.1 to the local local_mybatis branch
$Git branch local_mybatis mybatis - 3.4.1 track

#Viewing local branches
$ git branch
  local_mybatis
* master

#Switch to the local_mybatis branch
$ git checkout local_mybatis
Copy the code

debugging

Mybatis debugging can be directly in the source project debugging, you can also introduce the source code of the jar package in the existing project.

Method 1. Introduce jar packages for existing projects

  • Start by packaging the source code
$ mvn clean package -Dmaven.test.skip=true
Copy the code
  • Enter the project structure of an existing project (you can click the button or use the shortcut key)

  • Go to the Libraries menu and click the minus sign to remove the old Mybatis dependency

  • Click the plus sign (select Java) to add the jar package created in the first step (in the Target directory). Finally, select the application module (as required).

Method two, in the source code project debugging

  1. Mybatis has enough test cases for you to debug. Of course, you can write your own.
  2. Test classes related to database initializationBaseDataTest, database related files inorg.apache.ibatis.databasesPackage, Mapper interface and POJO objects inorg.apache.ibatis.domainPackage, Mapper corresponding XML files and related configuration files inorg.apache.ibatis.builderUnder the bag.
  3. Mybatis has the Derby database embedded, so you don’t need to do any other configuration, you can directly create a table (blog-derby-schema.sql), add data (blog-derby-dataload.sql), and then test.

  1. Create your own Test class as follows (based on version 3.5.8) and it will work.
public class MyTest extends BaseDataTest {

    private static SqlSessionFactory sqlSessionFactory;

    @BeforeAll
    static void setup(a) throws Exception {
        createBlogDataSource();
        final String resource = "org/apache/ibatis/builder/MapperConfig.xml";
        final Reader reader = Resources.getResourceAsReader(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    }

    @Test
    public void test(a) {
        try(SqlSession sqlSession = sqlSessionFactory.openSession()) { BlogMapper mapper = sqlSession.getMapper(BlogMapper.class); System.out.println(mapper.selectAllPosts()); }}}Copy the code

The preparation work is complete, next you can detail Mybatis source 🙂