Mybatis源码解析之四大对象

一 环境准备

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

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;
	}

}

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

直接 Step Into

 

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

三 源码分析

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

ParameterHandler:设置预编译参数用的

ResultSetHandler:       处理结果集

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

时序图如下:

四 总结

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

发布了54 篇原创文章 · 获赞 71 · 访问量 12万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览