_
***配套文档:***↓↓↓
mybatis – MyBatis 3 | 简介
三、配置解析
*** mybatis-config.xml***
1、核心配置文件
- mybatis-config,xml
- MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属性信息。配置文档的顶层结构如下:
2、环境配置(environments)
- 事务管理器(transactionManager)
MyBatis 中有两种类型的事务管理器(也就是 type="[JDBC|MANAGED]"; 默认:JDBC
- 数据源(dataSource)
作用:连接数据库; 默认:POOLED
有三种内建的数据源类型(也就是 type="[UNPOOLED|POOLED|JNDI]"):
3、属性 (properties)
1)编写一个配置文件
- db.properties(外部配置文件)
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306?serverTimezone=GMT&useUnicode=true&useSSL=true&characterEncoding=UTF-8
username=root
password=123456
2)在核心配置文件中映入
<properties resource="db.properties"></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>
</environments>
- 可以直接引入外部文件
- 可以在中增加一些属性
- 如果两个文件有同一个字段,优先使用外部配置文件的
4、类型别名(typeAliases)
- 类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写。
1)方法一:配置文件中配置
<!-- 给实体类起别名-->
<typeAliases>
<typeAlias type="com.pf.pojo.User" alias="User"/>
</typeAliases>
<select id="getUserList" resultType="User">
select * from mybatis.user
</select>
2)方法二:指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean
<!-- 给实体类起别名-->
<typeAliases>
<package name="com.pf.pojo"/>
</typeAliases>
<select id="getUserList" resultType="user">
select * from mybatis.user
</select>
- 在实体类没有注解的情况下,会使用@Alias(“uuuser”)注解来更换别名;否则默认类名小写:user。
@Alias("uuuser")
public class User {
...
}
3)总结:
- 实体类比较少时,使用方法一
- 实体类较多,使用方法二
5、设置(settings)
- 这是 MyBatis 中极为重要的调整设置,它们会改变 MyBatis 的运行时行为。
- 一个配置完整的 settings 元素的示例如下:(不需要了解)
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="multipleResultSetsEnabled" value="true"/>
<setting name="useColumnLabel" value="true"/>
<setting name="useGeneratedKeys" value="false"/>
<setting name="autoMappingBehavior" value="PARTIAL"/>
<setting name="autoMappingUnknownColumnBehavior" value="WARNING"/>
<setting name="defaultExecutorType" value="SIMPLE"/>
<setting name="defaultStatementTimeout" value="25"/>
<setting name="defaultFetchSize" value="100"/>
<setting name="safeRowBoundsEnabled" value="false"/>
<setting name="mapUnderscoreToCamelCase" value="false"/>
<setting name="localCacheScope" value="SESSION"/>
<setting name="jdbcTypeForNull" value="OTHER"/>
<setting name="lazyLoadTriggerMethods" value="equals,clone,hashCode,toString"/
6、 其他配置
- typeHandlers(类型处理器)
- objectFactory(对象工厂)
- plugins(插件)
- mybatis-generator-core
- mybatis-plus
- 通用mapper
7、映射器
- MapperRegistry:注册绑定我们的Mapper文件;
1)方式一:***【推荐使用】***
<mappers>
<mapper resource="com/pf/mapper/UserMapper.xml"/>
</mappers>
2)方式二:使用class文件绑定注册
<mappers>
<mapper class="com.pf.mapper.UserMapper"/>
</mappers>
- 接口和Mapper配置文件必须同名
- 接口和他的Mapper配置文件必须在同一个包下
3)方式三:使用扫描包地址注入绑定
<mappers>
<package name="com.pf.mapper"/>
</mappers>
- 接口和Mapper配置文件必须同名
- 接口和他的Mapper配置文件必须在同一个包下
必须同名
- 接口和他的Mapper配置文件必须在同一个包下
3)方式三:使用扫描包地址注入绑定
<mappers>
<package name="com.pf.mapper"/>
</mappers>
- 接口和Mapper配置文件必须同名
- 接口和他的Mapper配置文件必须在同一个包下
👆👆👆以上为mybatis-config,xml文件的配置解析