Mybatis学习笔记(二)配置文件

1. transactionManager:

Mybatis支持两种类型的事务管理器,JDBC/MANAGED(托管)

JDBC:应用程序负责管理数据库连接的生命周期

MANAGED:由应用服务器负责管理数据库连接的生命周期,一般商业服务器才有此功能,如Weblogic, Jboss

2.dataSource:

用于配置数据源,类型有:UNPOOLED, POOLED, JNDI.

UNPOOLED:没有连接池,每次操作数据库,MyBatis都会创建一个新的连接,用完后关闭,适合小并发项目,

POOLED:用上连接池

JNDI:使应用服务器配置JNDI数据源获取数据库连接.

3.properties

  1. <!-- 引入jdbc配置信息 -->  
  2. <properties resource="jdbc.properties"></properties>  

个人觉得最好使用.properties文件来配置jdbc.

也可以这样配置:

  1. <properties>  
  2.     <property name="jdbc.driverClassName" value="com.mysql.jdbc.Driver"/>  
  3. </properties>  


4.typeAlliase:

取别名.

  1. <typeAliases>  
  2.     <!-- 别名配置,方便书写 -->  
  3.     <typeAlias alias="Student" type="com.skymr.mybatis.model.Student"/>  
  4. </typeAliases>  


如果有很多类要取别名,使用上面这种方式就太累了

  1. <typeAliases>  
  2.   <package name="domain.blog"/>  
  3. </typeAliases>  

在domain.blog包下找到的所有Bean类,如果没有注解,将会用类名注册别名;可用@Alias注解来定义别名.例:

  1. @Alias("author")  
  2. public class Author {  
  3.     ...  
  4. }  

There are many built-in type aliases for common Java types. They are all case insensitive, note the
special handling of primitives due to the overloaded names.

Mybatis中有为Java类型自建的类型别名,它们是大小写敏感的

Alias Mapped Type
_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
object Object
map Map
hashmap HashMap
list List
arraylist ArrayList
collection Collection
iterator Iterator

5.引入映射文件

  1. <!-- 注册StudentMapper.xml文件, -->  
  2. <mapper resource="com/skymr/mybatis/mappers/StudentMapper.xml"/>  

也可以

  1. <mapper class="com.skymr.mybatis.mappers.StudentMapper"/>  


当文件较多时,最好用package方式

    1. <package name="com.skymr.mybatis.mappers"/> 
原文地址:https://www.cnblogs.com/bkyliufeng/p/6291755.html