Mybatis学习之路2

配置解析

核心配置文件

<?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">
    <environment 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>
    </environment>
  </environments>
  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration>

环境配置(environments)

  • 每个数据库对应一个 SqlSessionFactory 实例
  • 可以配置多个环境,但每个 SqlSessionFactory 实例只能选择一种环境

事务管理器(transactionManager)

在 MyBatis 中有两种类型的事务管理器(也就是 type="[JDBC|MANAGED]")

数据源(dataSource)

UNPOOLEDPOOLEDJNDI

属性(properties)

编写一个配置文件

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis_db?serverTimezone=UTC
user=root
password=123456
<configuration>
    <properties resource="db.properties"/>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${user}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
   ...
</configuration>

类型别名(typeAliases)

类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写

<configuration>
	...
    <typeAliases>
        <typeAlias alias="User" type="com.youzi.pojo.User"/>
    </typeAliases>
    ...
</configuration>

对应的 mapper.xml 就可以直接使用别名

<select id="getUserList" resultType="User">
    select * from mybatis_db.user;
</select>

也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean

扫描实体类的包,默认别名就为该类的类名,首字母小写(大小写都能查询到)

<typeAliases>
    <package name="com.youzi.pojo"/>
</typeAliases>
<select id="getUserList" resultType="user">
    select * from mybatis_db.user;
</select>

扫描包也可以通过注解改变别名

@Alias("hello")
public class User {
    ...
<select id="getUserList" resultType="hello">
    select * from mybatis_db.user;
</select>

设置(settings)

这是 MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。

cacheEnabled、lazyLoadingEnabled、logImpl...详细查看 Mybatis 官方文档

映射器(mappers)

告诉 MyBatis 到哪里去找映射文件

方式一:

<mappers>
    <mapper resource="com/youzi/dao/UserMapper.xml"/>
</mappers>

方式二:

  • 接口和 Mapper 配置文件对必须同名
  • 接口和 Mapper 配置文件必须在同一个包中
<mappers>
    <mapper class="com.youzi.dao.UserMapper"/>
</mappers>

方式三:

  • 接口和 Mapper 配置文件对必须同名
  • 接口和 Mapper 配置文件必须在同一个包中
<mappers>
    <package name="com.youzi.dao"/>
</mappers>

作用域(Scope)和生命周期

作用域和生命周期类别是至关重要的,因为错误的使用会导致非常严重的并发问题

SqlSessionFactoryBuilder:

  • 一旦创建了 SqlSessionFactory,就不再需要
  • 最佳作用域是方法作用域(也就是局部方法变量)

SqlSessionFactory:

  • 一旦被创建就应该在应用的运行期间一直存在
  • 最佳作用域是应用作用域
  • 使用单例模式或者静态单例模式

SqlSession:

  • 每个线程都应该有它自己的 SqlSession 实例
  • SqlSession 的实例不是线程安全的,因此是不能被共享的,所以它的最佳的作用域是请求或方法作用域
  • 关闭操作很重要

ResultMap

为什么要使用?

当实体类中有属性和数据库表中的字段不同名时,直接使用查询到的结果就会出错

public class User {
    private int id;
    private String name;
    private String password;
    ...
}
<select id="getUserList" resultType="user">
    select * from user;
    <!--select id,name,password from user-->
</select>
<select id="getUserById" resultType="user">
    select * from user where id = #{uId};
    <!--select id,name,password from user where id = #{uId}-->
</select>

User{id=1, name='zhangsan', password='null'}

此时虽然也可以使用 select id,name,pwd as password from ... 在Mybatis中有更好的解决办法就是配置 resultMap 映射结果集

映射名字不同的字段即可

<resultMap id="userResultMap" type="user">
    <result property="password" column="pwd"/>
</resultMap>
...
<select id="getUserById" resultType="user">
    select id,name,pwd as password from user where id = #{id};
</select>
...

此时去掉了 resultType 添加了所配置的 resultMap 现在就可以成功的查询到结果了

User{id=1, name='zhangsan', password='112233'}
原文地址:https://www.cnblogs.com/wangjr1994/p/12448855.html