MyBatis官方小例代码剖析
来源:互联网 发布:东航巴黎昆明 知乎. 编辑:程序博客网 时间:2024/05/02 02:09
mybatis历史
ibatis本是apache的一个开源项目,2010年这个项目由apache software foundation 迁移到了google code,并且改名为mybatis。
mybatis官方网址
官方首页:http://www.mybatis.org/
用到的表如下:
- 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 而已。
- MyBatis官方小例代码剖析
- 微信小程序「官方示例代码」剖析【下】:运行机制
- MyBatis入门小例
- mybatis官方文档
- mybatis 官方文档查看
- myBatis 官方参考手册
- mybatis官方文档
- Mybatis Github官方下载
- mybatis官方介绍
- MyBatis分页剖析
- MyBatis分页剖析
- 【MyBatis框架】SqlMapConfig剖析
- 【MyBatis框架】SqlMapConfig剖析
- 【MyBatis框架】SqlMapConfig剖析
- MyBatis.2剖析
- mybatis动态代理剖析
- MyBatis原理剖析
- 单例模式代码深度剖析
- Notepad++的ftp远程编辑功能
- MySql数据库的基本操作-表数据中的 唯一约束 & 默认约束
- Leetcode: Balanced Binary Tree
- 配置了两台redis主从failover
- Qt的4个图像类QImage/QPixmap/QBitmap/QPicture 转
- MyBatis官方小例代码剖析
- Java ZIp 压缩和解压
- hdu 1258 Sum It Up (dfs)
- 然而,家乡生活的舒适和安逸,对我的惰性来说,是一大引诱
- QT 操作记录,待实践
- linux中Dcumentation目录下的basic_profiling.txt文档翻译
- 开发小技巧:创建自定义的HTML5输入框验证出错信息
- 《编程之美》学习笔记——指挥CPU占用率
- 2014上海网络赛1004||hdu5045 二分图的最佳匹配 或 状态压缩dp