一对一:

在数据库里面有这样的一个主外键关系的表:

我需要查找身份证的号码就要知道这个人的姓名(通过一个SQL语句要查到两个实体类里面的信息):

SELECT c.*,p.* FROM idcard c LEFT JOIN person p  ON c.pid=p.pid

 建立两个实体类:

建立映射器的接口:

1 public interface IDCardMapper {
2
3 //根据身份证号来查询身份信息
4 public IDCardEntity queryCard(String cno);
5 }

映射器xml里面的配置:

 1 <!--mybatis的mapper的根节点-->
2 <mapper namespace="com.lv.study.mapper.IDCardMapper">
3
4 <resultMap id="cardMap" type="com.lv.study.entity.IDCardEntity">
5 <id property="cid" column="cid"></id>
6 <result property="cno" column="cno"></result>
7
8 <!--association是一个实体类里面还有一个实体类
9 javaType指定我们pe属性的实体类是什么
10 -->
11 <association property="pe" javaType="com.lv.study.entity.PersonEntity">
12 <id property="pid" column="pid"></id>
13 <result property="pname" column="pname"></result>
14
15 </association>
16 </resultMap>
17
18 <select id="queryCard" resultMap="cardMap">
19
20 SELECT c.*,p.* FROM idcard c LEFT JOIN person p ON c.pid=p.pid where c.cno=#{cno}
21
22 </select>
23
24 </mapper>

测试类:

 1 public class TsetOneToOne {
2 public static void main(String[] args) {
3
4
5 try {
6 String resource="mybatis-config.xml";//这个代表我们编译的根目录
7 InputStream is = null;//把配置文件弄成流的形式
8 is = Resources.getResourceAsStream(resource);
9 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
10 SqlSession sqlSession=sqlSessionFactory.openSession(true);
11 IDCardMapper mapper=sqlSession.getMapper(IDCardMapper.class);
12
13 IDCardEntity id = mapper.queryCard("150");
14 System.out.println(id);
15
16 } catch (IOException e) {
17 e.printStackTrace();
18 }
19
20
21
22 }
23
24 }

结果:

一对多:当我们要查询一颗数的时候我们想知道他有哪些叶子

数据表:

树和叶子的实体类:

 1 public class TreeEntity {
2
3 private int tid;
4 private String tname;
5
6 //在这一棵树里面有多片叶子
7 private List<LeafEntity> leafs;
8
9 public List<LeafEntity> getLeafs() {
10 return leafs;
11 }
12
13 public void setLeafs(List<LeafEntity> leafs) {
14 this.leafs = leafs;
15 }
16
17 public int getTid() {
18 return tid;
19 }
20
21 public void setTid(int tid) {
22 this.tid = tid;
23 }
24
25 public String getTname() {
26 return tname;
27 }
28
29 public void setTname(String tname) {
30 this.tname = tname;
31 }
32 }
 1 public class LeafEntity {
2 private int lid;
3 private String lname;
4 private String ldesc;
5
6
7 //一片叶子对应一棵树
8 private TreeEntity tree;
9
10 public String getLdesc() {
11 return ldesc;
12 }
13
14 public void setLdesc(String ldesc) {
15 this.ldesc = ldesc;
16 }
17
18 public TreeEntity getTree() {
19 return tree;
20 }
21
22 public void setTree(TreeEntity tree) {
23 this.tree = tree;
24 }
25
26 public int getLid() {
27 return lid;
28 }
29
30 public void setLid(int lid) {
31 this.lid = lid;
32 }
33
34 public String getLname() {
35 return lname;
36 }
37
38 public void setLname(String lname) {
39 this.lname = lname;
40 }
41 }

树的映射器接口:

public interface TreeMapper {

    //根据数的id查询数的信息
public TreeEntity queryTreeById(int id); //查询所有的数的信息
public List<TreeEntity> queryTreeList(); }

  树的xml配置:

 1 <!--mybatis的mapper的根节点-->
2 <mapper namespace="com.lv.study.mapper.TreeMapper">
3
4 <!--因为现在找的是树的信息所以需要树的entity-->
5 <resultMap id="treeMap" type="com.lv.study.entity.TreeEntity">
6 <id property="tid" column="tid"></id>
7 <result property="tname" column="tname"></result>
8
9 <!--一对一用的是Javatype 一对多用的是ofType-->
10 <!--一个实体类里面有多个实体类-->
11 <collection property="leafs" ofType="com.lv.study.entity.LeafEntity">
12 <!--不是说实体类里面有什么你就要写上面而是你写什么我们就查什么-->
13 <id property="lid" column="lid"></id>
14 <result property="lname" column="lname"></result>
15
16 <!--association是一个实体类里面有另外一个实体类-->
17 <association property="tree" javaType="com.lv.study.entity.TreeEntity">
18 <id property="tid" column="tid"></id>
19 <result property="tname" column="tname"></result>
20 </association>
21
22 </collection>
23 </resultMap>
24 <!-- #{id}是传的id sql语句里面不可以写注释不然会报异常-->
25 <!--查询一个数的实体类-->
26 <select id="queryTreeById" resultMap="treeMap">
27
28 SELECT t.*,l.* FROM tree t LEFT JOIN leaf l ON t.tid=l.tid WHERE t.tid = #{id}
29 </select>
30
31 <!--查询所有的数的实体类-->
32 <select id="queryTreeList" resultMap="treeMap">
33 SELECT t.*,l.* FROM tree t LEFT JOIN leaf l ON t.tid=l.tid
34 </select>

测试类:

 1   public static void main(String[] args) {
2
3
4 try {
5 String resource="mybatis-config.xml";//这个代表我们编译的根目录
6 InputStream is = null;//把配置文件弄成流的形式
7 is = Resources.getResourceAsStream(resource);
8 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
9 SqlSession sqlSession=sqlSessionFactory.openSession(true);
10 TreeMapper mapper=sqlSession.getMapper(TreeMapper.class);
11
12 TreeEntity treeEntity = mapper.queryTreeById(2);
13
14 List<TreeEntity> list = mapper.queryTreeList();
15
16 System.out.println();
17
18 } catch (IOException e) {
19 e.printStackTrace();
20 }

结果:

mybatis下的ResultMap配置一对一以及一对多的更多相关文章

  1. Mybatis注解开发多表一对一,一对多

    Mybatis注解开发多表一对一,一对多 一对一 示例:帐户和用户的对应关系为,多个帐户对应一个用户,在实际开发中,查询一个帐户并同时查询该账户所属的用户信息,即立即加载且在mybatis中表现为一对 ...

  2. Mybatis(四) 高级映射,一对一,一对多,多对多映射

    天气甚好,怎能不学习? 一.单向和双向 包括一对一,一对多,多对多这三种情况,但是每一种又分为单向和双向,在hibernate中我们就详细解析过这单向和双向是啥意思,在这里,在重复一遍,就拿一对多这种 ...

  3. 【mybatis深度历险系列】mybatis中的高级映射一对一、一对多、多对多

    学习hibernate的时候,小编已经接触多各种映射,mybatis中映射有到底是如何运转的,今天这篇博文,小编主要来简单的介绍一下mybatis中的高级映射,包括一对一.一对多.多对多,希望多有需要 ...

  4. mybatis入门基础----高级映射(一对一,一对多,多对多)

    阅读目录 一:订单商品数据模型 二.一对一查询 三.一对多查询 四.多对多查询 回到顶部 一:订单商品数据模型 1.数据库执行脚本 创建数据库表代码: CREATE TABLE items ( id ...

  5. Habernate配置一对一,一对多,多对多(二)

    一.开篇 紧接着上篇的博客来写:http://www.cnblogs.com/WJ--NET/p/7845000.html(habernate环境的搭建) 二.配置一对一 2.1.新建客户类和公司类( ...

  6. MyBatis入门(二)---一对一,一对多

    一.创建数据库表 1.1.创建数据表同时插入数据 /* SQLyog Enterprise v12.09 (64 bit) MySQL - 5.6.27-log : Database - mybati ...

  7. Mybatis 使用 mapper 接口规范的 一对一, 一对多,多对多映射

    首先的 是 最原始的 pojo 类来 做简单映射 简单 pojo 映射: <mapper namespace="com.ghc.dao.UserDao"> <se ...

  8. mybatis 框架 的应用之四(一对一 与 一对多)

    lf-driver=com.mysql.jdbc.Driver lf-url=jdbc:mysql://localhost:3306/test?allowMultiQueries=true&u ...

  9. mybatis之级联关系(一对一、一对多)

    0. 表结构 1. 准备工作 1.1 配置文件等信息,请参考  myBatis之入门示例 1.2 entity 1.2.1 TPersonInfo.java package com.blueStarW ...

  10. mybatis学习 十三 resultMap标签 一对一

    1 .<resultMap>标签 写在mapper.xml中,由程序员控制SQL查询结果与实体类的映射关系. 在写<select>标签中,有一个resultType属性,此时s ...

随机推荐

  1. PVE虚拟平台常用简明操作,三分钟搞定虚拟机更换安装配置

    Proxmox Virtual Environment是一个基于QEMU/KVM和LXC的开源服务器虚拟化管理解决方案,本文简称PVE,与之相类似的虚拟化平台是VMWARE的ESXi虚拟平台,相较于商 ...

  2. 【JDBC第6章】数据库事务理论

    第6章:数据库事务 6.1 数据库事务介绍 事务:一组逻辑操作单元,使数据从一种状态变换到另一种状态. 事务处理(事务操作):保证所有事务都作为一个工作单元来执行,即使出现了故障,都不能改变这种执行方 ...

  3. postgresql的日期函数

    一个to_char干完所有的活.包括日期的转换 函数 返回类型 描述 实例 to_char(timestamp, text) text 将时间戳转换为字符串 to_char(current_times ...

  4. JSON Objects Framework(1)

    学习datasnap,json必须掌握.用自身的JSON,就必须熟悉JSON Objects Framework.其中tostring和value区别就是一个坑. The JSON objects f ...

  5. Model Context Protocol(MCP)在claude使用

    定义 MCP通过统一的协议,使AI模型(如Claude.GPT等)能够动态调用外部工具(如数据库.API.本地文件等),并实现跨模型的上下文共享与协作 架构 客户端-服务器模型: MCP主机(Host ...

  6. web自动化的元素定位

    一.基础元素定位 1.id定位:根据元素的id定位 By.id("id") webDriver.findElement(By.id("tab-account") ...

  7. MySQL开启general_log

    General_log 详解 1.介绍 开启 general log 将所有到达MySQL Server的SQL语句记录下来. 一般不会开启开功能,因为log的量会非常庞大.但个别情况下可能会临时的开 ...

  8. SpringIntegrationRamble

    目录 Why SpringIntegration Background Consolidate Architecture ESB service Popular Solutions Getting S ...

  9. Nacos源码—2.Nacos服务注册发现分析二

    大纲 5.服务发现-服务之间的调用请求链路分析 6.服务端如何维护不健康的微服务实例 7.服务下线时涉及的处理 8.服务注册发现总结 5.服务发现-服务之间的调用请求链路分析 (1)微服务通过Naco ...

  10. Java编程——设计模式目录

    目录 学习指导 https://java-design-patterns.com/zh/patterns/