我是靠谱客的博主 尊敬小笼包,最近开发中收集的这篇文章主要介绍使用mybatis进行数据库中同一张表中的父子类别查询,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

同一张表中的父子查询

表中parent_id与id对应
在这里插入图片描述

Category.java
package com.meutech.entity;

import java.util.List;

public class Category {
    private Integer id;
    private Integer parent_id;
    private String name;
    private List<Category> childcategory;

    public List<Category> getChildcategory() {
        return childcategory;
    }

    public void setChildcategory(List<Category> childcategory) {
        this.childcategory = childcategory;
    }

    @Override
    public String toString() {
        return "Category{" +
                "id=" + id +
                ", parent_id=" + parent_id +
                ", name='" + name + ''' +
                ", childcategory=" + childcategory +
                '}';
    }

    public Integer getId() {
        return id;
    }

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

    public Integer getParent_id() {
        return parent_id;
    }

    public void setParent_id(Integer parent_id) {
        this.parent_id = parent_id;
    }

    public String getName() {
        return name;
    }

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

    public Category() {
    }
}

Category.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.meutech.mapper.Categorymapper">
    <resultMap id="Base_category" type="com.meutech.entity.Category">
        <id column="pid" property="id"/>
        <result column="ppia" property="parent_id"/>
        <result column="pname" property="name"/>
        <collection property="childcategory" ofType="com.meutech.entity.Category">
            <id column="cid" property="id"/>
            <result column="cpia" property="parent_id"/>
            <result column="cname" property="name"/>
        </collection>
    </resultMap>
   <select id="Listcategory" resultMap="Base_category">
       select p.id pid,p.parent_id ppid,p.name pname,c.id cid,c.parent_id cpid,c.name cname from cmt_category p inner join cmt_category c on p.id=c.parent_id
   </select>



</mapper>
Categorymapper.java
package com.meutech.mapper;

import com.meutech.entity.Category;

import java.util.List;

public interface Categorymapper {
    List<Category> Listcategory();
}

Categorytest.java
package com.meutech.test;

import com.meutech.entity.Category;
import com.meutech.mapper.Categorymapper;
import org.apache.ibatis.io.ResolverUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.util.List;

public class Categorytest {
    public static void main(String[] args) {
        SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(ResolverUtil.Test.class.getClassLoader().getResourceAsStream("mybatis.xml"));
        SqlSession sqlSession=factory.openSession(true);
        Categorymapper categorymapper=sqlSession.getMapper(Categorymapper.class);
        List<Category> result=categorymapper.Listcategory();
        for(Category category:result){
            System.out.println(result);
        }
    }
}
运行结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

分步查询

Category.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.meutech.mapper.Categorymapper">
    
<!--    分步查询-->
    <resultMap id="category1" type="com.meutech.entity.Category">
        <id column="id" property="id"/>
        <result column="parent_id" property="parent_id"/>
        <result column="name" property="name"/>
    </resultMap>
    <select id="ListcategoryByid" resultMap="category1">
        select * from cmt_category where parent_id=#{param1}
    </select>
    <resultMap id="category2" type="com.meutech.entity.Category">
        <id column="id" property="id"/>
        <result column="parent_id" property="parent_id"/>
        <result column="name" property="name"/>
        <collection property="childcategory" select="ListcategoryByid" column="id"></collection>
    </resultMap>
    <select id="Listcategoryleaf" resultMap="category2">
        select * from cmt_category where parent_id=0
    </select>

</mapper>
Categorymapper.java
package com.meutech.mapper;

import com.meutech.entity.Category;

import java.util.List;

public interface Categorymapper {
    List<Category> Listcategory();
    List<Category> ListcategoryByid(int parent_id);
    List<Category> Listcategoryleaf();
}

最后

以上就是尊敬小笼包为你收集整理的使用mybatis进行数据库中同一张表中的父子类别查询的全部内容,希望文章能够帮你解决使用mybatis进行数据库中同一张表中的父子类别查询所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部