<resultMap type="Student" id="StudentResult">

<id property="id" column="id"/>

<result property="name" column="name"/>

<result property="age" column="age"/>

</resultMap>

注:resultMap中的property对应的是model中的属性,column对应的是数据库表中的列属性,整个代表一个结果的映射集合

一.基本的SQL语句

1.insert语句

<insert id="add" parameterType="Student"  >

insert into t_student values(null,#{name},#{age})

</insert>

2.update语句

<update id="update" parameterType="Student">

update t_student set name=#{name},age=#{age} where id=#{id}

</update>

3.delete语句

<delete id="delete" parameterType="Integer">

delete from t_student where id=#{id}

</delete>

4.select语句

<select id="findById" parameterType="Integer" resultType="Student">

select * from t_student where id=#{id}

</select>

<select id="find" resultMap="StudentResult">

select * from t_student

</select>

接口中的方法如下所示:

public interface StudentMapper {

public int add(Student student);

public int update(Student student);

public int delete(Integer id);

public Student findById(Integer id);

public List<Student> find();

}

注:以上语句中的id表示表示mapper接口中的方法,须与接口中的方法名一致。

select语句中的resultType表示结果的返回类型,也可以用resultMap来表示,下面说一说这两者的区别:

MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultMap的引用,但是resultType跟resultMap不能同时存在。

在MyBatis进行查询映射时,其实查询出来的每一个属性都是放在一个对应的Map里面的,其中键是属性名,值则是其对应的值。

①当提供的返回类型属性是resultType时,MyBatis会将Map里面的键值对取出赋给resultType所指定的对象对应的属性。所以其实MyBatis的每一个查询映射的返回类型都是ResultMap,只是当提供的返回类型属性是resultType的时候,MyBatis对自动的给把对应的值赋给resultType所指定对象的属性。

②当提供的返回类型是resultMap时,因为Map不能很好表示领域模型,就需要自己再进一步的把它转化为对应的对象,这常常在复杂查询中很有作用。

二.Mybatis关系映射

1.一对一关系实现

为了方便讲述,在t_student表的基础上增加了t_address表,来作一对一的关系映射。

表结构如下:

CREATE TABLE `t_address` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`sheng` varchar(20) DEFAULT NULL,

`shi` varchar(20) DEFAULT NULL,

`qu` varchar(20) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

通常用association属性来表示一对一的关系实现

在resultMap中起嵌入asociation属性,表示将student表与address表关联起来

一般写作如下形式:

<resultMap type="Student" id="StudentResult">

<id property="id" column="id"/>

<result property="name" column="name"/>

<result property="age" column="age"/>

<association property="address" javaType="Address">

<result property="id" column="id"/>

<result property="sheng" column="sheng"/>

<result property="shi" column="shi"/>

<result property="qu" column="qu"/>

</association>

</resultMap>

这种方式就不需要addressMapper.xml文件了

但是当关联的表较多时就显得复杂了,因此一般写作如下形式

<resultMap type="Student" id="StudentResult">

<id property="id" column="id"/>

<result property="name" column="name"/>

<result property="age" column="age"/>

<association property="address" column="id" select="com.java1234.mappers.AddressMapper.findById">                         </association>

</resultMap>

通过这样的方式需要定义addressMapper接口如下:

public interface AddressMapper {

public Address findById(Integer id);

}

XML文件如下

<resultMap type="Address" id="AddressResult">

<result property="id" column="id"/>

<result property="sheng" column="sheng"/>

<result property="shi" column="shi"/>

<result property="qu" column="qu"/>

</resultMap>

<select id="findById" parameterType="Integer" resultType="Address">

select * from t_address where id=#{id}

</select>

这样就可以<association property="address" column="id" select="com.java1234.mappers.AddressMapper.findById">                         </association>将address的属性表示出来了。

一个简单的查询例子如下:

<select id="findStudentWithAddress" resultMap="StudentResult" parameterType="Integer">

select * from t_student t1,t_address t2 where t1.addressId=t2.id and t1.id=#{id}

</select>

2.一对多关系实现

为了方便讲述,在t_student表的基础上增加了t_grade表,来作一对多的关系映射,即一个grade里有多个student。

表结构如下:

CREATE TABLE `t_grade` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`gradeName` varchar(20) DEFAULT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

insert  into `t_grade`(`id`,`gradeName`) values (1,'大学一年级'),(2,'大学二年级');

通常用collection属性来表示一对多的关系实现

gradeMapper.xml文件的resultMap中起嵌入collection属性,表示将student表与grade表关联起来

如下所示:

<mapper namespace="com.java1234.mappers.GradeMapper">

<resultMap type="Grade" id="GradeResult">

<result property="id" column="id"/>

<result property="gradeName" column="gradeName"/>

<collection property="students" column="id" select="com.java1234.mappers.StudentMapper.findByGradeId"></collection>

</resultMap>

<select id="findById" parameterType="Integer" resultMap="GradeResult">

select * from t_grade where id=#{id}

</select>

</mapper>

StudentMppaer.xml文件如下:

<resultMap type="Student" id="StudentResult">

<id property="id" column="id"/>

<result property="name" column="name"/>

<result property="age" column="age"/>

<association property="address" column="id" select="com.java1234.mappers.AddressMapper.findById">   </association>

<association property="grade" column="gradeId" select="com.java1234.mappers.GradeMapper.findById"></association>

</resultMap>

一个简单的查询例子如下:

<select id="findByGradeId" resultMap="StudentResult" parameterType="Integer">

select * from t_student where gradeId=#{gradeId}

</select>

MyBatis学习笔记3--使用XML配置SQL映射器的更多相关文章

  1. Java Persistence with MyBatis 3(中文版) 第三章 使用XML配置SQL映射器

    关系型数据库和SQL是经受时间考验和验证的数据存储机制.和其他的ORM 框架如Hibernate不同,MyBatis鼓励开发者可以直接使用数据库,而不是将其对开发者隐藏,因为这样可以充分发挥数据库服务 ...

  2. Mybatis基于XML配置SQL映射器(二)

    Mybatis之XML注解 之前已经讲到通过 mybatis-generator 生成mapper映射接口和相关的映射配置文件: 下面我们将详细的讲解具体内容 首先我们新建映射接口文档  sysUse ...

  3. Mybatis基于XML配置SQL映射器(一)

    Durid和Mybatis开发环境搭建 SpringBoot搭建基于Spring+SpringMvc+Mybatis的REST服务(http://www.cnblogs.com/nbfujx/p/76 ...

  4. Mybatis基于XML配置SQL映射器(三)

    Mybatis之动态SQL mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: if choo ...

  5. (三)使用XML配置SQL映射器

    SqlSessionFactoryUtil.java package com.javaxk.util; import java.io.IOException; import java.io.Input ...

  6. MyBatis 3 使用注解配置SQL映射器

    l 在映射器Mapper接口上使用注解 l 映射语句 @Insert,@Update,@Delete,@SeelctStatements l 结果映射 一对一映射 一对多映射 l 动态SQL @Sel ...

  7. MyBatis 3(中文版) 第四章 使用注解配置SQL映射器

    本章将涵盖以下话题: l 在映射器Mapper接口上使用注解 l 映射语句 @Insert,@Update,@Delete,@SeelctStatements l 结果映射 一对一映射 一对多映射 l ...

  8. 使用注解配置SQL映射器

    在上一章,我们看到了我们是怎样在映射器Mapper XML配置文件中配置映射语句的.MyBatis也支持使用注解来配置映射语句.当我们使用基于注解的映射器接口时,我们不再需要在XML配置文件中配置了. ...

  9. Mybatis基于接口注解配置SQL映射器(一)

    上文已经讲解了基于XML配置的SQL映射器,在XML配置的基础上MyBatis提供了简单的Java注解,使得我们可以不配置XML格式的Mapper文件,也能方便的编写简单的数据库操作代码. Mybat ...

随机推荐

  1. [转]bus error与segment error

    在c程序中,经常会遇到段错误(segment error)和总线错误(bus error),这两种问题出现的原因可能如下 段错误: 对一个NULL指针解引用. 访问程序进程以外的内存空间. 实际上,第 ...

  2. ubuntu14.04下 Kinect V2+Ros接口安装

    1. 首先git下载代码,放到主文件夹下面 git clone https://github.com/OpenKinect/libfreenect2.git 2. 然后安装依赖项如下,最好事先编译安装 ...

  3. Eclipse中项目不会自动编译问题的坑和注意点

    最近接受了几个又小有老的项目,用eclipse反而比idea方便,但是好长时间不用eclipse了,还有有些问题的! 主要是碰到了classnotfound这个难缠的问题:这里记录一下几个坑,避免以后 ...

  4. Potential Pythonic Pitfalls

    Potential Pythonic Pitfalls Monday, 11 May 2015 Table of Contents Not Knowing the Python Version Obs ...

  5. Apriori 算法python实现

    1. Apriori算法简介 Apriori算法是挖掘布尔关联规则频繁项集的算法.Apriori算法利用频繁项集性质的先验知识,通过逐层搜索的迭代方法,即将K-项集用于探察(k+1)项集,来穷尽数据集 ...

  6. GCC编译过程与动态链接库和静态链接库

    1. 库的介绍 库是写好的现有的,成熟的,可以复用的代码.现实中每个程序都要依赖很多基础的底层库,不可能每个人的代码都从零开始,因此库的存在意义非同寻常. 本质上来说库是一种可执行代码的二进制形式,可 ...

  7. Ajax jsonp 跨域请求实例

    跨域请求 JSONP的缺点则是:它只支持GET请求而不支持POST等其它类型的HTTP请求:它只支持跨域HTTP请求这种情况,不能解决不同域的两个页面之间如何进行JavaScript调用的问题. $. ...

  8. Oracle实体化视图

    1.减轻网络负担:通过MV将数据从一个数据库分发到多个不同的数据库上,通过对多个数据库访问来减轻对单个数据库的网络负担.  2.搭建分发环境:通过从一个中央数据库将数据分发到多个节点数据库,达到分发数 ...

  9. keepalived实现haproxy负载均衡器的高可用

    一.keepalived简介 keepalived是集群管理中保证集群高可用的一个服务软件,其功能类似于,用来防止单点故障. 二.vrrp协议2.1 vrrp协议简介 在现实的网络环境中,两台需要通信 ...

  10. 支付宝&微信统一支付

    1.实体对应关系: Application  — 支付记录实体 --  支付记录详情 2.流程 1.生成订单选择支付类型 2.支付宝:PC端.手机端.扫码:微信:微信公众号支付.扫码支付.H5支付. ...