1、 核心配置文件
-
mybatis-config.xml
-
Mybatis的配置文件包含了会深深影响Mybatis行为的设置和属性信息
-
configuration(配置)
-
- environments(环境配置)
-
-
- environment(环境变量)
-
-
-
-
- transactionManager(事务管理器)
- dataSource(数据源)
-
-
2 、环境(environments)
Mybatis 可以配置成适应多套环境
尽管可以配置多套环境,但是每个SqlSessionFactory实例只能选择一种环境
学会配置多套环境!
Mybatis默认的事务管理器就是JDBC,连接池POOLED
3、 属性(properties)
我们可以通过properties属性来实现引用配置文件
这些属性可以在外部进行配置,并可以进行动态替换。你既可以在典型的 Java 属性文件中配置这些属性,也可以在 properties 元素的子元素中设置。【db.properties】
编写一个配置文件
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/practice?useSSL=false&useUnicode=true&characterEncoding=UTF-8
username=root
password=123
在核心配置文件中引入(注意顺序)
<?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核心配置文件-->
<configuration>
<!--引入外部配置文件,可以加属性键值对,与配置文件重复时,优先使用配置文件中的-->
<properties resource="db.properties">
<property name="username" value="root"/>
<property name="password" value="11111"/>
</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="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/practice?useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123"/>
</dataSource>
</environment>
</environments>
<!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册!-->
<mappers>
<mapper resource="com/kwok/dao/student/StudentMapper.xml"/>
</mappers>
</configuration>
-
可以直接引入外部文件
-
可以在其中增加一些属性配置
-
如果两个文件有同一个字段,优先使用外部配置文件的
4、类型别名(typeAliases)
类型别名可为 Java 类型设置一个缩写名字。
它仅用于 XML 配置,意在降低冗余的全限定类名书写。
<!--可以给实体类起别名-->
<typeAliases>
<typeAlias type="com.kwok.pojo.Student" alias="Student"/>
<typeAlias type="com.kwok.pojo.Teacher" alias="Teacher"/>
</typeAliases>
优化直接使用别名
也可以指定一个包名,Mybatis会在包名下面搜索需要的Java Bean,比如:
扫描实体类的包,它的默认别名就为这个类的类名,首字母小写(现在大写也可以了)
<!--可以给实体类起别名-->
<typeAliases>
<!--<typeAlias type="com.kwok.pojo.Student" alias="Student"/>
<typeAlias type="com.kwok.pojo.Teacher" alias="Teacher"/>-->
<package name="com.kwok.pojo"/>
</typeAliases>
在实体类比较少的情况,使用第一种方式
如果实体类非常多,使用第二种方式
第一种可以DIY自定义,第二种不行,如果非要取别名,需要使用ibatis的注解功能@Alias
这里注意改自定义别名后,要把所有原先的student的都改成hello使用
5、 设置
这是 MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。
参考官方文档
https://mybatis.org/mybatis-3/zh/configuration.html#settings
目前记这三个:
6 、其他配置
-
- mybatis-generator-core
- mybatis-plus
-
- 通用mapper
7、映射器(mappers)
MapperRegistry:注册绑定我们的Mapper文件
方式一:使用相对于类路径的资源引用【推荐使用】
<!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册!-->
<mappers>
<mapper resource="com/kwok/dao/student/StudentMapper.xml"/>
</mappers>
方式二:使用映射器接口实现类的完全限定类名
<!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册!-->
<mappers>
<!--<mapper resource="com/kwok/dao/student/StudentMapper.xml"/>-->
<mapper class="com.kwok.dao.student.StudentMapper"/>
</mappers>
方式三:将包内的映射器接口实现全部注册为映射器
<!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册!-->
<mappers>
<!--<mapper resource="com/kwok/dao/student/StudentMapper.xml"/>-->
<!--<mapper class="com.kwok.dao.student.StudentMapper"/>-->
<package name="com.kwok.dao"/>
</mappers>
方式四:使用完全限定资源定位符(弃用)
注意点:使用方法二和方法三
- 接口和他的Mapper配置文件必须同名
- 接口和他的Mapper配置文件必须在同一个包下
8、 生命周期和作用域
生命周期,和作用域,是至关重要的,因为错误的使用会导致非常严重的并发问题。
SqlSessionFactoryBuilder
- 一旦创建了SqlSessionFactory,就不再需要他了
- 局部变量
SqlSessionFactory
-
说白了就是可以想象为:数据库连接池
-
SqlSessionFactory一旦被创建就应该在应用运行的期间一直存在,没有任何理由丢弃它或重新创建另一个实例。
-
因此SqlSessionFactory的最佳作用域是应用作用域。
-
字简单的就是使用单例模式或者静态单例模式
SqlSession
-
连接到连接池的一个请求!
-
关闭
-
SqlSession 的实例不是线程安全的,因此是不能被共享的,所以它的最佳的作用域是请求或方法作用域。
-
用完之后需要赶紧关闭,否则资源被占用。
这里的每个Mapper,就代表一个具体的业务!