继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Mybatis源码解析之四大对象

刘信坚
关注TA
已关注
手记 52
粉丝 7795
获赞 665

(注:如果图片有些不清楚的话,访问 Mybatis源码解析之四大对象)

一 环境准备

以一个最简单的例子来看看Mybatis的运行流程以及Mybatis四大对象起了什么作用。

https://img4.mukewang.com/5b5094d20001acce02920394.jpg

Employee.java

package com.lxj.bean;

public class Employee {

	private Integer id;
	private String lastName;
	private String email;
	private String gender;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + "]";
	}

}


EmployeeMapper.java

package com.lxj.mapper;

import com.lxj.bean.Employee;

public interface EmployeeMapper {
	  public Employee getEmpById(Integer id);
}


EmployeeMapper.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.lxj.mapper.EmployeeMapper">
	<select id="getEmpById" resultType="com.lxj.bean.Employee">
		select * from tba_emp where id = #{id}
	</select>
</mapper>


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="mapUnderscoreToCamelCase" value="true"/>
       <!--使用驼峰命名法: JAVA 的 lastName对应数据库的last_name    -->
    </settings>
	<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/mybaits" />
				<property name="username" value="root" />
				<property name="password" value="Quit__12" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
	        <!-- mapper文件在类路径的位置-->
		<mapper resource="com/lxj/mapper/EmployeeMapper.xml" />
	</mappers>
</configuration>



二  测试

测试代码如下:

public class MybatisTest {

	@Test
	public void testMybatisSource() throws IOException {
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		SqlSession openSession = sqlSessionFactory.openSession();
		try {
			EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
			Employee employee = mapper.getEmpById(1);
			System.out.println(employee);
		}finally {
			openSession.close();
		}
	
	}

	private SqlSessionFactory getSqlSessionFactory() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		return sqlSessionFactory;
	}

}


https://img1.mukewang.com/5b50970700018c8712150624.jpg

可以看到运行一切正常,下面就开始从获取SqlSessionFactory开始打一个断点:

https://img2.mukewang.com/5b5097fb0001287011490182.jpg

直接step Into 

https://img4.mukewang.com/5b5098980001f02311110357.jpg

https://img1.mukewang.com/5b50994c0001355608780204.jpg

https://img3.mukewang.com/5b5099c30001754511820481.jpg

https://img.mukewang.com/5b5099ff0001c6cc09630632.jpg

https://img3.mukewang.com/5b509bb40001487a12640719.jpg

https://img2.mukewang.com/5b509bf70001208508760269.jpg

https://img3.mukewang.com/5b509c1300011ae409710094.jpg

https://img2.mukewang.com/5b509ca20001b93213720364.jpg

https://img1.mukewang.com/5b509ceb0001b84411200391.jpg

https://img.mukewang.com/5b509d6f0001ab2e10210314.jpg

https://img2.mukewang.com/5b509dfa00015eef13040299.jpg

https://img2.mukewang.com/5b509e860001070a12290471.jpg

https://img3.mukewang.com/5b509eb60001cc9d12930125.jpg

https://img3.mukewang.com/5b509edd0001014a08700246.jpg

https://img.mukewang.com/5b509f2000016a4e10530332.jpg

https://img4.mukewang.com/5b50a0220001c3a310010585.jpg

https://img1.mukewang.com/5b50a07a0001744910500377.jpg

https://img.mukewang.com/5b50a11400010ffc12750675.jpg

https://img.mukewang.com/5b50a20c000156d410460187.jpg

https://img1.mukewang.com/5b50a29f0001f24310180527.jpg

https://img2.mukewang.com/5b50a3170001aa6211720269.jpg

https://img3.mukewang.com/5b50a37c0001fe8e10980332.jpg

https://img2.mukewang.com/5b50a40100010dfc08120347.jpg

https://img2.mukewang.com/5b50a4320001c3d011420176.jpg

https://img3.mukewang.com/5b50a4770001d9b009700284.jpg

https://img1.mukewang.com/5b50a53d0001a1a912970790.jpg

https://img3.mukewang.com/5b50a5d60001ea2a11970432.jpg

https://img2.mukewang.com/5b50a645000181b412470635.jpg

https://img3.mukewang.com/5b50a69f000193e812800312.jpg

https://img1.mukewang.com/5b50a72500011c1f13130270.jpg

https://img2.mukewang.com/5b50a76a0001ac0f14030156.jpg

https://img4.mukewang.com/5b50a7b90001863613780620.jpg

https://img1.mukewang.com/5b50a8130001f57514270498.jpg

https://img.mukewang.com/5b50a8560001c5e814050132.jpg

https://img.mukewang.com/5b50a88400017c3f14270169.jpg

StatementHandler 借助ParameterHandler设置参数,借助ResultSetHandler处理结果

https://img2.mukewang.com/5b50a97700012ad811780703.jpg

https://img3.mukewang.com/5b50a9d90001b41d11910140.jpg

三 源码分析

https://img2.mukewang.com/5b50ab780001397207210770.jpg

StatementHandler:处理sql语句预编译,设置参数等相关工作;

ParameterHandler:设置预编译参数用的

ResultSetHandler:       处理结果集

TypeHandler:在整个过程中,进行数据库类型和javaBean类型的映射

时序图如下:

https://img.mukewang.com/5b50abd90001736713510734.jpg

https://img1.mukewang.com/5b50abec000115f412150741.jpg

https://img.mukewang.com/5b50abfa0001c72a12450730.jpg

https://img2.mukewang.com/5b50ac0a00014a2d11520718.jpg


四 总结

    Mybatis基本运行流程就到这里,自己过一遍源码比看几遍更有效率, 如果有什么问题请留言。




打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP