首頁 > 軟體

MybatisPlus實現物件巢狀關聯查詢一對多List集合查詢

2022-05-21 19:01:03

物件巢狀關聯查詢一對多List集合查詢

mybatis巢狀關聯查詢如下

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

@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;
}

許可權類

@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("所對應許可權為"+authority.getName());
        }
    }

springboot專案的依賴

       <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>

資料庫的設定我就不放了,直接編寫就可以了,看會下面這個xml設定就可以了 

一對多查詢(經典案例)

條件

查詢班級表 返回所有學生資訊  (一對多問題)

資料庫

班級class_info

學生student

程式碼實現

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

實體類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">
<!--名稱空間:對應mapper層某個介面的包的全名稱-->
<mapper namespace="com.example.demo.mapper.ClassInfoMapper">
 
    <!--    查詢班級 返回所有學生的資訊   一對多-->
 
    <!--    自定義對映規則-->
    <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>

關聯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);
    } 
}

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com