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. POJ 1860 Currency Exchange (SPFA松弛)

    题目链接:http://poj.org/problem?id=1860 题意是给你n种货币,下面m种交换的方式,拥有第s种货币V元.问你最后经过任意转换可不可能有升值.下面给你货币u和货币v,r1是u ...

  2. Unity3D之Mecanim动画系统学习笔记(八):Animator Layers(动画分层)

    解决什么问题? 动画分层可以用来解决什么样的问题呢?试想一下如果你要开发一款第三人称的射击游戏,那么肯定是希望身体的动画分为上下两部分,上方根据瞄准的位置和是否射击进行动画播放,下方根据移动播放动画. ...

  3. Nginx NLB 及Redis学习

    负载均衡: ARR: 微软的应用级别的负载均衡方案 NLB:服务器级别的负载均衡方案 Nginx:反向代理 达到负载均衡. Redis:用作缓存(Redis 主从配置和参数详解 http://www. ...

  4. PHP AJAXFORM提交图片上传并显示图片源代码

    PHP dofile.php 文件上传源代码 <? php $file_upload = "upload/"; $file_allow_ext='gif|jpg|jpeg|p ...

  5. 恢复WinMend Folder Hidden隐藏的文件

    前几天用了个WinMend Folder Hidden,顺手隐藏了一个文件,感觉很方便的样子. 今天突然要用的时候发现这个文件不见了,于是我去Everything搜索,其实我是忘了之前已经把它隐藏了, ...

  6. [置顶] How to dump redo log entry?

    1.转储针对特定数据块(4号文件的第10-20号数据块)修改的 redo entry select file#,name,blocks from v$datafile;      FILE# NAME ...

  7. delphi 18 屏蔽和替换 右键菜单

    //屏蔽右键菜单procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;  var Handled: Boolean);begin  wi ...

  8. centos6.x已经安装的系统添加图形界面

    yum groupinstall "X Window System" yum groupinstall GNOME Desktop Environment

  9. [ES6] 16. Object Enhancements

    Define object: var color = "blue"; var speed = 120; var car = {color, speed}; console.log( ...

  10. UNIX基础知识之时间值

    本篇博文内容摘自<UNIX环境高级编程>(第二版),仅作个人学习记录所用.关于本书可参考:http://www.apuebook.com/. 长期以来,UNIX系统一直使用两种不同的时间值 ...