MyBatis(3.2.3) - Configuring MyBatis using XML, typeAliases
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的更多相关文章
- 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 ...
- MyBatis(3.2.3) - Configuring MyBatis using XML, typeHandlers
As discussed in the previous chapter, MyBatis simplifies the persistent logic implementation by abst ...
- MyBatis(3.2.3) - Configuring MyBatis using XML, Settings
The default MyBatis global settings, which can be overridden to better suit application-specific nee ...
- MyBatis(3.2.3) - Configuring MyBatis using XML, Properties
The properties configuration element can be used to externalize the configuration values into a prop ...
- 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 ...
- Mybatis增加对象属性不增加mapper.xml的情况
Mybatis增加对象属性不增加mapper.xml的情况: 只增加Model 对象的属性,在查询语句中返回相同名称的字段,但是在mapper中的 resultMap上面不进行新增字段的增加,查询结果 ...
- Mybatis学习总结(三)——SqlMapConfig.xml全局配置文件解析
经过上两篇博文的总结,对mybatis中的dao开发方法和流程基本掌握了,这一节主要来总结一下mybatis中的全局配置文件SqlMapConfig.xml在开发中的一些常用配置,首先看一下该全局配置 ...
- Java Persistence with MyBatis 3(中文版) 第三章 使用XML配置SQL映射器
关系型数据库和SQL是经受时间考验和验证的数据存储机制.和其他的ORM 框架如Hibernate不同,MyBatis鼓励开发者可以直接使用数据库,而不是将其对开发者隐藏,因为这样可以充分发挥数据库服务 ...
- Mybatis手工写sql语句及Mapper.xml方法
首先在项目中 建一个mapper包,然后在spring集合mybatis的配置文件中设置扫描这个mapper包 然后,建 封装查询结果需要的 pojo 然后,在 mapper包中创建 Mapper接口 ...
随机推荐
- IPhone 设备状态、闪光灯状态
//判断闪光灯状态,修改默认的"CameraFlashOff" 按钮图片.转由 TGCameraFlash.m 控制图标切换 AVCaptureDevice *device ...
- hdu4614Vases and Flowers(线段树,段设置,更新时范围的右边值为变量)
Problem Description Alice is so popular that she can receive many flowers everyday. She has N vases ...
- ASP.NET MVC- 使用PageList.Mvc分页
ASP.NET MVC中进行分页的方式有多种,在NuGet上有提供使用PagedList.PagedList.Mvc进行分页. 1. 通过NuGet引用PagedList.Mvc 在安装引用Paged ...
- MVC神韵---你想在哪解脱!(十六)
MVC验证属性自动验证原理 也许有人会问,既然我们没有在C与V追加任何显示错误信息提示的代码,那么控制器或视图内部是如何生成这些显示错误信息提示的画面的.让我们揭开这么谜底吧!当在Movie类中追加了 ...
- 《C语言编写 学生成绩管理系统》
/* (程序头部凝视開始) * 程序的版权和版本号声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名: 学生成绩管理 ...
- Python 类型的分类
1.存储模型,对象可以保存多少个值.如果只能保存一个值,是原子类型.如果可以保存多个值,是容器类型.数值是原子类型,元组,列表,字典是容器类型.考虑字符串,按道理,字符串应该是容器类型,因为它包含多个 ...
- Java模拟登录系统抓取内容【转载】
没有看考勤的习惯,导致我的一天班白上了,都是钱啊,系统也不发个邮件通知下.... 为了避免以后还有类似状况特别写了个java模拟登录抓取考勤内容的方法(部分代码来自网络),希望有人修改后也可以 ...
- 2015南阳CCPC F - The Battle of Guandu 多源多汇最短路
The Battle of Guandu Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description In the year of 200, t ...
- mmc生产运输问题
本题目结合生产和运输,增加了约束, 其实,比较生产,运输问题,大同小异, 解法基本相同.
- 怎样配置Tomcat环境变量
JDK环境变量配置: 假设你的安装JDK在C盘里,如:C:\Program Files\Java\jdk1.6.0_05,那么就在系统变量里(当然也能够在用户变量里)点新建: 变量 ...