MyBatis — 多表查询

导读:本篇文章讲解 MyBatis — 多表查询,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

一、准备工作

博客系统场景。

创建库、表:

-- 创建数据库
drop database if exists mycnblog;
create database mycnblog DEFAULT CHARACTER SET utf8mb4;

-- 使用数据数据
use mycnblog;

-- 创建表[用户表]
drop table if exists  userinfo;
create table userinfo(
    id int primary key auto_increment,
    username varchar(100) not null,
    password varchar(32) not null,
    photo varchar(500) default '',
    createtime datetime default now(),
    updatetime datetime default now(),
    `state` int default 1
) default charset 'utf8mb4';

-- 创建文章表
drop table if exists  articleinfo;
create table articleinfo(
    id int primary key auto_increment,
    title varchar(100) not null,
    content text not null,
    createtime datetime default now(),
    updatetime datetime default now(),
    uid int not null,
    rcount int not null default 1,
    `state` int default 1
)default charset 'utf8mb4';

用户表实体类:

package com.example.demo.model;

import lombok.Data;

import java.util.Date;

/**
 * 普通的实体类,用于 Mybatis 做数据库表(userinfo表)的映射
 */
@Data
public class UserInfo {
    private int id;
    private String name;
    private String password;
    private String photo;
    private Date createtime;
    private Date updatetime;
    private int state;
}

文章表实体类:

package com.example.demo.model;

import lombok.Data;

import java.util.Date;

/**
 * 文章表的实体类
 */
@Data
public class ArticleInfo {
    private int id;
    private String title;
    private String content;
    private Date createtime;
    private Date updatetime;
    private int uid;
    private int rcount; // 访问量
    private int state; // 状态(预览字段)
    private String username; // 文章作者名
    //..
}
  • 创建接口 UserMapper 与其对应的 UserMapper.xml 文件
  • 创建接口 ArticleInfoMapper 与其对应的 ArticleMapper.xml 文件

二、多表查询

文章表 articleinfo 的 uid 字段是一个外键,关联到 userinfo 表中的信息了。
(查询文章时也需要显示用户信息)

在这里插入图片描述

在这里插入图片描述

ArticleInfoMapper 接口中创建 getAll 方法:public List<ArticleInfo> getAll();

单元测试:

package com.example.demo.mapper;

import com.example.demo.model.ArticleInfo;
import com.example.demo.model.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest // 当前测试的上下文环境为 springboot
class ArticleInfoMapperTest {

    @Autowired
    private ArticleInfoMapper articleInfoMapper;
    
    @Test
    void getAll() {
        List<ArticleInfo> list = articleInfoMapper.getAll();
        for (ArticleInfo item:list){
            System.out.println(item);
        }
    }
}

这样就能成功查询出所有文章 (带有文章作者名信息) !
想查询用户表其他字段的话,按照相同的方式加上就行了 ~

属性名与字段名不匹配

在这里插入图片描述

我们把 username 修改为 name,此时实体类属性名就与数据库表字段名不匹配了。

方案一:使用 resultMap

博客链接:https://blog.csdn.net/yyhgo_/article/details/128713697?spm=1001.2014.3001.5501

方案二:sql 中起别名

在这里插入图片描述

单元测试:
在这里插入图片描述
同样能够查询出 ~

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/118544.html

(0)

相关推荐

发表回复

登录后才能评论
半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!