5.SqlMapConfig.xml
Global mybatis profile SqlMapConfig.xml, configured as follows:
properties (attributes)
Settings (global parameters)
typeAliases (type alias)
typeHandlers (processor type)
objectFactory (Object Factory)
plugins (plugin)
Environments (Environmental collection property objects)
environment (environment sub-properties of the object)
transactionManager (transaction management)
the dataSource (data source)
by mappers (mapper)
5.1 properties property
demand:
A database arranged in connection parameters individually db.properties, the only need to load the attribute values in db.properties in SqlMapConfig.xml.
In SqlMapConfig.xml database connection it is not required to hard-coded parameters.
The database connection parameters in configuration only db.properties, the reasons: to facilitate unified management of the parameters, you can reference the other xml db.properties.
properties characteristics:
Suggest:
Do not add any properties body attribute value, only the attribute values are defined in the properties file properties.
Property name defined in the properties file to have a certain particularity, such as: xxx.xxx
db.properties:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis jdbc.username=root jdbc.password=root
References SqlMapConfig.xml profile
<! - Load properties file ->
<properties Resource = "db.properties">
<! - this can also be configured in a number of properties in the property name and property value ->
<! - <Property name = "" value = "" /> ->
</ Properties>
...
<the dataSource type = "the 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>
5.2 settings Global Parameter Settings
mybatis framework can adjust some of the operating parameters at runtime.
For example: On second-level cache, on delay loading. . .
Global parameters will affect the runtime behavior of mybatis .
5.3 typeAliases (alias) Key
5.3.1 Demand
In mapper.xml, the definition of many of the statement, statement of the type of input parameters to specify the parameterType, need resultType map of the type specified output.
If the input type in the full path of the specified type, to develop convenient, or can be specified for parameterType resultType types define aliases, the alias is defined in mapper.xml, facilitate the development.
5.3.2mybatis own support aliases
Aliases |
Type mappings |
_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 |
5.3.3 Custom aliases - in SqlMapConfig.xml configuration, the file referenced in the mapper.xml
5.3.3.1 single alias definitions
<!-- 别名定义 -->
<typeAliases>
<!-- 针对单个别名定义
type:类型的路径
alias:别名
-->
<typeAlias type="com.xjs.mybatis.po.User" alias="user"/>
</typeAliases>
5.3.3.2批量定义别名(常用)
<!-- 别名定义 --> <typeAliases> <!-- 批量别名定义 指定包名,mybatis自动扫描包中的POJO类,自动定义别名,别名就是类名(首字母大写或小写都可以) --> <package name="com.xjs.mybatis.po"/> </typeAliases>
5.4 typeHandlers(类型处理器)
mybatis中通过typeHandlers完成jdbc类型和Java类型的转换。
通常情况下,mybatis提供的类型处理器满足日常需要,不需要自定义。
5.5 mappers(映射配置)
5.5.1通过resource加载单个映射文件
使用相对于类路径的资源
<!-- 通过resource方法 一次加载一个映射文件--> <mapper resource="mapper/UserMapper.xml"/>
5.5.2 <mapper url=" " />
使用完全限定路径
如:<mapper url="file:///D:\workspace_springmvc\mybatis_01\config\sqlmap\User.xml" />
5.5.3 通过mapper(dao)接口加载单个mapper
<!-- 通过mapper(dao)接口加载单个映射文件 遵循一些规则:需要将mapper(dao)接口类名和mapper.xml映射文件名称保持一致,且在一个目录。 上边规范的前提是:使用的是mapper代理方法 --> <mapper class="com.xjs.mybatis.mapper.UserMapper"/>
按照上边的规范,将mapper.java和mapper.xml放在一个目录下,且同名。
5.5.4 批量加载多个mapper(推荐使用)
<!-- 批量加载mapper 指定mapper(dao)接口的包名,mybatis自动扫描包下所有的mapper(dao)接口进行加载 遵循一些规则:需要将mapper(dao)接口类名和mapper.xml映射文件名称保持一致,且在一个目录。 上边规范的前提是:使用的是mapper代理方法 --> <package name="com.xjs.mybatis.mapper"/>