溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2022-05-23 09:36:03 來源:億速云 閱讀:2510 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“MybatisPlus如何實(shí)現(xiàn)對(duì)象嵌套關(guān)聯(lián)查詢一對(duì)多List集合查詢”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“MybatisPlus如何實(shí)現(xiàn)對(duì)象嵌套關(guān)聯(lián)查詢一對(duì)多List集合查詢”吧!

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

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

由于我的是一對(duì)集合查詢,所以我有兩個(gè)類。

@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("所對(duì)應(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">
    <!--返回的對(duì)象為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ù)庫的配置我就不放了,直接編寫就可以了,看會(huì)下面這個(gè)xml配置就可以了 

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

條件

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

數(shù)據(jù)庫

班級(jí)class_info

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

學(xué)生student

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

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

<!--        多對(duì)一  或者 一對(duì)一   -->
<!--        <association property=""-->
<!--        一對(duì)多 返回集合-->
<!- -  <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">
<!--名稱空間:對(duì)應(yīng)mapper層某個(gè)接口的包的全名稱-->
<mapper namespace="com.example.demo.mapper.ClassInfoMapper">
 
    <!--    查詢班級(jí) 返回所有學(xué)生的信息   一對(duì)多-->
 
    <!--    自定義映射規(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);
    } 
}

感謝各位的閱讀,以上就是“MybatisPlus如何實(shí)現(xiàn)對(duì)象嵌套關(guān)聯(lián)查詢一對(duì)多List集合查詢”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)MybatisPlus如何實(shí)現(xiàn)對(duì)象嵌套關(guān)聯(lián)查詢一對(duì)多List集合查詢這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI