亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java之Mybatis多層嵌套查詢方式

 更新時間:2022年03月11日 08:57:16   作者:蒼穹之躍  
這篇文章主要介紹了Java之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');

實(shí)體類

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>

實(shí)體類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;//套餐對應(yīng)圖片存儲路徑
    @TableField(exist = false)
    private List<CheckGroup> checkGroups;//體檢套餐對應(yīng)的檢查組,多對多關(guān)系
}

實(shí)體類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;//注意事項(xiàng)
    @TableField(exist = false)
    private List<CheckItem> checkItems;//一個檢查組合包含多個檢查項(xiàng)
}

實(shí)體類CheckItem

@Data
@TableName("t_checkitem")
public class CheckItem {
    @TableId(type = IdType.AUTO)
    private Integer id;//主鍵
    private String code;//項(xiàng)目編碼
    private String name;//項(xiàng)目名稱
    private String sex;//適用性別
    private String age;//適用年齡(范圍),例如:20-50
    private Float price;//價格
    private String type;//檢查項(xiàng)類型,分為檢查和檢驗(yàn)兩種類型
    private String remark;//項(xiàng)目說明
    private String attention;//注意事項(xiàng)
}

中間表t_setmeal_checkgroup

中間表t_checkgroup_checkitem

可以看出Setmeal里面包含多個CheckGroup,而CheckGroup包括多個CheckItem

mapper層

CheckItemMapper
/**
? ? ?* 根據(jù)檢查組得到檢查項(xiàng)
? ? ?* @param checkgroupId
? ? ?* @return
? ? ?*/
? ? List<CheckItem> findCheckItemById(@Param("checkgroupId") Integer checkgroupId);

CheckItemMapper.xml

<!--根據(jù)檢查組id查詢檢查項(xiàng)信息-->
? ? <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

/**
? ? ?* 根據(jù)體驗(yàn)套餐的id得到檢查項(xiàng)的分組
? ? ?* @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>
? ? <!--根據(jù)套餐id查詢檢查項(xiàng)信息-->
? ? <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"應(yīng)該是把CheckGroup的id當(dāng)作參數(shù)傳給findCheckGroupBySetmealId

SetmealMapper

/**
? ? ?* 根據(jù)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"應(yīng)該就是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

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java_object的簡單使用詳解

    java_object的簡單使用詳解

    下面小編就為大家?guī)硪黄猨ava_object的簡單使用詳解。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • Java把數(shù)字格式化為貨幣字符串實(shí)例代碼

    Java把數(shù)字格式化為貨幣字符串實(shí)例代碼

    這篇文章主要介紹了Java把數(shù)字格式化為貨幣字符串實(shí)例代碼,需要的朋友可以參考下
    2014-02-02
  • java連接Access數(shù)據(jù)庫的方法

    java連接Access數(shù)據(jù)庫的方法

    這篇文章主要為大家詳細(xì)介紹了java連接Access數(shù)據(jù)庫的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • mybatis如何封裝List<String>類型屬性

    mybatis如何封裝List<String>類型屬性

    這篇文章主要介紹了mybatis如何封裝List<String>類型屬性問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • spring單元測試下模擬rabbitmq的實(shí)現(xiàn)

    spring單元測試下模擬rabbitmq的實(shí)現(xiàn)

    這篇文章主要介紹了spring單元測試下模擬rabbitmq的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • spring使用redis操作key-value的示例代碼

    spring使用redis操作key-value的示例代碼

    這篇文章主要介紹了spring使用redis操作key-value的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Mybatis-Plus如何使用分頁實(shí)例詳解

    Mybatis-Plus如何使用分頁實(shí)例詳解

    最近在研究mybatis,然后就去找簡化mybatis開發(fā)的工具,下面這篇文章主要給大家介紹了關(guān)于Mybatis-Plus如何使用分頁的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03
  • Java實(shí)現(xiàn)飛機(jī)小游戲

    Java實(shí)現(xiàn)飛機(jī)小游戲

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)飛機(jī)小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • 詳解Java的MyBatis框架中的事務(wù)處理

    詳解Java的MyBatis框架中的事務(wù)處理

    利用MyBatis框架的配置管理比直接使用JDBC API編寫事務(wù)控制要來得更加輕松,這里我們就來詳解Java的MyBatis框架中的事務(wù)處理,尤其是和Spring框架集成后更加exciting
    2016-06-06
  • JAVA反射機(jī)制中g(shù)etClass和class對比分析

    JAVA反射機(jī)制中g(shù)etClass和class對比分析

    這篇文章主要介紹了JAVA反射機(jī)制中g(shù)etClass和class對比分析,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11

最新評論