MyBatis使用细节

发布时间 2023-10-12 19:30:20作者: yanggdgg

一、别名

在核心配置文件中使用<typeAliases>标签配置别名。

别名可以用于映射文件中的resultType属性。

1.直接配置别名

    <typeAliases>
        <!-- type:类型全限定路径   alias:别名名称 -->
        <typeAlias type="com.gsy.pojo.People" alias="p"></typeAlias>
        <typeAlias type="com.gsy.pojo.People" alias="p2"></typeAlias>
    </typeAliases>

注意:同一个类可以有多个别名;

   且在使用时不区分大小写;

   设置了别名后,类的全限定名依然有效。

2.扫描包配置别名

    <typeAliases>
        <!-- 指定包 -->
        <package name="com.gsy.pojo"/>
    </typeAliases>

MyBatis在解析xml文件时会将指定的包中所有实体类配置别名为类名

注意:使用这种方式时严格区分大小写;

   可以与直接配置别名混用。

3.MyBatis内置的别名

别名映射的类型 别名映射的类型 别名映射的类型
_byte byte   string String   date Date
_long long   byte Byte   decimal BigDecimal
_short short   long Long   bigdecimal BigDecimal
_int int   short Short   object Object
_integer int   int Integer   map Map
_double double   integer Integer   hashmap HashMap
_float float   double Double   list List
_boolean boolean   float Float   arraylist ArrayList
      boolean Boolean   collection Collection
            iterator Iterator