在实现实列中我们在学生表里面增加了一个地址表用于与学生表的一对一

1.创建地址实体类:

package com.java1234.mappers;

import com.java1234.model.Address;

public interface AddressMapper {

public Address findById(Integer id);

}

2.创建地址实体类关联的映射文件

<?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="com.java1234.mappers.AddressMapper">

<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>

</mapper>

3.在学生表的映射文件里面关联地址

<?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="com.java1234.mappers.StudentMapper">

<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>

<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>

<insert id="add" parameterType="Student" >
insert into t_student values(null,#{name},#{age})
</insert>

<update id="update" parameterType="Student">
update t_student set name=#{name},age=#{age} where id=#{id}
</update>

<delete id="delete" parameterType="Integer">
delete from t_student where id=#{id}
</delete>

<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>
</mapper>

mybatis-关联关系的更多相关文章

  1. MyBatis关联关系

    1.一对多:一个国家对应多个城市 01.实体类 package cn.pb.bean;import java.util.Set;/** * 国家的实体类 */public class Country ...

  2. mybatis关联关系映射

    1.一对多关联关系 2.多对多关联关系 首先先用逆向生成工具生成t_hibernate_order.t_hibernate_order_item t_hibernate_book.t_hibernat ...

  3. mybatis 关联关系查询 java

    <mapper namespace="com.rrz.modules.awardrecord.dao.CheckAwardsDao"> <resultMap ty ...

  4. mybatis之关联关系映射

    Mybatis关联关系映射 1.一对多 首先先用逆向生成工具生成t_hibernate_order.t_hibernate_order_item 这两张表对应的model与mapper OrderVo ...

  5. JavaEE MyBatis

    1.  简介 MyBatis本是apache的一个开源项目iBatis的升级版,2013年11月迁移到Github,是三层架构中持久层框架. 目前提供了Java..NET.以及Ruby三种语言实现的版 ...

  6. MyBatis学习笔记(二) 关联关系

    首先给大家推荐几个网页: http://blog.csdn.net/isea533/article/category/2092001 没事看看 - MyBatis工具:www.mybatis.tk h ...

  7. myBatis 基础测试 表关联关系配置 集合 测试

    myBatis 基础测试 表关联关系配置 集合 测试 测试myelipse项目源码 sql 下载 http://download.csdn.net/detail/liangrui1988/599388 ...

  8. mybatis学习笔记三(关联关系)

    学习mybatis的关联关系,主要注解在代码上,这里不做解释.配置文件一样的就不贴了 1.关联关系表创建(学生对应老师 多对一) 学生老师表 2.表对应的实体类 package com.home.en ...

  9. MyBatis对象关联关系---- association与collection

    Mybatis处理“一对多”的关系时,需要用到associasion元素.处理”多对一“用collection元素来实现(这两个元素在之前mapper文件中提到过). 本例子中,假设一名User可以有 ...

  10. Mybatis输入输出映射_动态sql_关联关系(一对一、一对多、多对多)

    Mybatis输入输出映射_动态sql_关联关系(一对一.一对多.多对多)输入输出映射parameterType完成输入映射parameterType可以传入的参数有,基本数据类型(根据id查询用户的 ...

随机推荐

  1. Codeforces Round #527 (Div. 3)C(多重集,STRING)

    #include<bits/stdc++.h>using namespace std;const int maxn=1e6+7;pair<string,int>p[maxn]; ...

  2. (转)System.Web.Mvc.UrlHelper的学习与使用

    转载自http://www.cnblogs.com/longgel/archive/2010/02/06/1664884.html 上一次学习了HtmlHelper帮助类,这次我们学习一下UrlHel ...

  3. 让你的Kivy支持中文字符

    前言 默认Kivy字体不支持中文,本文简单描述如何让你的应用支持中文字符 代码 from kivy.app import App from kivy.core.text import LabelBas ...

  4. web综合案例03

    web综合案例03 web综合案例03 web综合案例03 web综合案例03 ... 待复习

  5. Oracle 11g 数据类型

    1.     字符类型 数据类型 长度 说明 CHAR(n BYTE/CHAR) 默认1字节,n值最大为2000 末尾填充空格以达到指定长度,超过最大长度报错.默认指定长度为字节数,字符长度可以从1字 ...

  6. Jmeter的BeanShell中报错:调用bsh方法时出错Error invoking bsh method: eval

    报错内容:ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval In file: inline evalu ...

  7. ip route,ip rule, iptables和docker的端口映射

    iptables 默认5个表, 不可增加其他表 raw 用于配置数据包,raw 中的数据包不会被系统跟踪. filter 是用于存放所有与防火墙相关操作的默认表. nat 用于 网络地址转换(例如:端 ...

  8. js通过行列获取表格中input的值

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...

  9. 关于String的split方法

    在做剑指offer的时候,有一道替换空格的题,立刻就想到用这个split方法来做,但发现,这个方法会丢掉字符串最后的空格??? 百度后,知道原因,这里直接复制粘贴了: 在使用java中的split按照 ...

  10. Unity UGUI按钮添加点击事件

    1. 可视化创建及事件绑定 # 1 : 通过 Hierarchy 面板创建 UI > Button. 2 : 创建一个脚本 TestClick.cs, 定义了一个 Click 的 public ...