MyBatis 學習筆記一基本對象

最近想用MyBatis 做爲ORM, 學習記錄, 大部份內容來自官方文檔。 html

官方文檔

http://mybatis.github.io/mybatis-3/zh/getting-started.html


配置文件

和HIBERNATE 同樣, mybatis 同樣提供了一個入口文件。使用以下代碼加載mybatis 配置。 java

String resource = "org/mybatis/example/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);             

Hibernate  git

//org.hibernate.cfg.Configuration
SessionFactory sessions = cfg.buildSessionFactory();
XML config(Simple)
<?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>
  <environments default="development"> //  我的理解 persistence-units
    <environment id="development">     //  persistence-unit
      <transactionManager type="JDBC"/>//  事務類型 
      <dataSource type="POOLED">       //  數據庫鏈接相關配置
        <property name="driver" value="${driver}"/>//driver 相似 hibernate.properties 中加載KEY值
        <property name="url" value="${url}"/>
        <property name="username" value="${username}"/>
        <property name="password" value="${password}"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/> // hbm.xml
  </mappers>
</configuration>

和HIBERNATE 同樣 支持編程式的構建SessionFatory( 暫不關注, 須要看官方文檔) github

SQLSession

mybatis 操做數據庫對象  org.apache.ibatis.session.SqlSession (hibernate org.hibernate.Session)
sql

操做實例  數據庫

SqlSession session = sqlSessionFactory.openSession();
try {
  Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
} finally {
  session.close();
}


MyBatis 提供了一種新的訪問方式, 已減小編程時候的字符串映射錯誤,暫時不採用這種作法
(這個應該和JPA2.O 中的 元數據模型的概念是一致的, 爲了減小運行是錯誤,提供這樣的方式將異常在編譯時察覺, 可是以爲爲每個Modal 提供一個數據模型,實在以爲有點不值得) 
apache

SqlSession session = sqlSessionFactory.openSession();
try {
  BlogMapper mapper = session.getMapper(BlogMapper.class);
  Blog blog = mapper.selectBlog(101);
} finally {
  session.close();
}


探究已映射的 SQL 語句

已映射SQL 定義
<?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="Blog">
    select * from Blog where id = #{id}
  </select>
</mapper>
使用方式
Blog blog = (Blog) session.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101);
比較JPA
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
                 version="2.0">
      <named-query name="TasksAssignedAsBusinessAdministrator">
          <query>
			select * from Blog where id = :id	
          </query>
          <!-- hint name="org.hibernate.timeout" value="200"/ -->
      </named-query>
</entity-mappings>

使用  編程

entityManager.createNameQuery("TasksAssignedAsBusinessAdministrator");
MyBatis 使用影視器來建立已映射SQL -- 優勢類型映射安全
package org.mybatis.example;
public interface BlogMapper {
  @Select("SELECT * FROM blog WHERE id = #{id}")
  Blog selectBlog(int id);
}

MyBatis 映射文件  安全

官方很詳細 http://mybatis.github.io/mybatis-3/zh/configuration.html session

相關文章
相關標籤/搜索