类型别名(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. Nutch相关框架安装使用最佳指南(转帖)

    Nutch相关框架安装使用最佳指南 Chinese installing and using instruction  -  The best guidance in installing and u ...

  2. 【LeetCode】69. Sqrt(x) (2 solutions)

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 解法一:牛顿迭代法 求n的平方根,即求f(x)= ...

  3. 输出流格式化(以操纵子方式格式化,以ios类成员函数方式格式化)

    一.以操纵子方式格式化 数据输入输出的格式控制使用系统头文件<iomanip>中提供的操纵符.把它们作为插入操作符<<的输出对象即可.如setiosflags.setw.set ...

  4. C++多态有哪几种方式?

    C++多态方式: (1)静态多态(重载,模板) 是在编译的时候,就确定调用函数的类型. (2)动态多态(覆盖,虚函数实现) 在运行的时候,才确定调用的是哪个函数,动态绑定.运行基类指针指向派生类的对象 ...

  5. AutoFac文档5(转载)

    目录 开始 Registering components 控制范围和生命周期 用模块结构化Autofac xml配置 与.net集成 深入理解Autofac 指导 关于 词汇表 扫描 autofac可 ...

  6. mysql-5.7.9 shutdown 语法详解

    mysql-5.7.9 终于提供shutdown 语法啦: 之前如果想关闭一个mysql数据库可以通过kill 命令.mysqladmin shutdown .service mysqld stop ...

  7. unity, ugui button 禁止重复点击

    如上图,button名称为btn_sim,当点击button后,开始播放zoomToTarget动画.为了防止在动画播放过程中再次点击button导致动画被打断,希望当首次点击button后butto ...

  8. ubuntu apt-get方式安装与卸载

    在ubuntu终端中安装软件: 安装软件 apt-get install softname1 softname2 softname3……卸载软件 apt-get remove softname1 so ...

  9. Linux下磁盘管理命令df与du

    Linux下磁盘管理命令df与du  对磁盘进行查看和控制的两个linux命令,df和du.  一.du命令 首先看一下du的help说明: [root@misdwh opt]# du --help ...

  10. [转]C艹中的各种const总结

    Ps: 难免碰到C家族的代码 ,各种const直接搞晕,搜集各种资料备用.... ----------------------------------------------------------- ...