欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MybatisPlus實(shí)現(xiàn)對象嵌套關(guān)聯(lián)查詢一對多List集合查詢

 更新時(shí)間:2022年05月21日 12:03:57   作者:weixin_52366309  
這篇文章主要介紹了MybatisPlus實(shí)現(xiàn)對象嵌套關(guān)聯(lián)查詢一對多List集合查詢,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

對象嵌套關(guān)聯(lián)查詢一對多List集合查詢

mybatis嵌套關(guān)聯(lián)查詢?nèi)缦?/h3>

由于我的是一對集合查詢,所以我有兩個類。

@Data
@TableName("tb_user")
public class User {
? ? @TableId(type= IdType.INPUT)
? ? private String id;
? ? @TableField("user_name")
? ? private String username;
? ? private String password;
? ? private String name;
? ? private String email;
? ? private int age;
? ? private ArrayList<Authority> list;
}

權(quán)限類

@Data
@TableName
public class Authority {
? ? @TableId(type = IdType.INPUT)
? ? @TableField("aid")
? ? private int id;
? ? @TableId("aname")
? ? private String name;
}

測試類

?@Test
? ? public void ManyToMany(){
? ? ? ? User user = userMapper.selectAuthorityById(1);
? ? ? ? ArrayList <Authority> list = user.getList();
? ? ? ? System.out.println(user);
? ? ? ? for (Authority authority : list) {
? ? ? ? ? ? System.out.println("所對應(yīng)權(quán)限為"+authority.getName());
? ? ? ? }
? ? }

springboot項(xiàng)目的依賴

? ? ? ?<dependency>
? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? <artifactId>spring-boot-starter</artifactId>
? ? </dependency>
? ? ? ? <dependency>
? ? ? ? <groupId>mysql</groupId>
? ? ? ? <artifactId>mysql-connector-java</artifactId>
? ? ? ? <version>5.1.26</version>
? ? </dependency>
? ? ? ? <dependency>
? ? ? ? <groupId>org.projectlombok</groupId>
? ? ? ? <artifactId>lombok</artifactId>
? ? ? ? <optional>true</optional>
? ? </dependency>
? ? ? ? <dependency>
? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? <artifactId>spring-boot-starter-test</artifactId>
? ? ? ? <scope>test</scope>
? ? </dependency>
? ? ? ?<!--mybatis plus 起步依賴-->
? ? ? ? <dependency>
? ? ? ? <groupId>com.baomidou</groupId>
? ? ? ? <artifactId>mybatis-plus-boot-starter</artifactId>
? ? ? ? <version>3.4.0</version>
? ? </dependency>

這下面就是我xml文件里面怎么寫的嵌套查詢語句

<mapper namespace="com.itheima.mybatisplus.mapper.UserMapper">
? ? <!--返回的對象為authority-->
? ? <resultMap id="authority" type="com.itheima.mybatisplus.domain.User">
? ? ? ? <id column="id" property="id"/>
? ? ? ? <id column="password" property="password"/>
? ? ? ? <id column="age" property="age"/>
? ? ? ? <id column="email" property="email"/>
? ? ? ? <id column="name" property="name"/>
? ? ? ? <id column="user_name" property="username"/>
? ? ? <collection property="list"
? ? ? ? ? ? ? ? ? ofType="com.itheima.mybatisplus.domain.Authority">
? ? ? ? ? <id property="id" column="aid"/>
? ? ? ? ? <id property="name" column="aname"/>
? ? ? </collection>
?? ?<select id="selectAuthorityById" parameterType="int" resultMap="authority">
? ? ? ?SELECT * FROM
? ? ? ? ?authority a,tb_user t,user_authority ua
? ? ? ? ?WHERE a.aid=ua.authority_id
? ? ? ? ?AND t.id=ua.user_id
? ? ? ? ?AND t.id=#{id}
? ? </select>

數(shù)據(jù)庫的配置我就不放了,直接編寫就可以了,看會下面這個xml配置就可以了 

一對多查詢(經(jīng)典案例)

條件

查詢班級表 返回所有學(xué)生信息  (一對多問題)

數(shù)據(jù)庫

班級class_info

學(xué)生student

代碼實(shí)現(xiàn)

<!--        多對一  或者 一對一   -->
<!--        <association property=""-->
<!--        一對多 返回集合-->
<!- -  <collection  property=""- ->

實(shí)體類ClassInfo.java

@Data
public class ClassInfo {
 
    private Long id;
    private String name;
    private String nameTest; 
    private List<Student> studentList;
}

ClassInfoMapper.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">
<!--名稱空間:對應(yīng)mapper層某個接口的包的全名稱-->
<mapper namespace="com.example.demo.mapper.ClassInfoMapper">
 
    <!--    查詢班級 返回所有學(xué)生的信息   一對多-->
 
    <!--    自定義映射規(guī)則-->
    <resultMap id="OneToMany" type="com.qcby.zsgc.entity.ClassInfo">
        <result column="name" jdbcType="VARCHAR" property="nameTest" />
        <collection  column="{id1=id,name=name}"
                     property="studentList"
                     select="com.example.demo.mapper.StudentMapper.listByClassInfoId">                 </collection>
    </resultMap>
 
    <select id="listAllWithStudent" resultMap="OneToMany">
        select * from class_info
    </select>

關(guān)聯(lián)StudentMapper.xml中的子查詢

 <select id="listByClassInfoId" resultType="com.example.demo.entity.Student">
        SELECT
           *
        FROM
            student s
        where class_info_id = #{id1} or name = #{name}
    </select>

ClassInfoMapper.java

public interface ClassInfoMapper extends BaseMapper<ClassInfo> {??
? ? IPage<ClassInfo> listAllWithStudent(IPage<ClassInfo> page);?
}

ClassInfoService.java

public interface ClassInfoService extends IService<ClassInfo> {?
? ? IPage<ClassInfo> listAllWithStudent(IPage<ClassInfo> page);?
}

ClassInfoServiceImpl.java

@Service
public class ClassInfoServiceImpl extends ServiceImpl<ClassInfoMapper, ClassInfo> implements ClassInfoService {
? ? @Autowired
? ? private StudentService studentService;
? ? @Override
? ? public IPage<ClassInfo> listAllWithStudent(IPage<ClassInfo> page) {
? ? ? ? return this.baseMapper.listAllWithStudent(page);
? ? }
}

ClassInfoController.java

@Controller
@RequestMapping("classInfo")
public class ClassInfoController {
?
? ? @Autowired
? ? private ClassInfoService classInfoService;
?
? ? @RequestMapping("listAllWithStudent")
? ? @ResponseBody
? ? public IPage<ClassInfo> listAllWithStudent(Integer pageNo,Integer pageSize){
? ? ? ? Page<ClassInfo> page = new Page<>(pageNo,pageSize);
? ? ? ? return classInfoService.listAllWithStudent(page);
? ? }?
}

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

相關(guān)文章

  • Spring中過濾器(Filter)和攔截器(Interceptor)的區(qū)別和聯(lián)系解析

    Spring中過濾器(Filter)和攔截器(Interceptor)的區(qū)別和聯(lián)系解析

    在我們?nèi)粘5拈_發(fā)中,我們經(jīng)常會用到Filter和Interceptor,這篇文章主要介紹了Spring中過濾器(Filter)和攔截器(Interceptor)的區(qū)別和聯(lián)系?,需要的朋友可以參考下
    2022-10-10
  • Spring Boot 整合 Mockito提升Java單元測試的高效實(shí)踐案例

    Spring Boot 整合 Mockito提升Java單元測試的高效實(shí)踐案例

    Mockito與Spring Boot的整合為Java開發(fā)者提供了一套完整的解決方案,使得單元測試更為精準(zhǔn)、高效,從而確保了代碼質(zhì)量、降低了維護(hù)成本,并促進(jìn)了項(xiàng)目的持續(xù)集成與交付,感興趣的朋友跟隨小編一起看看吧
    2024-04-04
  • Java如何自動生成api文檔

    Java如何自動生成api文檔

    在?Java?開發(fā)中,自動生成?API?文檔是一項(xiàng)非常實(shí)用的功能,這篇為大家介紹了幾種常見的?Java?自動生成?API?文檔的方式,需要的可以參考一下
    2025-02-02
  • 10分鐘帶你理解Java中的反射

    10分鐘帶你理解Java中的反射

    反射是java中一種強(qiáng)大的工具,能夠使我們很方便的創(chuàng)建靈活的代碼,這篇文章帶大家十分鐘快速理解Java中的反射,有需要的可以參考借鑒。
    2016-08-08
  • Java如何使用interrupt()終止線程

    Java如何使用interrupt()終止線程

    這篇文章主要介紹了Java如何使用interrupt()終止線程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 解決SpringBoot應(yīng)用啟動失敗:UnsatisfiedDependencyException與NoSuchBeanDefinitionException

    解決SpringBoot應(yīng)用啟動失敗:UnsatisfiedDependencyException與NoSuchBean

    在Spring?Boot開發(fā)過程中,啟動應(yīng)用時(shí)可能會遇到各種錯誤,其中最常見的就是UnsatisfiedDependencyException和NoSuchBeanDefinitionException,下面我們來看看該如何解決呢
    2025-06-06
  • WebUploader實(shí)現(xiàn)圖片上傳功能

    WebUploader實(shí)現(xiàn)圖片上傳功能

    這篇文章主要為大家詳細(xì)介紹了WebUploader實(shí)現(xiàn)圖片上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • eclipse導(dǎo)入IntelliJ IDEA的maven項(xiàng)目的示例

    eclipse導(dǎo)入IntelliJ IDEA的maven項(xiàng)目的示例

    本篇文章主要介紹了eclipse導(dǎo)入IntelliJ IDEA的maven項(xiàng)目的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • springboot+jwt+springSecurity微信小程序授權(quán)登錄問題

    springboot+jwt+springSecurity微信小程序授權(quán)登錄問題

    這篇文章主要介紹了springboot+jwt+springSecurity微信小程序授權(quán)登錄問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Java面試題沖刺第二十七天--JVM2

    Java面試題沖刺第二十七天--JVM2

    這篇文章主要為大家分享了最有價(jià)值的三道關(guān)于JVM的面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下
    2021-08-08

最新評論