您的位置:首页 > 其它

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

2015-08-30 11:15 471 查看
1. transactionManager:

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

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

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

2.dataSource:

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

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

POOLED:用上连接池

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

3.properties

<!-- 引入jdbc配置信息 -->
<properties resource="jdbc.properties"></properties>
个人觉得最好使用.properties文件来配置jdbc.

也可以这样配置:

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


4.typeAlliase:

取别名.

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


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

<typeAliases>
<package name="domain.blog"/>
</typeAliases>


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

@Alias("author")
public class Author {
...
}

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.引入映射文件

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

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


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

<package name="com.skymr.mybatis.mappers"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: