大家好,我是田哥

很多人都最讨厌源码,可是又感觉必须看,为什么感觉必须看?因为很多时候面试官喜欢问:你看过什么源码? 如果你肯多某个框架的源码,比如:Spring、Dubbo、MyBatis等其中的一个源码,那面试官至少可能好好的探讨一下。

其实,要想把一个框架的源码看完那到也不是很有必要,但是主要的功能源码部分肯定得熟悉。不建议大家一开始就去看Spring源码,因为Spring太庞大了,Dubbo很多人又没用过,整体来说MyBatis不管是难度系数还是使用率,都是相对适中的。

所以,我特意给大家安排了一个MyBatis源码分析专栏,本专栏是从入门到源码分析,也是市面上所讲的《从入门到精通》

本系列文章重点从下面这个案例开始:

案例

看我是如何轻松拿下MyBatis源码_java-ee

pom.xml依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.tian</groupId>
    <artifactId>mybatis-demo</artifactId>
    <version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.7</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.16</version>
    </dependency>
</dependencies>
</project>

mybatis整体配置文件: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>
    <settings>
       <!-- <setting name="cacheEnabled" value="true" />-->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mblog?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false&serverTimezone=UTC"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mappers/UserMapper.xml"/>
        <mapper url="" class="" resource="" ></mapper>
    </mappers>
</configuration>

实体类User:

package com.tian.eneity;


public class User {
    private Integer id;
    private String name;
    private Integer age;
    //set get toString
}

UserMapper接口:

package com.tian.mapper;

import com.tian.eneity.User;
import org.apache.ibatis.annotations.Param;

public interface UserMapper {
    User selectUserById(@Param("id") Integer id);
}

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.tian.mapper.UserMapper">
<cache/>
<select id="selectUserById" resultType="com.tian.eneity.User" useCache="true">
        select * from m_user where id = #{id}
    </select>
</mapper>

MybatisApplication类:

package com.tian;

import com.tian.eneity.User;
import com.tian.mapper.UserMapper;
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 java.io.IOException;
import java.io.InputStream;

public class MybatisApplication {

    public static void main(String[] args) {
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        SqlSession sqlSession = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            //工厂模式
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            //sql操作会话
            sqlSession = sqlSessionFactory.openSession();
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            //获取数据并解析成User对象
            User user = userMapper.selectUserById(1);
            System.out.println(user);
            User user1 = userMapper.selectUserById(1);
            System.out.println(user1);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            sqlSession.close();
        }
    }
}

然后,我们采用debug模式,运行上面这段代码,可以逐行跑。同时,我们记录好执行步骤,遇到哪些疑问难点,然后在针对性的学习,比如说:JDK动态代理不会,那就可以快速去复习一下(也有专门的文章讲代理模式、动态代理),这样就更牛理解框架源码。

好了,先忙是本专栏的内容,我也会不断优化,不断完善。

专栏内容

看我是如何轻松拿下MyBatis源码_java_02

另外,凡是购买这个专栏的朋友,可以获取一份MyBatis源码(中文注释版)

专栏地址:MyBatis源码分析(菜鸟版)