《MyBatis》读书笔记 - 入门

一、入门

1. 安装

构建项目,需要引入如下的 dependency 到 pom.xml 中:

<dependency>
  <groupId>or.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>x.x.x</version>
</dependency>

2. 从XML配置SQLSessionFactory

每个基于 MyBatis 的应用都是以一个 SqlSessionFactory 的实例为核心的。

以下 XML 配置文件中包含了对 MyBatis 系统的核心设置,如获取数据源(DataSource)和决定事务作用于和控制方式的事务管理器(TransactionManager):

<?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 id="development">
        <transactionManager type="JDBC" />
        <dataSource type="POOLED">
            <property name="driver" value="${driver}"/>
            <property name="url" value="${url}"/>
            <property name="username" value="${username}"/>
            <property name="password" value="${password}"/>
        </dataSource>
    </environments>
    <mappers>
        <mapper resource="org/mybatis/example/BlogMapper.xml"/>
    </mappers>
</configuration>

使用类路径下的资源文件进行配置,构建 SqlSessionFactory 实例:

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

3. 从SqlSessionFactory 获取 SqlSession

SqlSession 中完全包含了面向数据库执行 SQL 命令所需的所有方法,例如:

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

或者使用一种更简单的方式:

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

提示:命名空间(Namespaces)

命名空间的作用有两个,一个是利用更长的完全限定名来将不同的语句隔离开来,同时也实现了接口绑定。

命名解析:为了减少输入量,MyBatis 对所有的命名配置元素(包括语句,结果映射,缓存等)使用了如下的命名解析规则。

  1. 完全限定名,将被直接用于查找和使用
  2. 短名称,如果全局唯一也可作为单独的应用。如果不唯一,那么使用时就会出现错误,此时就需要使用完全限定名

4. 基于 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" resultType="Blog">
        select * from Blog where id = #{id}
    </select>
</mapper>

5. 基于注解的示例

public interface BlogMapper {
    @Select("SELECT * FROM blog WHERE id = #{id}")
    Blog selectBlog(int id);
}

6. 作用域(Scope)和生命周期

依赖注入框架可以创建线程安全的、基于事务的 SqlSession 和映射器,并将它们直接注入到你的 bean 中,因此可以直接忽略它们的生命周期。

SqlSessionFactoryBuilder

这个类可以被实例化、使用和丢弃,一旦创建了 SqlSessionFactory,就不再需要了。

因此该实例的最佳作用域为方法作用域,可以重用 SqlSessionFactoryBuilder 来创建多个 SqlSessionFactory,但是使用后最好不要让其一直存在,以保证 XML 资源可以被释放。

SqlSessionFactory

一旦创建就应该在应用的运行期间一直存在,尽量不要重复创建该实例。

因此该实例的最佳作用域为应用作用域。可以使用单例模式或静态单例模式来做到。

SqlSession

每个线程都应该由自己的 SqlSession 实例。SqlSession 的实例不是线程安全的,因此不能被共享。

因此该实例的最佳作用于为请求或方法作用域。绝不能将 SqlSession 的实例引用放在静态域中,甚至一个类的实例变量也不行,也不能将引用放在任何类型的托管作用域中(如Servlet 的HttpSession)。

如果您使用的是 Web 框架,那么应该考虑将 SqlSession 放在和 Http 请求对象相似的作用域中。即每次 Http 请求,都打开一个 SqlSession,返回响应后就关闭它。

映射器实例

映射器是映射Sql语句的接口,该接口的实例是从 SqlSession 中获得的。

因此该实例的最佳作用于与 SqlSession 的作用域保持一致

文章作者: koral
文章链接: http://luokaiii.github.io/2019/09/05/读书笔记/《Mybatis》/1_入门/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自