MyBitis(iBitis)系列随笔之二:类型别名(typeAliases)与表-对象映射(ORM)
类型别名(typeAliases):
作用:通过一个简单的别名来表示一个冗长的类型,这样可以降低复杂度。
类型别名标签typeAliases中可以包含多个typeAlias,如下
- <typeAliases>
- <typeAlias alias="user" type="com.jefry.User"/>
- <typeAlias alias="student" type="com.jefry.student"/>
- ……
- ……
- ……
- </typeAliases>
本着简单的原则,我还是接着上一讲实例代码来作修改
在mybatis-config.xml文件增加一个<typeAliases>
代码如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <typeAliases>
- <typeAlias alias="user" type="com.jefry.User"/>
- </typeAliases>
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC"/>
- <dataSource type="POOLED">
- <property name="driver" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://127.0.0.1:3306/test"/>
- <property name="username" value="root"/>
- <property name="password" value="root"/>
- </dataSource>
- </environment>
- </environments>
- <mappers>
- <mapper resource="com/jefry/UserMapper.xml"/>
- </mappers>
- </configuration>
那么在UserMapper.xml文件中type="com.jefry.User"就可以替换为user
如下:
- <span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="User">
- <!--resultType 表示com.jefry.User这样一个实体对象 -->
- <select id="selectUser" parameterType="int" resultType="user">
- select * from t_user where id = #{id}
- </select>
- </mapper> </span>
表-对象映射
上一讲中实例,我们并没有配置表的字段与实体属性之间的关联,但是我们却得到了我们想要的实例对象。原因是我们的t_user表的各个字段名与User类的成员属性名完全一致,一旦t_user表的各个字段名与User类的成员属性名不同(比如t_user某个字段是name,而User的属性却是username)怎么办呢?
MyBitis借助resultMap实现了表-对象映射如下:
- <span style="color:#000000;"><resultMap id="userResultMap" type="user">
- <id property="id" column="id"/>
- <result property="userName" column="name"/>
- <result property="password" column="pass"/>
- </resultMap></span>
根据字面意思我们很容易将字段与属性映射起来。
需要主要的是 resultType="com.jefry.User">变为resultMap="userResultMap"啦。
- <span style="color:#000000;"> <select id="selectUser" parameterType="int" resultMap="userResultMap" >
- select * from t_user where id = #{id}
- </select></span>
新的UserMapper.xml代码如下:
- <span style="color:#000000;"><?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="user">
- <resultMap id="userResultMap" type="user">
- <id property="id" column="id"/>
- <result property="userName" column="name"/>
- <result property="password" column="pass"/>
- </resultMap>
- <!--resultType 表示com.jefry.User这样一个实体对象 -->
- <select id="selectUser" parameterType="int" resultMap="userResultMap" >
- select * from t_user where id = #{id}
- </select>
- </mapper></span>
MyBitis(iBitis)系列随笔之二:类型别名(typeAliases)与表-对象映射(ORM)的更多相关文章
- MyBitis(iBitis)系列随笔之五:多表(一对多关联查询)
MyBitis(iBitis)系列随笔之一:MyBitis入门实例 MyBitis(iBitis)系列随笔之二:类型别名(typeAliases)与表-对象映射(ORM) MyBitis(iBitis ...
- MyBitis(iBitis)系列随笔之四:多表(多对一查询操作)
前面几篇博客介绍的都是单表映射的一些操作,然而在我们的实际项目中往往是用到多表映射.至于多表映射的关键要用到mybitis的association来加以实现. 这篇介绍的是多表中 ...
- MyBitis(iBitis)系列随笔之三:简单实现CRUD
Mybitis(iBitis)实现对对象增删改查操作要借助<select/>查询,<insert/>增加,<update/>更新,<delete/>删除 ...
- MyBitis(iBitis)系列随笔之六:mybitis与spring集成
目前Spring官方还没有出整合Mybatis的特性,但是mybitis比较给力,开发了一个mybatis-spring插件,达到与Spring的完美整合目的. 在与Spring集成前,一方面我们需要 ...
- MyBitis(iBitis)系列随笔之一:MyBitis入门实例
MyBits前身是iBitis,相对于Hibernate而言,它是半自动化ORM框架.本着分享和学习的目的,笔者将陆续把学习笔记与代码贴出,希望对想学习mybitis的同窗们有所帮助. ...
- ASP.NET Core3.1使用IdentityServer4中间件系列随笔(二):创建API项目,配置IdentityServer保护API资源
配套源码:https://gitee.com/jardeng/IdentitySolution 接上一篇<ASP.NET Core3.1使用IdentityServer4中间件系列随笔(一):搭 ...
- TS学习随笔(二)->类型推论,联合类型
这篇内容指南: -----类型推论 -----联合类型 类型推论 第一篇中我们看了TS的基本使用和基本数据类型的使用,知道了变量在使用的时候都得加一个类型,那我们可不可以不加呢,这个嘛 ...
- ASP.NET Core3.1使用IdentityServer4中间件系列随笔(三):创建使用[ClientCredentials客户端凭证]授权模式的客户端
配套源码:https://gitee.com/jardeng/IdentitySolution 上一篇<ASP.NET Core3.1使用IdentityServer4中间件系列随笔(二):创建 ...
- 浩哥解析MyBatis源码(八)——Type类型模块之TypeAliasRegistry(类型别名注册器)
原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6705769.html 1.回顾 前面几篇讲了数据源模块,这和之前的事务模块都是enviro ...
随机推荐
- Jni中图片传递的3种方式(转)
java层的图片如何传递到c/c+层处理,处理完之后如何传回java层,下面总结了一下用到的三种方法. 1.将Bitmap转为int[]数组对象,将数组作为参数传递到C/C++层,处理完之后再以int ...
- 深入RecyclerView-为什么要使用ItemDecoration
Part 1:不要用view做分割线 首先,什么是ItemDecoration?来看看官网是如何解释的. ItemDecoration允许从adapter的数据集合中为特定的item视图添加特性的绘制 ...
- Yii::记录日志到自定义文件
默认情况下,Yii::log($msg, $level, $category)会把日志记录到runtime/application.log文件中 日志格式如下: [时间] - [级别] - [类别] ...
- weblogic设置jvm参数
http://www.quiee.com.cn/archives/592/ weblogic a) 编辑Weblogic Server启动脚本文件:BEA_HOME\user_projects\dom ...
- js open窗口父子窗口操作
http://zhidao.baidu.com/question/61358246.html?an=0&si=1 js open窗口父子窗口操作 父窗口js代码: function ...
- java基础学习总结——GUI编程(二) 未学习
一.事件监听
- 创建你的第一个Android PHP应用
google的开源移动操作系统Android给智能手机市场带来了风暴.不像Apple,对想要为水果市场(Iphone App Store)提供应用软件的开发者们有着严格的指导原则以及要求,Google ...
- Jquery实现无刷新DropDownList联动
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...
- 求子数组的最大和要求O(n)
//求子数组的最大和 //输入一个整形数组.有整数也有负数,数组中连续一个或多个子数组,每一个子数组都有一个和,求全部子数组的和的最大值,要求时间复杂度O(n) #include<iostrea ...
- django 错误信息
一.No module named 'requests' 安装: pip install django-salmonella 二.No module named 'requests' 安装: pip ...