mybatis中的Collection嵌套实现树状结构

2019-05-23  本文已影响0人  dwwl

由 MyBatisde ResultMap标签 及 Collection标签 生成树状结构图

表关系:

三张表:

One顶级 Two中间 Three最低级

One.java

package com.huang.pojo;

import java.util.List;

/**
 * @author 
 * @date 2019/5/22
 */
public class One {
    private int id;
    private String name;
    private List<Two> twos;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Two> getTwos() {
        return twos;
    }

    public void setTwos(List<Two> twos) {
        this.twos = twos;
    }

    @Override
    public String toString() {
        return "One{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", twos=" + twos +
                '}';
    }
}

Two.java

package com.huang.pojo;

import java.util.List;

/**
 * @author 
 * @date 2019/5/22
 */
public class Two {
    private int two_id;
    private String name;
    private int one_id;
    private List<Three> threes;

    public int getTwo_id() {
        return two_id;
    }

    public void setTwo_id(int two_id) {
        this.two_id = two_id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getOne_id() {
        return one_id;
    }

    public void setOne_id(int one_id) {
        this.one_id = one_id;
    }

    public List<Three> getThrees() {
        return threes;
    }

    public void setThrees(List<Three> threes) {
        this.threes = threes;
    }

    @Override
    public String toString() {
        return "Two{" +
                "two_id=" + two_id +
                ", name='" + name + '\'' +
                ", one_id=" + one_id +
                ", threes=" + threes +
                '}';
    }
}

Three.java

package com.huang.pojo;

/**
 * @author huangQiChang
 * @date 2019/5/22
 */
public class Three {
    private int three_id;
    private String name;
    private int two_id;

    public int getThree_id() {
        return three_id;
    }

    public void setThree_id(int three_id) {
        this.three_id = three_id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getTwo_id() {
        return two_id;
    }

    public void setTwo_id(int two_id) {
        this.two_id = two_id;
    }

    @Override
    public String toString() {
        return "Three{" +
                "three_id=" + three_id +
                ", name='" + name + '\'' +
                ", two_id=" + two_id +
                '}';
    }
}

TreeDao.java

package com.huang.dao;

import com.huang.pojo.One;
import com.huang.pojo.Three;
import com.huang.pojo.Two;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * @author 
 * @date 2019/5/22
 */
@Mapper
public interface TreeDao {
    List<One> showTree();

    List<Two> findTwosByOneId(int id);

    List<Three> findThreesByTwoId(int two_id);

    List<Two> findTwos();
}

TreeDao.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.huang.dao.TreeDao">
    <resultMap id="result" type="com.huang.pojo.One">
        <collection property="twos" ofType="com.huang.pojo.Two" column="id" select="findTwosByOneId">
        </collection>
    </resultMap>
    @localhost
    <select id="showTree" resultMap="result">
        select *
        from mtree.one where id = 1;
    </select>

    <resultMap id="two_threes" type="com.huang.pojo.Two">
        <collection property="threes" ofType="com.huang.pojo.Three" column="two_id" select="findThreesByTwoId"/>
    </resultMap>
    <select id="findTwosByOneId" resultMap="two_threes">
        select *
        from mtree.two
        where one_id = #{id};
    </select>

    <select id="findThreesByTwoId" resultType="com.huang.pojo.Three">
        select *
        from mtree.three
        where two_id = #{two_id};
    </select>
    <select id="findTwos" resultMap="two_threes">
        select *
        from mtree.two;
    </select>


</mapper>

最后一个老生常谈 但不得不注意的问题:

在使用springBoot整合mybatis的时候,注意在Dao接口类上加注解@Mapper,在appliaction.yml

添加xml扫描的配置和别名扫描那个配置,以及显示sql的配置

mybatis:
  type-aliases-package: com.huang.pojo
  mapper-locations: mappers/*.xml

application.yml

spring:
  datasource:
    url: xxxxxxxx
    driver-class-name: xxxxxxxx
    username: xxxxxxx
    password: xxxxxxx
server:
  port: 9001
mybatis:
  type-aliases-package: com.huang.pojo
  mapper-locations: mappers/*.xml
logging:
  level:
    com:
     huang:
       bmDemo:
         dao: DEBUG
上一篇下一篇

猜你喜欢

热点阅读