类型别名(typeAliases):
     
作用:通过一个简单的别名来表示一个冗长的类型,这样可以降低复杂度。
    类型别名标签typeAliases中可以包含多个typeAlias,如下

  1. <typeAliases>
  2. <typeAlias alias="user" type="com.jefry.User"/>
  3. <typeAlias alias="student" type="com.jefry.student"/>
  4. ……
  5. ……
  6. ……
  7. </typeAliases>

本着简单的原则,我还是接着上一讲实例代码来作修改
 在mybatis-config.xml文件增加一个<typeAliases>
    代码如下:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <typeAliases>
  7. <typeAlias alias="user" type="com.jefry.User"/>
  8. </typeAliases>
  9. <environments default="development">
  10. <environment id="development">
  11. <transactionManager type="JDBC"/>
  12. <dataSource type="POOLED">
  13. <property name="driver" value="com.mysql.jdbc.Driver"/>
  14. <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
  15. <property name="username" value="root"/>
  16. <property name="password" value="root"/>
  17. </dataSource>
  18. </environment>
  19. </environments>
  20. <mappers>
  21. <mapper resource="com/jefry/UserMapper.xml"/>
  22. </mappers>
  23. </configuration>

那么在UserMapper.xml文件中type="com.jefry.User"就可以替换为user
如下:

  1. <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="User">
  6. <!--resultType 表示com.jefry.User这样一个实体对象 -->
  7. <select id="selectUser" parameterType="int" resultType="user">
  8. select * from t_user where id = #{id}
  9. </select>
  10. </mapper>  </span>

表-对象映射

上一讲中实例,我们并没有配置表的字段与实体属性之间的关联,但是我们却得到了我们想要的实例对象。原因是我们的t_user表的各个字段名与User类的成员属性名完全一致,一旦t_user表的各个字段名与User类的成员属性名不同(比如t_user某个字段是name,而User的属性却是username)怎么办呢?
       MyBitis借助resultMap实现了表-对象映射如下:

  1. <span style="color:#000000;"><resultMap id="userResultMap" type="user">
  2. <id property="id" column="id"/>
  3. <result property="userName" column="name"/>
  4. <result property="password" column="pass"/>
  5. </resultMap></span>

根据字面意思我们很容易将字段与属性映射起来。
 需要主要的是 resultType="com.jefry.User">变为resultMap="userResultMap"啦。

  1. <span style="color:#000000;">    <select id="selectUser" parameterType="int"  resultMap="userResultMap" >
  2. select * from t_user where id = #{id}
  3. </select></span>

新的UserMapper.xml代码如下:

  1. <span style="color:#000000;"><?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="user">
  6. <resultMap id="userResultMap" type="user">
  7. <id property="id" column="id"/>
  8. <result property="userName" column="name"/>
  9. <result property="password" column="pass"/>
  10. </resultMap>
  11. <!--resultType 表示com.jefry.User这样一个实体对象 -->
  12. <select id="selectUser" parameterType="int"  resultMap="userResultMap" >
  13. select * from t_user where id = #{id}
  14. </select>
  15. </mapper></span>

MyBitis(iBitis)系列随笔之二:类型别名(typeAliases)与表-对象映射(ORM)的更多相关文章

  1. MyBitis(iBitis)系列随笔之五:多表(一对多关联查询)

    MyBitis(iBitis)系列随笔之一:MyBitis入门实例 MyBitis(iBitis)系列随笔之二:类型别名(typeAliases)与表-对象映射(ORM) MyBitis(iBitis ...

  2. MyBitis(iBitis)系列随笔之四:多表(多对一查询操作)

      前面几篇博客介绍的都是单表映射的一些操作,然而在我们的实际项目中往往是用到多表映射.至于多表映射的关键要用到mybitis的association来加以实现.          这篇介绍的是多表中 ...

  3. MyBitis(iBitis)系列随笔之三:简单实现CRUD

    Mybitis(iBitis)实现对对象增删改查操作要借助<select/>查询,<insert/>增加,<update/>更新,<delete/>删除 ...

  4. MyBitis(iBitis)系列随笔之六:mybitis与spring集成

    目前Spring官方还没有出整合Mybatis的特性,但是mybitis比较给力,开发了一个mybatis-spring插件,达到与Spring的完美整合目的. 在与Spring集成前,一方面我们需要 ...

  5. MyBitis(iBitis)系列随笔之一:MyBitis入门实例

        MyBits前身是iBitis,相对于Hibernate而言,它是半自动化ORM框架.本着分享和学习的目的,笔者将陆续把学习笔记与代码贴出,希望对想学习mybitis的同窗们有所帮助.     ...

  6. ASP.NET Core3.1使用IdentityServer4中间件系列随笔(二):创建API项目,配置IdentityServer保护API资源

    配套源码:https://gitee.com/jardeng/IdentitySolution 接上一篇<ASP.NET Core3.1使用IdentityServer4中间件系列随笔(一):搭 ...

  7. TS学习随笔(二)->类型推论,联合类型

    这篇内容指南:        -----类型推论  -----联合类型 类型推论 第一篇中我们看了TS的基本使用和基本数据类型的使用,知道了变量在使用的时候都得加一个类型,那我们可不可以不加呢,这个嘛 ...

  8. ASP.NET Core3.1使用IdentityServer4中间件系列随笔(三):创建使用[ClientCredentials客户端凭证]授权模式的客户端

    配套源码:https://gitee.com/jardeng/IdentitySolution 上一篇<ASP.NET Core3.1使用IdentityServer4中间件系列随笔(二):创建 ...

  9. 浩哥解析MyBatis源码(八)——Type类型模块之TypeAliasRegistry(类型别名注册器)

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6705769.html 1.回顾 前面几篇讲了数据源模块,这和之前的事务模块都是enviro ...

随机推荐

  1. sql各种连接详解

      迁移时间:2017年6月1日16:33:58 CreateTime--2016年9月14日11:19:00Author:Marydon sql各种连接详解 参考链接: http://www.jb5 ...

  2. mariadb 10.1.10安装

    Maridb 10.1.10 on centos 6.6 *********************************************************************** ...

  3. java反射--通过反射了解集合泛型的本质

    通过Class,Method来认识泛型的本质 package com.reflect; import java.lang.reflect.Method; import java.util.ArrayL ...

  4. javax.naming.NoInitialContextException: Need to specify class name in environment or system property

    javax.naming.NoInitialContextException: Need to specify class name in environment or system property ...

  5. C-常用构造哈希函数

    1.定址法(比如0-100岁的人数统计, 可以按年龄作为散列地址, 1980年后每年出生人数的统计, 可以把"年限 - 1980"作为散列地址) 2.取余法 3.数字分析法(比如一 ...

  6. JSP页面输出的几种方式:

    1. 内置九大对象之out         下载图片 2. <%= %> JSP输出表达式      JSP中出现大量脚本 3. response.getWriter()        n ...

  7. perl: warning: Setting locale failed.

    本篇文章由:http://xinpure.com/perl-warning-setting-locale-failed/ 将 mac 系统切换成英文后,使用 git 命令出现如下错误: perl: w ...

  8. TextWatcher

    对于一些需求,如非法字符限制(例如不允许输入#号,如果输入了#给出错误提示),做成动态判断更方便一些,而且容易扩展: 在Android里使用TextWatcher接口可以很方便的对EditText进行 ...

  9. 51单片机晶振11.0592M延时函数

    /********************************************** º¯ÊýÃû£ºdelay_ms(uint z) ÑÓʱº¯Êý(12MHZ¾§Õñ) ´Ëº¯ÊýÑ ...

  10. ExecuteScalar,ExecuteReader,ExecuteNonQuery区别

    ExecuteScalar is typically used when your query returns a single value. If it returns more, then the ...