There is a personal public account at the bottom of the article: Xiao Zheng who loves technology. Mainly to share development knowledge, those who are interested can pay attention to first-hand.
premise
Studying the source code is still very helpful for our technical improvement. It is recommended to start with mybatis for simple source code. There are not many design patterns involved. The source code and parent project dependencies of mybatis need to be downloaded . Note that the parent project dependency version in the downloaded mybatis should correspond . Download the more stable version here. . The corresponding parent version is 31.mybatis-3.5.3
mybatis-3.5.3
Download address: github address
mybatis-parent
Download address: github download address
If the access to github is slow and the comment area is at the top, I will give a link to the source code address of the downloaded project.
build environment
1. First create a Maven project
The specific creation process is omitted
2. Import source code
Open the project structure, or press the shortcut key:Ctrl + Alt + Shift + S
Select Modules --" Click the + sign -- "Select import Module --" Select the downloaded source code
Import the downloaded source code into
After selection, a dialog box pops up, select Maven and click finish
Import both downloaded source codes
Project structure after importing two source codes
3. In the created Maven project, add code
Create a database by yourself, write a few query statements, there is nothing to say. Brief here
4. Debug and run the research source code
Then you can view the code execution through Debug mode
test class
package com.zyz;
import com.zyz.mapper.userMapper;
import com.zyz.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.List;
/**
* @author zyz
* @version 1.0
* @data 2023/7/28 12:48
* @Description:
*/
public class AppTest {
@Test
public void test() throws IOException {
InputStream input = Resources.getResourceAsStream("SqlSessionConfig.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(input);
SqlSession sqlSession = sessionFactory.openSession();
userMapper dao = sqlSession.getMapper(userMapper.class);
System.out.println(dao);
List<User> userList = dao.selectAllUser();
for (User level : userList) {
System.out.println(level);
}
}
@Test
public void testMyBatisBuild() throws IOException {
Reader reader = Resources.getResourceAsReader("SqlSessionConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = factory.openSession();
userMapper mapper = sqlSession.getMapper(userMapper.class);
User one = mapper.getOne(1);
System.out.println(one);
sqlSession.close();
}
}
5. Project start effect
possible problems
The problems that arise are generally caused by dependencies. Just download the corresponding dependencies.
Add the following dependencies to the pom under the maven project you created
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.24.1-GA</version>
</dependency>
Is the xml file not compiled? If so, add the following code to your pom file.
Add to
<build>
<!-- 加载配置文件 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
Afterword
If you hate the trouble and don't want to build this environment yourself. The environment I will build here has been packaged, and the address link is at the top of the comment area . You can run it directly and study the code. My personal suggestion is to build an environment. Can also learn a lot.
I put the project link on the top of the comment area