搭建阅读环境
Mybatis源码解析一——搭建阅读环境
Mybatis源码解析二——解析器模块(parsing)
Mybatis源码解析三——反射模块(Reflector)
前言
在阅读源码之前,我们需要搭建好源码阅读的环境。需要使用到的依赖有IDEA,GIT,MAVEN。一、拉取源码
先从官方仓库 Mybatis fork出自己的代码库,再从自己的代码库中拉取文件。
二、包结构
三、test包结构
Mybatis各个模块都有对应的test包,这对源码调试有非常大的帮助
四、运行
运行org.apache.ibatis.autoconstructor包下的测试方法,验证Mybatis是否正常工作。
在这之前需要关注几个事情
4.1测试数据库创建
MyBatis提供了测试文件 createDB.sql
DROP TABLE subject
IF EXISTS;
DROP TABLE extensive_subject
IF EXISTS;
CREATE TABLE subject (
id INT NOT NULL,
name VARCHAR(20),
age INT NOT NULL,
height INT,
weight INT,
active BIT,
dt TIMESTAMP
);
CREATE TABLE extensive_subject (
aByte TINYINT,
aShort SMALLINT,
aChar CHAR,
anInt INT,
aLong BIGINT,
aFloat FLOAT,
aDouble DOUBLE,
aBoolean BIT,
aString VARCHAR(255),
anEnum VARCHAR(50),
aClob Text,
aBlob LONGTEXT,
aTimestamp TIMESTAMP
);
INSERT INTO subject VALUES
(1, 'a', 10, 100, 45, 1, CURRENT_TIMESTAMP),
(2, 'b', 10, NULL, 45, 1, CURRENT_TIMESTAMP),
(2, 'c', 10, NULL, NULL, 0, CURRENT_TIMESTAMP);
INSERT INTO extensive_subject
VALUES
(1, 1, 'a', 1, 1, 1, 1.0, 1, 'a', 'AVALUE', 'ACLOB', 'aaaaaabbbbbb', CURRENT_TIMESTAMP),
(2, 2, 'b', 2, 2, 2, 2.0, 0, 'b', 'BVALUE', 'BCLOB', '010101010101', CURRENT_TIMESTAMP),
(3, 3, 'c', 3, 3, 3, 3.0, 1, 'c', 'CVALUE', 'CCLOB', '777d010078da', CURRENT_TIMESTAMP);
原文件应该是基于db2数据库的,这里我改为使用mysql。
4.2配置数据源
mybatis-config.xml
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- autoMappingBehavior should be set in each test case -->
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC">
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://ip:3306/mybatis?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="****"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/apache/ibatis/autoconstructor/AutoConstructorMapper.xml"/>
</mappers>
</configuration>
4.3运行
启动org.apache.ibatis.autoconstructor.AutoConstructorTest#fullyPopulatedSubject测试类
观察日志
成功查询到数据,至此,说明我们的MyBatis环境没有问题,接下来就可以愉快的debug了。