MyBatis官方小例代码辨析

MyBatis官方小例代码剖析

   个人认为从技术上来讲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.*包MyBatis官方小例代码辨析。几乎完全可以肯定spring dao、hibernate、ibatis技术上归根揭底用都是xml api、java反射(注解)、java.sql.* api 而已。

 

………………