之前看过一些mybatis源码,但都是断断续续看的,没有整体的梳理过。今天准备整体再过一遍。纯粹的研究源码比较枯燥,带着问题去看可以提高阅读的效率和成就感,本次源码阅读主要是想搞明白以下几个问题。
1、mybatis如何找到存储sql的xml文件
2、mybatis如何解析xml中sql相关标签并拼装成完整的sql语句
3、mybatis如何将xml中sql与接口中方法绑定
4、mybatis如何将sql发送出去并获得结果
一、这里先贴下demo源码(基于springboot脚手架),有兴趣的小伙伴可以直接粘贴使用
代码结构:

建表sql语句:
CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`salary` int(10) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB
核心maven依赖:
<!-- springboot-mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
mybatis-config.xml文件
<?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>
<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://localhost:3306/olap?serverTimezone=GMT"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="person.xml"/>
</mappers>
</configuration>
person.xml
<?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">
<!-- namespace为空间命名,可以随便写。在通过接口编程时,该namespace则为对应接口的包路径 -->
<mapper namespace="dao.PersonDao">
<!-- id也是为了方便查找所要执行的sql语句 -->
<select id="queryById" resultType="entity.Person">
select id,name ,age,salary from person where id = #{id}
</select>
</mapper>
实体类Person:
@Data
public class Person {
public Integer id;
public String name;
public Integer age;
public Integer salary;
//这里使用了lombok的Data注解,所以省略了属性的set,get方法。
}
dao接口PersonDao:
public interface PersonDao {
public Person queryById(Integer id);
}
mybatis使用:
public static void main(String[] args) {
String resource = "mybatis-config.xml";
try (
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSession session = new SqlSessionFactoryBuilder().build(inputStream).openSession();
){
PersonDao personDao = session.getMapper(PersonDao.class);
Person person = personDao.queryById(1);
System.out.println(JSONObject.toJSONString(person));
}catch (Exception e){
System.out.println(e);
}
}
运行后结果为:

注意点:
1、main方法中的代码是mybatis最基础的使用之一,实际使用时基本不需要指定mybatis-config.xml配置文件,甚至不用写这个文件,也不用生成SqlSession等。这里因为研究源码才这么编辑。
2、try catch使用了流自动关闭的写法,就是使用 try(){}catch{}形式。该模式可以优化代码结构,对其感兴趣可以查看这篇文章:try(){}catch{} 自动对资源的关闭_Interest1_wyt的博客-CSDN博客
本文探讨MyBatis如何定位SQL文件、解析SQL标签、绑定接口方法及执行SQL获取结果。提供SpringBoot环境下示例代码。
demo创建&spm=1001.2101.3001.5002&articleId=121963271&d=1&t=3&u=f17fb0a374a349708efe0c801c600659)
1万+

被折叠的 条评论
为什么被折叠?



