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

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. Eclipse超级有用的快捷键

    1.Alt + Shift + R 重构 2.Ctrl + F11 运行并调试程序 3.Ctrl + Shift + O 自动导入包 4.Ctrl + Shift + F 格式化代码 5.F5 调试模 ...

  2. 网页游戏开发秘笈 PDF扫描版

    精选10种常见的游戏类型,透过典型实例,深入剖析游戏引擎及工具的选用技巧,详细讲解每款游戏的制作过程,为快速掌握网页游戏开发提供系统而实用的指南. 网页游戏开发秘笈 目录: 译者序  前 言  导 言 ...

  3. 安装mysql8.0.11及修改root密码、连接navicat for mysql的思路详解

    1.1. 下载: 官网下载zip包,我下载的是64位的: 下载地址:https://dev.mysql.com/downloads/mysql/ 下载zip的包: 下载后解压:(解压在哪个盘都可以的) ...

  4. CentOS6.5内核升级FATAL: Module scsi_wait_scan not found

    系统为CentOS6.5的虚拟机内核升级至版本4.6.0-1,重启后,报以下错误: Module scsi_wait_scan not found. 无法进入系统. 问题描述详见:Known Issu ...

  5. bzoj 2827: 千山鸟飞绝

    2827: 千山鸟飞绝 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 802  Solved: 228[Submit][Status][Discuss ...

  6. Redis数据类型,持久化,回收策略——(Redis缓存第一章)

    缓存:第一种是内存缓存 比如Map(简单的数据结构),以及EH Cache(Java第三方库),第二种是缓存组件比如Memached,Redis:Redis(remote dictionary ser ...

  7. thinkphp实现登录后返回原界面

    主要思路还是用session记录原地址,在登录后再跳转回原界面 先保存请求login方法界面的url public function savelogin(){ session('returnUrl', ...

  8. day23作业详解

    1.题目 2.题目详解 点击查看详细内容 1. 1-1 封装 把功能封装到类中 class Message(object): def email(self):pass def msg(self):pa ...

  9. 如何简单的理解TDD与DDT

    TDD:TEST-DRIVEN Development 测试驱动开发究竟是什么意思?如何理解测试驱动开发? 举个红绿条简单的例子: 1.编写测试代码 2.编译运行测试代码,肯定会失败,因为实现代码还没 ...

  10. chapter09

    import java.io.File import java.nio.file._ import scala.collection.mutable.ArrayBuffer/** * Created ...