个人认为从技术上来讲mybatis 没什么难的,但或许可以从里面学习一些东西……;最差简历上也可以写:熟悉mybatis , 阅读过去其源代码。
mybatis历史
ibatis本是apache的一个开源项目,2010年这个项目由apache software foundation 迁移到了google code,并且改名为mybatis。
mybatis官方网址
官方首页:http://www.mybatis.org/
因为是第一用,所以运行了的一个官方例子,例子见附件。对了,版本是mybatis-3.1.0。
用到的表如下:
CREATE TABLE `Blog` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(12) DEFAULT NULL,
`remark` varchar(24) DEFAULT NULL,
`createtime` datetime DEFAULT NULL,
`updatetime` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=100001 DEFAULT CHARSET=utf8
insert into `Blog` (`id`, `name`, `remark`, `createtime`, `updatetime`) values('1','博客1','博客1',NULL,NULL);
insert into `Blog` (`id`, `name`, `remark`, `createtime`, `updatetime`) values('2','博客2','博客2',NULL,NULL);
mybatis官方小例运行原理
看了一天,显而易见的是,他的运行原来是和我设想的基本一样。
第一步:加载xml,并解析xml。代码如:
String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
//
SqlSession session = sqlSessionFactory.openSession();
其中,可能会把BlogMapper.xml中的信息放到Configuration对象中的“ protected final Map<String, MappedStatement> mappedStatements = new StrictMap<MappedStatement>("Mapped Statements collection");”成员变量中。
在本例中,mappedStatements 对象的key即包含“org.mybatis.example.BlogMapper.selectBlog”。通过map的get方法你可以获取MappedStatement对象,里面包含sql语句等。关于对应的BlogMapper.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="org.mybatis.example.BlogMapper">
<select id="selectBlog" parameterType="int" resultType="org.mybatis.example.Blog">
select *
from Blog where id = #{id}
</select>
</mapper>
第二步:根据map.get("org.mybatis.example.BlogMapper.selectBlog")找到对于的sql语句创建PrepareStatement对象,并执行。
创建PrepareStatement对象的部分代码你可以从类“PreparedStatementHandler”中看到,如:
protected Statement instantiateStatement(Connection connection) throws SQLException {
String sql = boundSql.getSql();
if (mappedStatement.getKeyGenerator() instanceof Jdbc3KeyGenerator) {
String[] keyColumnNames = mappedStatement.getKeyColumns();
if (keyColumnNames == null) {
return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
} else {
return connection.prepareStatement(sql, keyColumnNames);
}
} else if (mappedStatement.getResultSetType() != null) {
return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);
} else {
return connection.prepareStatement(sql);//本例中调用的就是这里
}
}
执行sql语句的代码也在此类中,如下
public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {
PreparedStatement ps = (PreparedStatement) statement;
ps.execute();
return resultSetHandler.<E> handleResultSets(ps);
}
第三步 ,根据BlogMapper.xml中的resultType="org.mybatis.example.Blog"创建对象Blog。
第四部,利用jdbc中api,java反射中的api把jdbc取出的数据塞入刚才创建的“org.mybatis.example.Blog”对象中,并返回。
唉,数不清的数据库持久层框架,几乎让人没时间去看java.sql.*包。几乎完全可以肯定spring dao、hibernate、ibatis技术上归根揭底用都是xml api、java反射(注解)、java.sql.* api 而已。
………………
分享到:
相关推荐
本文将以五大章节分别阐述系统的开发背景、意义,关键的开发技术:Spring Boot、MySQL、微信小程序,分析思想与方法:用例图、活动图、数据流图;设计理念与设计逻辑,实现效果与实现结果,介绍本系统的设计实现过程...
+ mybatis 1.3.1+ + mysql 5.1.21+ **运行爬虫:** 以爬取虎扑步行街的帖子、用户和评论为例。 1. 初始化数据库 在本地MySQL中创建自己的schema,执行初始化数据库的脚本 [`hupu-spider/src/main/resources/db.sql`...
囊括了功能结构图,用例图,业务流程图,ER结构图,清晰明了,足以满足您在技术上的需求或者直接进行使用,您可以在参考原项目的基础上进行修改,或者直接录入数据信息进行使用。 本系统属于原创代码,如您需要在...
再次,分析了系统总体建设范围,通过用例图、流程图等 UML 建模方式,描述了用户可以执行的功能和业务处理机制,并对软件架构、UI 界面、安全等方面的非功能性需求进行分析。同时,构建了系统体系结构、技术架构与...
积分管理系统java源码 基础知识 java基础 基本类型(占用的内存)和包装类型 数组和对象 程序控制语句,if、switch...分析Mybatis动态代理的真正实现 手动实现Mini版本的Mybatis 分布式 分布式原理 分布式架构的演进过
JDBC:java代码里面用程序进行操作数据库(比较底层的代码实现) MyBatis框架:对数据库进行封装 JAVA EE:核心是JSP、SERVLET Spring框架:JAVA EE体系最重要的框架 Redis:数据库的读写,属于一种内存...
导入结果会丢失精度问题优化更多操作按钮左侧移入内容闪现消失情况修复主子表提交中列隐藏后出现列偏移问题单据打印网页时通过hidden-print隐藏元素表格销毁清除记住选择数据增加表格动态列示例代码生成选择主子表...
4、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 # 商城项目开发过程 ## 处理登录注册功能 ### 1. 分析项目 当需要开发某个项目时,首先,应该分析这个项目中,需要...