最近想用MyBatis 做为ORM, 学习记录, 大部份内容来自官方文档。 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
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(); }
<?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