MyBatis官方小例代码剖析

来源:互联网 发布:东航巴黎昆明 知乎. 编辑:程序博客网 时间:2024/05/02 02:09

mybatis历史

 ibatis本是apache的一个开源项目,2010年这个项目由apache software foundation 迁移到了google code,并且改名为mybatis。

mybatis官方网址

官方首页:http://www.mybatis.org/


用到的表如下:

 

Sql代码  
  1. CREATE TABLE `Blog` (  
  2.   `id` bigint(20) NOT NULL AUTO_INCREMENT,  
  3.   `namevarchar(12) DEFAULT NULL,  
  4.   `remark` varchar(24) DEFAULT NULL,  
  5.   `createtime` datetime DEFAULT NULL,  
  6.   `updatetime` datetime DEFAULT NULL,  
  7.   PRIMARY KEY (`id`)  
  8. ) ENGINE=MyISAM AUTO_INCREMENT=100001 DEFAULT CHARSET=utf8  
  9.   
  10. insert into `Blog` (`id`, `name`, `remark`, `createtime`, `updatetime`) values('1','博客1','博客1',NULL,NULL);  
  11.   
  12. insert into `Blog` (`id`, `name`, `remark`, `createtime`, `updatetime`) values('2','博客2','博客2',NULL,NULL);  
 

mybatis官方小例运行原理

看了一天,显而易见的是,他的运行原来是和我设想的基本一样。

第一步:加载xml,并解析xml。代码如:

 

Java代码  
  1. String resource = "org/mybatis/example/mybatis-config.xml";  
  2.     InputStream inputStream = Resources.getResourceAsStream(resource);  
  3.     SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()  
  4.             .build(inputStream);  
  5.     //  
  6.     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代码 
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper  
  3.   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4.   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5. <mapper namespace="org.mybatis.example.BlogMapper">  
  6.     <select id="selectBlog" parameterType="int" resultType="org.mybatis.example.Blog">  
  7.         select *  
  8.         from Blog where id = #{id}  
  9.   </select>  
  10. </mapper>  
 

第二步:根据map.get("org.mybatis.example.BlogMapper.selectBlog")找到对于的sql语句创建PrepareStatement对象,并执行。 创建PrepareStatement对象的部分代码你可以从类“PreparedStatementHandler”中看到,如:

 

Java代码  
  1. protected Statement instantiateStatement(Connection connection) throws SQLException {  
  2.    String sql = boundSql.getSql();  
  3.    if (mappedStatement.getKeyGenerator() instanceof Jdbc3KeyGenerator) {  
  4.      String[] keyColumnNames = mappedStatement.getKeyColumns();  
  5.      if (keyColumnNames == null) {  
  6.        return connection.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);  
  7.      } else {  
  8.        return connection.prepareStatement(sql, keyColumnNames);  
  9.      }  
  10.    } else if (mappedStatement.getResultSetType() != null) {  
  11.      return connection.prepareStatement(sql, mappedStatement.getResultSetType().getValue(), ResultSet.CONCUR_READ_ONLY);  
  12.    } else {  
  13.   
  14.      return connection.prepareStatement(sql);//本例中调用的就是这里  
  15.    }  
  16.  }  

 

执行sql语句的代码也在此类中,如下

 

Java代码  
  1. public <E> List<E> query(Statement statement, ResultHandler resultHandler) throws SQLException {  
  2.    PreparedStatement ps = (PreparedStatement) statement;  
  3.    ps.execute();  
  4.    return resultSetHandler.<E> handleResultSets(ps);  
  5.  }  
 

第三步 ,根据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 而已。

0 0