mybaties源码浅析
1.Mybatis介绍
MyBatis并不是一个完整的ORM框架,其官方首页是这么介绍自己: The MyBatis data mapper framework makes it easier to use a relational database with object-oriented applications. MyBatis couples objects with stored procedures or SQL statements using a XML descriptor or annotations.Simplicity is the biggest advantage of the MyBatis data mapper over object relational mapping tools. ORM是 Object和Relation之间的映射,包括Object->Relation和Relation->Object两方面。Hibernate是个完整的ORM框架,而MyBatis完成的是Relation->Object,也就是其所说的data mapper framework.。
2.整体设计
2.1功能架构设计
2.2接口
3.代码分析
3.1测试代码
public class Test { public static void main(String[] args) throws Exception { String resource = "test-mybatis-config.xml"; Reader reader = Resources.getResourceAsReader(resource); SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(reader); SqlSession session = ssf.openSession(); TestData params = new TestData(); params.setId("1"); session.selectList("com.thinkgem.jeesite.test.dao.TestDataDao.get", params); } }
<?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> <!-- url和resource不能同时配置, url和resource优先级高于内部 --> <properties resource="jeesite.properties"> <property name="jdbc.password" value="1"/> </properties> <environments default="myexample"> <environment id="myexample"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <mappers> <mapper resource="TestDataDao.xml" /> </mappers> </configuration>
3.2测试时序图
3.3源码
3.3.1配置文件解析
配置文件解析使用XMLConfigBuilder来解析
3.2解析sql
通过SqlSource解析得到BoundSql。SqlSource代表从xml文件或注解映射的sql内容,BoundSql保护解析后的sql,参数,参数类型等。
其中SqlNode为xml中sql的每个标签信息
测试代码的sql解析以MappedStatement.getBoundSql(Object parameterObject)开始
注:mybatis为3.2.8版本
上一篇: Hibernate 注解机制
下一篇: hibernate体系结构整理