属性优化
环境配置(environments)
mybatis可以配置多种环境,但每个 SqlSessionFactory实例只能选择一种环境
Mybatis默认的事务管理器是 JDBC,连接池:POOLED
属性(properties)
可以通过properties属性来实现引入配置文件
思路:
properties可以在外部进行配置,进行动态替换
主要思想:
在外面写一个 db.properties,然后在 mybatis-config.xml中进行引入
mybatis-config.xml
<?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>
<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="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/geng/dao/UserMapp.xml"/>
</mappers>
</configuration>
db.properties
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=UTF8&useSSL=false
username=root
password=123456
- 可以直接引入外部文件
- 可以在其中添加一些属性配置
- 如果两个文件有同一个字段,优先使用外部配置文件的!
别名优化(Alias)
类型别名
目的:用来减少完全限定名的长度
mybatis-config.xml
<typeAliases>
<typeAlias type="com.geng.pojo.User" alias="uuuu"/>
</typeAliases>
UserMapp.xml
<mapper namespace="com.geng.dao.UserDao">
<select id="getUserList" resultType="uuuu">
select * from mybatis.user
</select>
</mapper>
包别名
指定一个包名,mybatis会在包名下面搜索需要的JavaBean。在没有注解的情况下,会使用 Bean 的首字母小写的非限定类名来作为它的别名
mybatis-config.xml
<typeAliases>
<package name="com.geng.pojo"/>
</typeAliases>
UserMapp.xml
<mapper namespace="com.geng.dao.UserDao">
<select id="getUserList" resultType="user">
select * from mybatis.user
</select>
</mapper>
设置
cacheEnabled,全局性的开启或关闭所有映射器配置文件中已配置的任何缓存;
lazyLoadingEnabled,延迟加载的全局开关。当开启时,所有关联对象都会延迟加载。特定关联关系中可通过设置 fetchType 属性来覆盖该项的开关状态。
logImpl,指定Mybatis所用日志的具体实现,未指定时将自动查找。