下面介绍 MyBatis 的核心配置文件 SqlMapConfig.xml 的配置实用:
SqlMapConfig.xml 中配置的内容和顺序如下:
properties(属性)、settings(配置)、typeAliases(类型别名)、typeHandlers(类型处理器)、objectFactory(对象工厂)、plugins(插件)、
environments(环境集合属性对象)
environment(环境子属性对象)
transactionManager(事务管理)
dataSource(数据源)
databaseIdProvider(数据库ID提供者)
1. properties(属性)
SqlMapConfig.xml 可以引用 Java 属性文件中的配置信息,如下:
在classpath 下定义 database.properties 文件
#Database configuration
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
mysql.username=root
mysql.password=mysql
oracle.driver=oracle.jdbc.driver.OracleDriver
oracle.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
oracle.username=dog
oracle.password=dog
在 SqlMapConfig.xml 中引用:
标签引入配置文件,${XXX} 调用配置文件中的属性
2. settings(配置)
MyBatis 的全局配置参数,全局参数的修改将会影响MyBatis的运行行为。MyBatis会自动进行性能优化。
3. typeAliases(类型别名)
a. MyBatis 支持的别名:
别名 映射的类型
_byte byte byteByte
_long long long Long
_short short short Short
_int int int Integer
_integer int integer Integer
_double double double Double
_float float float Float
_boolean boolean boolean Boolean
string String date Date
decimal BigDecimal bigdecimal BigDecimal
b. 自定义别名:
方法一:在SqlMapConfig.xml 中配置:
然后在UserMapper.xml 文件中使用别名代替原来的类路径即可。
select * from users where userId = #{userId}
方法二:使用注解 @Alias("user")
@Alias("user")
public class User {
private int userId;
private String username;// 用户姓名
4. typeHandlers(类别处理器)
类型处理器的作用是:将 Java类型和 Sql 映射文件进行映射。如下:
select * from users where userId = #{userId}
parameterType:指定输入参数类型为int,即向statement 设置值
resultType:指定输出数据类型为自定义User,即将 ResultSet 转为 Java 对象
MyBatis 自带的类型处理器基本上能够满足日常需要,不太需要单独定义。
MyBatis 支持类型处理器:
类型处理器
Java 类型
JDBC 类型
BooleanTypeHandler
Boolean,boolean
任何兼容的布尔值
ByteTypeHandler
Byte,byte
任何兼容的数字或者字节类型
ShortTypeHandler
Short,short
任何兼容的数字或短整型
IntegerTypeHandler
Integer,int
任何兼容的数字和整型
LongTypeHandler
Long,long
任何兼容的数字或长整型
FloatTypeHandler
Float,float
任何兼容的数字或单精度浮点型
DoubleTypeHandler
Double,double
任何兼容的数字或双精度浮点型
BigDecimalTypeHandler
BigDecimal
任何兼容的数字或十进制小数类型
StringTypeHandler
String
CHAR 和 VARCHAR类型
ClobTypeHandler
String
CLOB 和 LONGVARCHAR类型
NStringTypeHandler
String
NCHAR 和 NVARCHAR类型
NClobTypeHandler
String
NCLOB类型
ByteArrayTypeHandle
byte[]
任何兼容的字节流类型
BlobTypeHandler
byte[]
BLOB 和 LONGVARBINARY类型
DateTypeHandler
Date(java.util)
TIMESTAMP类型
DateOnlyTypeHandler
Date(java.util)
DATE类型
TimeOnlyTypeHandler
Date(java.util)
TIME类型
SqlTimestampTypeHandler
Timestamp(java.sql)
TIMESTAMP类型
SqlDataTypeHandler
Date(java.sql)
DATE类型
SqlTimeTypeHandler
Time(java.sql)
TIME类型
ObjectTypeHandler
任意
其他或未指定类型
EnumTypeHandler
Enumeration类型
VARCHAR-任何兼容的字符串类型,作为代码存储(而不是索引)
5. envrionments(环境集合属性对象)
MyBatis 可以配置多种环境,将SQL 映射应用于多种数据库之中。
a. 首先在 SqlMapConfig.xml 中配置多个环境
b. 同样的写Sql映射和对应的Mapper接口
c. 在代码中根据 environment 获取SqlSessionFactory
//获取SqlSessionFactory
//environment参数:("development_oracle")
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, "development_oracle");
//获取SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//指定 mapper 接口的类型,MyBatis通过动态代理的方式实现mapper接口
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findUserById(100101);
select * from users where userId = #{userId}
select * from users where userId = #{userId}
public User findUserById(int userId) throws Exception;
public User findUserById_mysql(int userId) throws Exception;
如上这样做,会需要两套 Mapper 接口(sql配置的ID 、接口名 都不相同)来针对不同的数据库?如何解决?? 解决方法:利用 databaseIdProvider
6. databaseIdProvider(数据库ID提供者)
在使用 JDBC 操作数据库时通过 connnection.getMetaData().getDatavaseProductName() 方法可以获取数据库产品信息,不同的数据库可以获取不同的信息,不过获取的信息字符串很多,这里使用关键字匹配的方法将数据库产品名称简化,如获取到的数据库产品名称含有Oracle的则简化为oracle,目的是要在 UserMapper.xml 映射为那件中指定sql用在哪个数据库中。例如:在 SqlMapConfig.xml 中添加
这样,在写 UserMapper.xml 映射文件的SQL时,只需要加上 databaseId 属性,就可以使用相同的Sql Id:
select * from users where userId = #{userId}
select * from users where userId = #{userId}
Mapper接口就可以使用同一个
public User findUserById(int userId) throws Exception;
//public User findUserById_mysql(int userId) throws Exception;
在执行时同第5点,根据 environment 获取 SqlSessionFactory。
7. mappers (映射器)
映射器有四种指定方式: