表格结构:

CREATE TABLE `ssmpdemo`.`person_test`  (
`id` varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
`type` int(4) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
INDEX `type`(`type`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; CREATE TABLE `ssmpdemo`.`type_test` (
`id` int(4) NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

使用Mapper文件

使用Mapper映射进行多表查询,通过定义字段的映射关系:

  1. 定义resultMap 定义当前实体的属性以及子对象的属性。
  2. 和数据库字段名称相同的属性也要定义。
  3. 外键可用 <association> 或者 <collection>。指定对应的类用 javaType="com.example.ssmpdemo.entity.MyType"字段。

实体类:

package com.example.ssmpdemo.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data; @Data
@TableName("person_test")
public class Person {
@TableId
@TableField("id")
private String id; @TableField("name")
private String name; @TableField(value = "type", exist = false)
private MyType myType;
}

Mapper 接口

package com.example.ssmpdemo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.ssmpdemo.entity.Person;
import java.util.List;
public interface PersonMapper extends BaseMapper<Person> { public List<Person> getFullData();
}

Mapper.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.example.ssmpdemo.mapper.PersonMapper"> <!--定义结果类型-->
<resultMap id="BaseResultMap" type="com.example.ssmpdemo.entity.Person">
<id property="id" column="id" jdbcType="VARCHAR"/>
<result property="name" column="NAME" jdbcType="VARCHAR"/>
<!--写成 association 和 collection 都可-->
<association property="myType" javaType="com.example.ssmpdemo.entity.MyType">
<id property="id" column="typeid" jdbcType="INTEGER" />
<result property="name" column="typename" jdbcType="VARCHAR" />
</association>
</resultMap> <!--定义查询语句,注意字段名不要相同,不然在结果类型中有同名字段无法匹配-->
<select id="getFullData" resultMap="BaseResultMap">
select person_test.*, type_test.id as typeid, type_test.name as typename from person_test, type_test where person_test.type=type_test.id
</select>
</mapper>

Mapper.xml 文件也可以简化。<collection> 中可以使用Mapper接口中已有的查询方法,避免重复定义子对象。

    <resultMap id="BaseResultMap" type="com.example.ssmpdemo.entity.Person">
<id property="id" column="id" jdbcType="VARCHAR"/>
<result property="name" column="NAME" jdbcType="VARCHAR"/>
<!--typeid表示外键字段-->
<collection property="myType" column="typeid"
select="com.example.ssmpdemo.mapper.TypeMapper.selectById" />
</resultMap>

使用 VO

建立VO

import lombok.Data;
@Data
public class PersonVO {
String id;
String name;
Integer typeid;
String typename;
}

使用@Select指定查询sql,查询的字段需要一一对应。

import java.util.List;

public interface PersonMapper extends BaseMapper<Person> {
public List<Person> getFullData(); @Select("select person_test.id, person_test.name ,type_test.id as typeid, type_test.name as typename \n" +
"from person_test, type_test \n" +
"where person_test.type=type_test.id\n")
public List<PersonVO> getFullData2();
}

也可使用Mapper.xml进行匹配。使用resultType指定类型。

    <select id="getFullData2" resultType="com.example.ssmpdemo.entity.vo.PersonVO">
select person_test.*, type_test.id as typeid, type_test.name as typename from person_test, type_test where person_test.type=type_test.id
</select>

Mybatis-Plus多表联查的更多相关文章

  1. Mybatis中多表联查,查询出来的字段出现重名,造成数据异常的解决方法!

    在做一对多出现的问题,引发的思考:当数据库表中,主表的主键id和明细表的中的字段名相同时怎么办?Mybatis进行自动映射赋值的时候会不会出现异常?                      注意:M ...

  2. mybatis.net 多表联查

    mybatis.net针对多表联查,其实不用讲联查出的所有的列全部做一个新的resultMap,我们完全可以通过集成关系来实现,真是上一次说的懒加载,在一定程度上可以提高其性能,但这并不是说懒加载性能 ...

  3. 使用Mybatis进行多表联查操作

    (1)增加一个测试数据库shop_order,sql语句如下: CREATE DATABASE `shop_order`; USE `shop_order`; CREATE TABLE `t_user ...

  4. mybatis的多表联查

    多对一连表查询简单记录

  5. 使用mybatis多表联查的时候结果异常及springmvc的理解

    今天使用mybatis多表联查的时候,在dos窗口查询时可以出结果集,但是使用mybatis查询的时候最后返回的结果只有最后一个结果 然后研究了半天没弄出来,后来无意中发现添加了最外层从表的ID字段后 ...

  6. 【mybatis】mybatis多表联查,存在一对多关系的,实体中使用List作为字段接收查询结果的写法

    实体如下: IntegralGoods  积分商品 IntegralGoodsImg 积分商品图片 ShelfLog 积分商品自动上架记录 IntegralGoods :IntegralGoodsIm ...

  7. mybatis-plus注解版实现多表联查(sql)

    mybatis注解版实现多表联查 需求: 用户有角色,角色有权限,需要一次取用户信息包含角色信息及其对应权限 实体类: package cn.zytao.taosir.common.model.use ...

  8. Mybatis中多表关联时,怎么利用association优雅写resultMap来映射vo

    前言 有好一阵没碰mybatis了,这次的项目基于性能考虑,选了mybatis,写着写着,发现有下面的需求,比如两表联查,取其中各一部分字段,怎么更方便地用vo来接,这里犯了难: 我想的是,因为这个s ...

  9. mybatis动态调用表名和字段名

    以后慢慢启用个人博客:http://www.yuanrengu.com/index.php/mybatis1021.html 一直在使用Mybatis这个ORM框架,都是使用mybatis里的一些常用 ...

  10. CDH中,执行HIVE脚本表联查权限问题。。

    文章来自http://www.cnblogs.com/hark0623/p/4174641.html 转发请注明 有时候执行表联查的时候总会出现没有权限写文件的情况. 这个时候使用sudo -H hi ...

随机推荐

  1. 基于bert_bilstm_crf的命名实体

    前言 本文将介绍基于pytorch的bert_bilstm_crf进行命名实体识别,涵盖多个数据集.命名实体识别指的是从文本中提取出想要的实体,本文使用的标注方式是BIOES,例如,对于文本虞兔良先生 ...

  2. html网页乱码原因以及解决办法

    一.乱码造成原因 1.如果网页源代码是gbk编写的,而内容中的文字是utf-8的,那么,此时打开浏览器就会出现HTML乱码.反之也会出现乱码. 2.HTML网页编码是gbk,但是程序从程序库中调出呈现 ...

  3. ARC125E - Snack (网络流)

    题面 有 N N N 种糖果, M M M 个小孩子,第 i i i 种糖果有 A i A_i Ai​ 个,第 i i i 个孩子不能有超过 B i B_i Bi​ 个同种类型的糖果,第 i i i ...

  4. KingbaseES 自增列三种方式

    KingbaseES中3种自增长类型sequence,serial,identity的区别: 对象 显示插入 显示插入后更新最大值 清空表后是否重置 是否跟事务一起回滚 多对象共享 支持重置 出现错误 ...

  5. Django 使用cmd 创建工程

    一.Django 安装 1 通过pip安装 Django 如果你是通过升级的方式安装Django,那么你需要先卸载旧的版本. Django 提供3种发行版本,推荐使用官方的稳定版本: 你的操作系统提供 ...

  6. 2.云原生之Docker容器环境安装实践

    转载自:https://www.bilibili.com/read/cv15181036/?from=readlist 官方一键安装脚本 补充时间:[2020年4月22日 11:00:59] 一键安装 ...

  7. Grafana Loki 学习之踩坑记

    转发自:https://mp.weixin.qq.com/s/zfXNEkdDC9Vqd9lh1ptC1g Grafana 出品的 loki 日志框架完美地与 kubernetes 的 label 理 ...

  8. 在 Kubernetes 集群中使用 NodeLocal DNSCache

    转载自:https://www.qikqiak.com/post/use-nodelocal-dns-cache/ NodeLocal DNSCache 通过在集群节点上运行一个 DaemonSet ...

  9. Dockerfile文件:设置变量启动的时候传递进去

    from openjdk:8-jdk-alpine RUN ln -sf /usr/share/zoneinfo/Asia/shanghai /etc/localtime RUN echo 'Asia ...

  10. 第一个Django应用 - 第四部分:表单和类视图

    一.表单form 为了接收用户的投票选择,我们需要在前端页面显示一个投票界面.让我们重写先前的polls/detail.html文件,代码如下: <h1>{{ question.quest ...