In the SQL Mapper configuration file, we need to give the fully qualified name of the JavaBeans for the resultType and parameterType attributes.

An example is as follows:

<select id="findStudentById" parameterType="int" resultType="com.mybatis3.domain.Student">
SELECT STUD_ID AS ID, NAME, EMAIL, DOB FROM STUDENTS WHERE STUD_ID=#{Id}
</select>
<update id="updateStudent" parameterType="com.mybatis3.domain.Student">
UPDATE STUDENTS SET NAME=#{name}, EMAIL=#{email}, DOB=#{dob} WHERE STUD_ID=#{id}
</update>

Here we are giving the fully qualified name of the Student type com.mybatis3.domain.Student for the resultType and parameterType attributes.

Instead of typing the fully qualified names everywhere, we can give the alias names and use these alias names in all the other places where we need to give the fully qualified names.

An example is as follows:

<typeAliases>
<typeAlias alias="Student" type="com.mybatis3.domain.Student"/>
<typeAlias alias="Tutor" type="com.mybatis3.domain.Tutor"/>
<package name="com.mybatis3.domain"/>
</typeAliases>

Now in the SQL Mapper file, we can use the alias name Student as follows:

<select id="findStudentById" parameterType="int" resultType="Student">
SELECT STUD_ID AS ID, NAME, EMAIL, DOB FROM STUDENTS WHERE STUD_ID=#{id}
</select>
<update id="updateStudent" parameterType="Student">
UPDATE STUDENTS SET NAME=#{name}, EMAIL=#{email}, DOB=#{dob} WHERE STUD_ID=#{id}
</update>

Instead of giving an alias name for each JavaBeans separately, you can give the package name where MyBatis can scan and register aliases using uncapitalized, nonqualified class names of the Bean.

An example is as follows:

<typeAliases>
<package name="com.mybatis3.domain"/>
</typeAliases>

If there are Student.java and Tutor.java Beans in the com.mybatis3.domain package, com.mybatis3.domain.Student will be registered as student and com.mybatis3.domain.Tutor will be registered as tutor.

An example is as follows:

<typeAliases>
<typeAlias alias="Student" type="com.mybatis3.domain.Student"/>
<typeAlias alias="Tutor" type="com.mybatis3.domain.Tutor"/>
<package name="com.mybatis3.domain"/>
<package name="com.mybatis3.webservices.domain"/>
</typeAliases>

There is another way of aliasing JavaBeans, using the @Alias annotation.

@Alias("StudentAlias")
public class Student {
// ...
}

The @Alias annotation overrides the <typeAliases> configuration.

MyBatis(3.2.3) - Configuring MyBatis using XML, typeAliases的更多相关文章

  1. MyBatis(3.2.3) - Configuring MyBatis using XML, Environment

    The key component of MyBatis is SqlSessionFactory from which we get SqlSession and execute the mappe ...

  2. MyBatis(3.2.3) - Configuring MyBatis using XML, typeHandlers

    As discussed in the previous chapter, MyBatis simplifies the persistent logic implementation by abst ...

  3. MyBatis(3.2.3) - Configuring MyBatis using XML, Settings

    The default MyBatis global settings, which can be overridden to better suit application-specific nee ...

  4. MyBatis(3.2.3) - Configuring MyBatis using XML, Properties

    The properties configuration element can be used to externalize the configuration values into a prop ...

  5. MyBatis(3.2.3) - Configuring MyBatis using XML, Mappers

    Mapper XML files contain the mapped SQL statements that will be executed by the application using st ...

  6. Mybatis增加对象属性不增加mapper.xml的情况

    Mybatis增加对象属性不增加mapper.xml的情况: 只增加Model 对象的属性,在查询语句中返回相同名称的字段,但是在mapper中的 resultMap上面不进行新增字段的增加,查询结果 ...

  7. Mybatis学习总结(三)——SqlMapConfig.xml全局配置文件解析

    经过上两篇博文的总结,对mybatis中的dao开发方法和流程基本掌握了,这一节主要来总结一下mybatis中的全局配置文件SqlMapConfig.xml在开发中的一些常用配置,首先看一下该全局配置 ...

  8. Java Persistence with MyBatis 3(中文版) 第三章 使用XML配置SQL映射器

    关系型数据库和SQL是经受时间考验和验证的数据存储机制.和其他的ORM 框架如Hibernate不同,MyBatis鼓励开发者可以直接使用数据库,而不是将其对开发者隐藏,因为这样可以充分发挥数据库服务 ...

  9. Mybatis手工写sql语句及Mapper.xml方法

    首先在项目中 建一个mapper包,然后在spring集合mybatis的配置文件中设置扫描这个mapper包 然后,建 封装查询结果需要的 pojo 然后,在 mapper包中创建 Mapper接口 ...

随机推荐

  1. js隐藏

    function openLoadingIcon(){ $("#dataLoading").css("display","block"); ...

  2. SpringMVC(四)

    好久没有来谢谢总结性的东西了,一直在赶项目进度,终于忙完了,今天就来说说项目过程中遇到的一些问题: 1.关于在使用@Param的用法,在前面也说过了一点,但是在实际使用中还遇到了一个问题.就是在Map ...

  3. [原]使用node-mapnik和openstreetmap数据初步搭建瓦片服务

    最近依然还是有点小忙,只能挤点时间来学习点,先解决有没有的问题,再解决好不好的问题:) 本文将承接上文<使用node-mapnik生成openstreetmap-carto风格的瓦片>的内 ...

  4. 定义文档兼容性,让IE按指定的版本解析我们的页面

    作为开发人员,特别是作为Web的前端开发人员 ,最悲催的莫过于要不断的,不断的去调试各种浏览器的显示效果,而这其中最让人头痛的莫过于MS下的IE系列浏览器,在IE系列中的调试我们将会发现没有一个是好伺 ...

  5. 一个序列是否可能是push序列的pop序列

    输入:两个序列A.B,A作栈的push序列,判断B是否可能是A的pop序列. #include<iostream> #include<stack> using namespac ...

  6. Thinkphp框架 -- 短信接口验证码

    我用的是一款名叫 短信宝 的应用,新注册的用户可以免费3条测试短信,发现一个BUG,同个手机可以无限注册,自己玩玩还是可以的. 里面的短信接口代码什么信息都没有,感觉看得不是很明白,自己测试了一遍,可 ...

  7. 【转】VIM 快速注释

    我是用自己自定义的,跟你分享一下吧.希望能帮到你. 在.vimrc中加入下面的语句:vmap <C-S-P>    dO#endif<Esc>PO#if 0<Esc> ...

  8. Class.forName的作用以及为什么要用它【转】

    Class.forName(xxx.xx.xx) 返回的是一个类 首先你要明白在java里面任何class都要装载在虚拟机上才能运行.这句话就是装载类用的(和new 不一样,要分清楚). 至于什么时候 ...

  9. iOS开发——动画篇Swift篇&炫酷弹出菜单

    炫酷弹出菜单   这个是一个第三方按钮菜单组件,原版是使用Objective-C编写的名为AwesomeMenu的组件,地址是:https://github.com/levey/AwesomeMenu ...

  10. oracle if then else

    语句一: IF-THEN IF 条件 THEN 运行语句 END IF; 语句二: IF-THEN-ELSE IF 条件 THEN 运行语句 ELSE 运行其它语句 END IF; 语句三: IF-T ...