sqlMapConfig.xml
SqlMapConfig.xml
中配置的内容和顺序如下:
properties
(属性)
settings
(全局配置参数)
typeAliases
(类型别名)
typeHandlers
(类型处理器)
objectFactory
(对象工厂)
plugins
(插件)
environments
(环境集合属性对象)
environment
(环境子属性对象)
transactionManager
(事务管理)
dataSource
(数据源)
mappers
(映射器)
1.1 properties
属性定义
可以把一些通用的属性值配置在属性文件中,加载到
mybatis
运行环境内。
比如:创建
db.properties
配置数据库连接参数。
注意:在xml文件中,
&->&
,但是在properties文件中,
&->
注意:
MyBatis
将按照下面的顺序来加载属性:
u
在
properties
元素体内定义的属性首先被读取。
u
然后会读取
properties
元素中
resource
或
url
加载的属性,它会覆盖已读取的同名属性。
u
最后读取
parameterType
传递的属性,它会覆盖已读取的同名属性。
建议使用
properties
,不要在
properties
中定义属性,只引用定义的
properties
文件中属性,并且
properties
文件中定义的
key
要有一些特殊的规则。
1.2 settings
全局参数配置
mybatis
运行时可以调整一些全局参数(相当于软件的运行参数),参考:mybatis-settings.xlsx
根据使用需求进行参数配置。
注意:小心配置,配置参数会影响
mybatis
的执行。
ibatis
的全局配置参数中包括很多的性能参数(最大线程数,最大待时间。。。),通过调整这些性能参数使
ibatis
达到高性能的运行,
mybatis
没有这些性能参数,由
mybatis
自动调节。
1.3 typeAliases(
常用
)
可以将
parameterType
、
resultType
中指定的类型 通过别名引用。
1.3.1 mybaits
提供了很多别名
别名 | 映射的类型 |
_byte | byte |
_long | long |
_short | short |
_int | int |
_integer | int |
_double | double |
_float | float |
_boolean | boolean |
string | String |
byte | Byte |
long | Long |
short | Short |
int | Integer |
integer | Integer |
double | Double |
float | Float |
boolean | Boolean |
date | Date |
decimal | BigDecimal |
bigdecimal | BigDecimal |
1.3.2
自定义别名
1.3.3
使用别名
在
parameterType
、
resultType
中使用别名:
1.3.4 typeHandlers
类型处理器将
java
类型和
jdbc
类型进行映射。
mybatis
默认提供很多类型处理器,一般情况下够用了。
1.3.5 mappers