0. 表结构

1. 准备工作

1.1 配置文件等信息,请参考  myBatis之入门示例

1.2 entity

  1.2.1 TPersonInfo.java

package com.blueStarWei.entity;

public class TPersonInfo {

    private Integer id;
private String name;
private Integer age;
private Address address; //setter & getter @Override
public String toString() {
return "TPersonInfo [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
} }

1.2.2 Address.java

package com.blueStarWei.entity;

public class Address {

    private int id;
private String country;
private String city; //setter & getter @Override
public String toString() {
return "Address [country=" + country + ", city=" + city + "]";
} }

2 一对一关系(Maper)

2.1 方法一:

2.1.1 PersonAddressMapper.java

package com.blueStarWei.mappers;

import java.util.List;

import com.blueStarWei.entity.TPersonInfo;

public interface PersonAddressMapper {

    List<TPersonInfo> findAllWithAddress();
}

2.1.2 PersonAddressMapper.xml

<?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.blueStarWei.mappers.PersonAddressMapper"> <select id="findAllWithAddress" resultMap="personResult">
SELECT t1.*,t2.* FROM t_person_info t1 JOIN t_address t2 ON t2.id = t1.addressid
</select> <resultMap type="TPersonInfo" id="personResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" resultMap="addressResult"/>
</resultMap> <resultMap type="Address" id="addressResult">
<id property="id" column="id"/>
<result property="country" column="country"/>
<result property="city" column="city"/>
</resultMap> </mapper>

2.2 方法二【推荐】

2.2.1 AddressMapper.java

package com.blueStarWei.mappers;

import com.blueStarWei.entity.Address;

public interface AddressMapper {

    public Address findById(Integer id);
}

2.2.2 AddressMapper.xml

<?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.blueStarWei.mappers.AddressMapper"> <select id="findById" parameterType="Integer" resultMap="addressResult">
SELECT * FROM t_address t where t.id = #{id}
</select> <resultMap type="Address" id="addressResult">
<id property="id" column="id"/>
<result property="country" column="country"/>
<result property="city" column="city"/>
</resultMap> </mapper>

2.2.3 PersonMapper.java

package com.blueStarWei.mappers;

import java.util.List;

import com.blueStarWei.entity.TPersonInfo;

public interface PersonMapper {

    TPersonInfo findById(Integer id);

    List<TPersonInfo> findAll();
}

2.2.4 PersonMapper.xml

<?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.blueStarWei.mappers.PersonMapper"> <select id="findById" parameterType="Integer" resultMap="personResult">
select * from t_person_info a where a.id = #{id}
</select> <select id="findAll" resultMap="personResult">
select * from t_person_info
</select> <resultMap type="TPersonInfo" id="personResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" column="addressid"
select="com.blueStarWei.mappers.AddressMapper.findById"/>
</resultMap> </mapper>

2.3. 总结:

方法一是使用级联的方式查找出需要的所有信息,然后将结果返回到对应的entity类中,方法二是通过外键关联的方式返回结果。方法二在开发过程中被推荐时间,因为其具有代码高复用性。

3 一对多关系

3.1 Family.java

package com.blueStarWei.entity;

import java.util.List;

public class Family {

    private Integer id;
private String familyCode;
private List<TPersonInfo> persons; //setter & getter @Override
public String toString() {
return "Family [id=" + id + ", familyCode=" + familyCode + ", persons=" + persons + "]";
} }

3.2 FamilyMapper.java

package com.blueStarWei.mappers;

import com.blueStarWei.entity.Family;

public interface FamilyMapper {

    Family findById(Integer id);

}

3.3 FamilyMapper.xml

<?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.blueStarWei.mappers.FamilyMapper"> <select id="findById" parameterType="Integer" resultMap="familyResult">
SELECT * FROM t_family t where t.id = #{id}
</select> <resultMap type="Family" id="familyResult">
<id property="id" column="id"/>
<result property="familyCode" column="familyCode"/>
<collection property="persons" column="id" select="com.blueStarWei.mappers.PersonMapper.findByFamilyId"/>
</resultMap> </mapper>

3.4 PersonMapper.java

package com.blueStarWei.mappers;

import java.util.List;

import com.blueStarWei.entity.TPersonInfo;

public interface PersonMapper {

    List<TPersonInfo> findByFamilyId(Integer familyId);
}

3.5 PersonMapper.xml

<?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.blueStarWei.mappers.PersonMapper"> <select id="findByFamilyId" parameterType="Integer" resultMap="personResult">
select * from t_person_info a where a.familyId = #{familyId}
</select> <resultMap type="TPersonInfo" id="personResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" column="addressid"
select="com.blueStarWei.mappers.AddressMapper.findById"/>
</resultMap> </mapper>

mybatis之级联关系(一对一、一对多)的更多相关文章

  1. Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作

    Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作,单表查询,多表查询 一丶表与表之间的关系 背景: ​ ​ ​  ​ ​ 由于如果只使用一张表存储所有的数据,就会操作数 ...

  2. JPA级联(一对一 一对多 多对多)注解【实际项目中摘取的】并非自己实际应用

    下面把项目中的用户类中有个:一对一  一对多  多对多的注解对应关系列取出来用于学习      说明:项目运行正常 问题类:一对多.一对一.多对多 ============一对多 一方的设置 @One ...

  3. LayUI table表格控件 如何显示 对象中的属性(针对Mybatis的级联查询--一对一情况)

    1.entity如下: 2.Mybatis的Mapper.xml文件如下 <resultMap id="BaseResultMapPlus" type="dicIt ...

  4. MyBatis之级联——一对一关系

    在学数据库概论的时候会听到这么几个词:数据库的关系分为一对一.一对多.多对多.对于在学校里学的知识已经忘得差不多了,在这里简单的提一下数据库的关系.此篇是介绍MyBatis是如何实现数据库中一对一关系 ...

  5. MyBatis之级联——一对多关系

    上次我们讲到了MyBatis的一对一关系的表示,简单回顾一下一对一关系就是一个学生只有一个学生证.那么什么是一对多关系呢?一个学生有多个课程这就是一对多的关系.我们结合上一章中的学生和学生证,在此基础 ...

  6. Springboot整合Mybatis实现级联一对多CRUD操作

    在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...

  7. Spring Boot整合Mybatis完成级联一对多CRUD操作

    在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...

  8. Mybatis 级联查询 (一对多 )

    后台系统中 涉及到添加试卷 问题 答案的一个模块的.我需要通过试卷 查询出所有的试题,以及试题的答案.这个主要要使用到Mybatis的级联查询. 通过试卷 查询出与该试卷相关的试题(一对多),查询出试 ...

  9. hibernate 一对多(级联关系)

    hibernate 核心配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hiber ...

随机推荐

  1. Java的输入语句以及本周对于文件读写的研究

    日期:2018.9.20 博客期:010 星期四 ##:今天下午要考试 java(小考)!那么,我就应对相应的方法给出策略吧! 首先是 Java 里的输入语句,我一般是用Scanner类,用这个之前要 ...

  2. 初见TensorFlow :知其所以然

    2.1 TensorFlow的主要依赖包 TensorFlow依赖的两个最主要的工具包——Protocol Buffer和Bazel. 2.1.1 Protocol Buffer Protocol B ...

  3. 按照勾选 删除表格的行<tr>

    需求描述:有一个产品列表,有一个删减按钮,点击删减按钮,按照产品勾选的行,删除产品列表中对应的行数据 代码: //html代码<table id="table1"> & ...

  4. laravel 视图

    在实际开发中,除了 API 路由返回指定格式数据对象外,大部分 Web 路由返回的都是视图,以便实现更加复杂的页面交互,我们在前面已经看到过了视图的定义方式: return view('以.分隔的视图 ...

  5. arm指令1

    .section .text.writeFUNCTION(write) ldr r12, =__NR_write swi #0 bx lr LDR: LDR 的两种用法 1)LDR pc, =MyHa ...

  6. cf1051d 简单的状态压缩dp

    /* 给定一个二行n列的格子,在里面填黑白色,要求通过黑白色将格子分为k块 请问有多少种填色方式 dp[j][k][0,1,2,3] 填到第j列,有k块,第j列的颜色, */ #include< ...

  7. 在组件放使用v-model和slot插槽的简单实用

    封装的组件(SelectDefault.vue文件): <template> <div class="select-default"> <label& ...

  8. C和Java判断一个数字是否为素数

    C: /* 素数: 素数又称质数.所谓素数是指除了 1 和它本身以外,不能被任何整数整除的数,例如17就是素数,因为它不能被 2~16 的任一整数整除. */ # include <stdio. ...

  9. 数据增强(每10度进行旋转,进行一次增强,然后对每张图片进行扩充10张patch,最后得到原始图片数*37*10数量的图片)

    # -*- coding: utf-8 -*-"""Fourmi Editor This is a temporary script file.""& ...

  10. Centos7上实现不同网段的服务器文件共享

    目的:实现不同网段的服务器实现文件共享 前提:服务器1可以和共享服务器互通,共享服务器和服务器2互通 拓扑如下: 思路: 一般文件共享有涉及windown系统的用samba,纯类centos系统就用n ...