正文
3.Mybatis全局配置文件属性详解(SqlMapConfig.xml)
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
视频地址:http://edu.51cto.com/sd/be679
首先我们要知道一点,该配置文件的中属性的存放是有顺序的,没有办法随意的乱放。如果你属性的配置位置出错,会有如下错误提示:
The content of element type "configuration" must match "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)".
这个错误的大概意思是说,在configuration里面的属性存放必须按照红体字那些顺序存放。
接下来我们按照顺序来看一下这些配置属性到底是什么意思:
1.properties属性:加载属性的配置文件
比如:这个数据源信息,应该是放在一个db.properties里面然后在在数据源里引用的。
<properties resource="db.properties"></properties>//把db.properties的文件引用进来
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
2.第二个配置信息<settings>
Mybatis在运行的时候可以调整的一些参数
比如:是否开启缓存,延迟加载就是在setting中配置,这里把延迟加载和开启二级缓存的配置贴在下面
<!-- 开启延迟加载 -->
<settings>
<!-- lazyLoadingEnabled:延迟加载启动,默认是false -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- aggressiveLazyLoading:积极的懒加载,false的话按需加载,默认是true -->
<setting name="aggressiveLazyLoading" value="false"/>
<!-- 开启二级缓存,默认是false -->
<setting name="cacheEnabled" value="true"/>
</settings>
这个全局的参数别随便乱配,因为它会影响Mybatis的运行
3.typeAliases 别名(这个属性用的非常平繁)
默认支持的别名类型:我们在用parameterType指定输入参数类型的时候需要写全称,但是比如:java.lang.Integer也可以写int就是因为这个默认支 持的别名型大概支持的默认类型如下(没有全部列出来)
自定义别名:
对单个的定义:
<typeAliases>
<!--
type:就是你要为那个类去别名的路径:包名+类型
alias:就是你自己取的别名
-->
<typeAlias type="com.etc.entity.Student" alias="student"/>
</typeAliases>
引用别名:本来该写 com.etc.entity.Student现在只要写alias里的名字就好
批量定义别名:
<!--
Mybatis批量定义别名:通过指定包名来定义
就是内部去定义这个取别名的规则:
规则:Mybatis会自动去扫描这个包底下的类,并且Mybatis自定义别名,别名就是类名(首字母大小写都可以)
-->
<package name="com.etc.entity"/>
注意:扫描包,Mybatis自定义别名,别名就是类型首字母的大小写都可以
typeHanlders:类型处理器
Mybatis中是通过typeHanlders完成java类型和数据库类型的转换,Mybatis内部定义的类型已经足够我们去使用,就不需要自己在去定义。
environments:运行环境(数据源等等)
Mappers:关联配置文件(就是mybatis写在xml中的数据库操作,需要告诉这个全局配置文件),主要有一下三种方式
单个文件:
<mappers>
<mapper resource="mapper/StudentMapper.xml"/>
</mappers>
类加载:
<!--
通过类去加载该类的配置文件
规则:mapper接口和mapper.xml映射文件的名称必须一致
:在同一个目录
-->
<mapper class="com.etc.mapper.StudentMapper"/>
批量加载:
<!--批量加载:也是扫描这个包底下的所有东西 仍然要遵守类加载的原则 -->
<package name="com.etc.mapper"/>
以上就是这些就是配置文件的详细解析。