Java之Mybatis多层嵌套查询方式
作者:苍穹之跃 发布时间:2023-06-17 11:48:52
标签:Mybatis,多层,嵌套,查询
Mybatis多层嵌套查询
三张表:user article blog
表的存储sql文件
/*
Navicat MySQL Data Transfer
Source Server : localhost
Source Server Version : 50620
Source Host : localhost:3306
Source Database : mybatis
Target Server Type : MYSQL
Target Server Version : 50620
File Encoding : 65001
Date: 2014-10-19 18:27:31
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(50) DEFAULT NULL,
`userAge` int(11) DEFAULT NULL,
`userAddress` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1', 'summer', '30', 'shanghai');
INSERT INTO `user` VALUES ('2', 'test1', '22', 'suzhou');
INSERT INTO `user` VALUES ('3', 'test1', '29', 'some place');
INSERT INTO `user` VALUES ('4', 'lu', '28', 'some place');
INSERT INTO `user` VALUES ('5', 'xiaoxun', '27', 'nanjing');
-- ----------------------------
-- Table structure for `article`
-- ----------------------------
DROP TABLE IF EXISTS `article`;
CREATE TABLE `article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`title` varchar(100) DEFAULT NULL,
`content` text,
`blogid` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of article
-- ----------------------------
INSERT INTO `article` VALUES ('1', '1', 'test_title_1', 'test_content_1', '1');
INSERT INTO `article` VALUES ('2', '1', 'test_title_2', 'test_content_2', '1');
INSERT INTO `article` VALUES ('3', '1', 'test_title_3', 'test_content_3', '2');
INSERT INTO `article` VALUES ('4', '1', 'test_title_4', 'test_content_4', '2');
INSERT INTO `article` VALUES ('5', '2', 'test_title_5', 'test_content_5', '2');
-- ----------------------------
-- Table structure for `blog`
-- ----------------------------
DROP TABLE IF EXISTS `blog`;
CREATE TABLE `blog` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of blog
-- ----------------------------
INSERT INTO `blog` VALUES ('1', 'xiaoxun_blog');
INSERT INTO `blog` VALUES ('2', 'zhang_blog');
实体类
package com.mybatis.test;
public class Article {
private int id;
private User user;
private String title;
private String content;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
package com.mybatis.test;
import java.util.List;
public class Blog {
private int id;
private String title;
private List<Article> articles;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<Article> getArticles() {
return articles;
}
public void setArticles(List<Article> articles) {
this.articles = articles;
}
}
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.mybatis.test.IBlogOperation">
<resultMap id="userResultMap" type="User">
<id property="id" column="user_id" />
<result property="userName" column="user_userName" />
<result property="userAge" column="user_userAge" />
<result property="userAddress" column="user_userAddress" />
</resultMap>
<resultMap id="articleResultMap" type="Article">
<id property="id" column="article_id" />
<result property="title" column="article_title" />
<result property="content" column="article_content" />
<association property="user" javaType="User" resultMap="userResultMap"/>
</resultMap>
<resultMap id="blogResultMap" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title" />
<!-- 将article list属性映射到collection -->
<collection property="articles" ofType="Article" resultMap="articleResultMap"/>
</resultMap>
<!-- select语句 -->
<select id="getBlogByID" parameterType="int" resultMap="blogResultMap">
select user.id user_id,user.userName user_userName,user.userAddress user_userAddress,
article.id article_id,article.title article_title,article.content article_content,
blog.id blog_id, blog.title blog_title
from user,article,blog
where user.id=article.userid and blog.id=article.blogid and blog.id=#{id}
</select>
</mapper>
Mybatis多层嵌套查询(多对多)
依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
实体类Setmeal
@Data
@TableName("t_setmeal")
public class Setmeal implements Serializable {
@TableId(type = IdType.AUTO)
private Integer id;
private String name;
private String code;
private String helpCode;
private String sex;//套餐适用性别:0不限 1男 2女
private String age;//套餐适用年龄
private Float price;//套餐价格
private String remark;
private String attention;
private String img;//套餐对应图片存储路径
@TableField(exist = false)
private List<CheckGroup> checkGroups;//体检套餐对应的检查组,多对多关系
}
实体类CheckGroup
@Data
@TableName("t_checkgroup")
public class CheckGroup {
@TableId(type = IdType.AUTO)
private Integer id;//主键
private String code;//编码
private String name;//名称
private String helpCode;//助记
private String sex;//适用性别
private String remark;//介绍
private String attention;//注意事项
@TableField(exist = false)
private List<CheckItem> checkItems;//一个检查组合包含多个检查项
}
实体类CheckItem
@Data
@TableName("t_checkitem")
public class CheckItem {
@TableId(type = IdType.AUTO)
private Integer id;//主键
private String code;//项目编码
private String name;//项目名称
private String sex;//适用性别
private String age;//适用年龄(范围),例如:20-50
private Float price;//价格
private String type;//检查项类型,分为检查和检验两种类型
private String remark;//项目说明
private String attention;//注意事项
}
中间表t_setmeal_checkgroup
中间表t_checkgroup_checkitem
可以看出Setmeal里面包含多个CheckGroup,而CheckGroup包括多个CheckItem
mapper层
CheckItemMapper
/**
* 根据检查组得到检查项
* @param checkgroupId
* @return
*/
List<CheckItem> findCheckItemById(@Param("checkgroupId") Integer checkgroupId);
CheckItemMapper.xml
<!--根据检查组id查询检查项信息-->
<select id="findCheckItemById" resultType="com.zhubayi.common.pojo.CheckItem">
select * from t_checkitem
where id
in (select checkitem_id from t_checkgroup_checkitem where checkgroup_id=#{checkgroupId})
</select>
CheckGroupMapper
/**
* 根据体验套餐的id得到检查项的分组
* @param setmealId
* @return
*/
List<CheckGroup> findCheckGroupBySetmealId(@Param("setmealId") Integer setmealId);
CheckGroupMapper.xml
<resultMap type="com.zhubayi.common.pojo.CheckGroup" id="baseResultMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="code" property="code"/>
<result column="help_code" property="helpCode"/>
<result column="sex" property="sex"/>
<result column="remark" property="remark"/>
<result column="attention" property="attention"/>
</resultMap>
<resultMap type="com.zhubayi.common.pojo.CheckGroup"
id="findByIdResultMap"
extends="baseResultMap">
<collection property="checkItems"
javaType="ArrayList"
ofType="com.zhubayi.common.pojo.CheckItem"
column="id"
select="com.zhubayi.provider.mapper.CheckItemMapper.findCheckItemById">
</collection>
</resultMap>
<!--根据套餐id查询检查项信息-->
<select id="findCheckGroupBySetmealId" resultMap="findByIdResultMap">
select * from t_checkgroup
where id
in (select checkgroup_id from t_setmeal_checkgroup where setmeal_id=#{id})
</select>
column="id"应该是把CheckGroup的id当作参数传给findCheckGroupBySetmealId
SetmealMapper
/**
* 根据id查询套餐信息
* @param id
* @return
*/
Setmeal findById(@Param("id") int id);
SetmealMapper.xml
<resultMap type="com.zhubayi.common.pojo.Setmeal" id="baseResultMap">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="code" property="code"/>
<result column="help_code" property="helpCode"/>
<result column="sex" property="sex"/>
<result column="age" property="age"/>
<result column="price" property="price"/>
<result column="remark" property="remark"/>
<result column="attention" property="attention"/>
<result column="img" property="img"/>
</resultMap>
<!--column="id"应该就是t_setmeal的id,然后传过去-->
<resultMap type="com.zhubayi.common.pojo.Setmeal"
id="findByIdResultMap"
extends="baseResultMap">
<collection property="checkGroups"
javaType="ArrayList"
ofType="com.zhubayi.common.pojo.CheckGroup"
column="id"
select="com.zhubayi.provider.mapper.CheckGroupMapper.findCheckGroupBySetmealId">
</collection>
</resultMap>
<select id="findById" resultMap="findByIdResultMap">
select * from t_setmeal where id=#{id}
</select>
测试
一个setmeal里面有多个checkGroup,checkGroup里面有多个checkItems
来源:https://blog.csdn.net/wenxingchen/article/details/88599024
0
投稿
猜你喜欢
- 定义与结构 备忘录(Memento)模式又称标记(Token)模式。GOF给备忘录模式的定义为:在不破坏
- 一、前言ConcurrentHashMap的源码采用了一种比较独特的方式对map中的元素数量进行统计,自然是要好好研究一下其原理思想,同时也
- 面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对象进行存储,集合就是存储对象最常用的一种方式。数组虽然也可以存
- package com.chen.lucene.image;import java.io.File;import java.io.FileI
- 前言在项目开发中,日志系统是必不可少的,用AOP在Web的请求做入参和出参的参数打印,同时对异常进行日志打印,避免重复的手写日志,完整案例见
- 本文实例为大家分享了Java实现寻找迷宫出路的具体代码,供大家参考,具体内容如下项目名称寻找迷宫出路项目描述给定一个自定义迷宫,0表示能通过
- 代码:package com.lwj.test.proxy;import java.lang.reflect.InvocationHandl
- 一、deleteById 和 delete为什么要把这两个方法放在一起呢?我们先看源码再说deleteById(Id id)(通过id进行删
- 前言今天遇到一个问题,我们有个ip.dat二进制文件,通过里面内容可以解析ip所在的地域信息,本地单元测试都是OK的,部署到测试环境后,发现
- 详解java中的PropertyChangeSupport与PropertyChangeListenerjava中的PropertyChan
- SpringMVC @RequestBody自动转json Http415错误项目中想用@RequestBody直接接收json串转成对象网
- 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了。近年来,随着HTML5的诞生,WebSocket协议被提出,它实
- @Param注解导致分页失效—分页 * 问题描述在使用mybatis分页时,使用@Param注解传入了两个对象,分页失效,查询出的总是全部的
- Jenkins集成Sonar过程中遇到的报错1、jenkins中无法添加sonarqube的token凭证因为添加的凭证类型错误,所以无法添
- 首先声明一点,这里的重试并不是报错以后的重试,而是负载均衡客户端发现远程请求实例不可到达后,去重试其他实例。@Bean@LoadBalanc
- 在平时的开发过程中,会有很多场景需要实时监听文件的变化,如下:1、通过实时监控 mysql 的 binlog 日志实现数据同步2、修改配置文
- 一、interrupt的使用特点我们先看2个线程打断的示例首先是可打断的情况:@Testpublic void interruptedTes
- spring boot RestTemplate 发送get请求踩坑闲话少说,代码说话RestTemplate 实例手动实例化,这个我基本不
- 核心配置文件mybatis-config.xml 系统核心配置文件MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属
- 使用zxing批量在做好的立牌背景图的指定位置上,把指定的文本内容(链接地址、文本等)生成二维码并放在该位置,最后加上立牌编号。步骤:1).