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接口 ...
随机推荐
- [置顶] a+=1/a=+1/a-=1区别-c语言
1.解释 a+=1/a=+1/a-=1 含义 a+=1 实质等于 a += 1,也就是等于 a = a + 1: a=+1 实质等于 a = +1:[因为运算符中没有=+,很多人误以为是 a =+ 1 ...
- javabean总结
一. javabean 是什么? Bean的中文含义是“豆子”,顾名思义,JavaBean是指一段特殊的Java类, 就是有默然构造方法,只有get,set的方法的java类的对象. 专业点解释是: ...
- Measuring Signal Similarities
http://cn.mathworks.com/help/signal/examples/measuring-signal-similarities.html Open This Example ...
- SSH三大框架整合使用的配置文件 注解实现
1 Struts.xml 使用拦截器 <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE str ...
- APK的目录结构
APK 包含以下内容: 被编译的代码文件(.dex文件) 文件资源(resources) assets. 证书(certificates) 清单文件(maifestfile) assets 文件 li ...
- nginx将http重定向到https
1.rewrite server { listen 80; server_name test.com; rewrite ^(.*)$ https://$host$1 permanent; } 2. n ...
- 标准C++ 字符串处理增强函数
转自:http://dewei.iteye.com/blog/1566734 //标准C++ string 去除首尾空白字符 2012-8-12 By Dewei static inline void ...
- 什么是比特币(bitcoin)
一.什么是比特币? 比特币是一种由开源的P2P软件产生的电子货币,是一种网络虚拟货币.比特币使用遍布整个P2P网络节点的分布式数据库来记录货币的交易,并使用密码学的设计来确保货币流通各个环节安全性.比 ...
- Codeforces Round #329 (Div. 2) A. 2Char 暴力
A. 2Char Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/593/problem/A De ...
- mmc生产运输问题
本题目结合生产和运输,增加了约束, 其实,比较生产,运输问题,大同小异, 解法基本相同.