Mybatis源码阅读(一)

准备

  1. mybatis-3 Github源码地址
  2. mybatis 官方指导文档

须知

  1. 此次阅读的 mybatis 源码版本为 3.5.3
  2. 官方下载的源码无法直接使用,pom文件做了改动,注释了部分依赖,也修改了一些依赖的版本,作为学习,只需保证编译运行调试通过即可。
  3. 增加了测试代码,即可运行调试。

工程结构

在这里插入图片描述

测试

测试主入口

public class Main {

    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        try {
            User user = sqlSession.selectOne("com.mybatis.read.UserDao.selectUser", 1);
        } finally {
            sqlSession.close();
        }
    }
}

mybatis-config 配置文件

<?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>
    <!--configuration下的标签有顺序-->
    <properties resource = "config.properties"/>
    <settings>
        <!-- 设置日志打印-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mybatis/UserMapper.xml"/>
    </mappers>
</configuration>

mysql配置文件

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=root

UserMapper.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">

<mapper namespace="com.mybatis.read.UserDao">
    <select id="selectUser" resultType="com.mybatis.read.User">
        SELECT * FROM User where id = #{id}
    </select>
</mapper>

User

public class User {
    private String name;
    private String age;
    private String des;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getDes() {
        return des;
    }

    public void setDes(String des) {
        this.des = des;
    }
}

UserDao

public interface UserDao {
    User selectUser(Long id);
}

createSQL

CREATE TABLE `user` (
  `id` int(32) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) DEFAULT NULL,
  `age` int(32) DEFAULT NULL,
  `des` varchar(32) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

启动调试

启动main方法,运行测试,打上断点后即可启动DEBUG模式调试。

在这里插入图片描述
调试的源码已上传到github,mybatis源码阅读,需要的话自行下载。

推荐阅读更多精彩内容