Mybatis查树的两种写法

Mybatis查树必须会,它有两种写法:

1、联表查询。只访问一次数据库。

2、递归查询。访问多次数据库。

1、联表查询(推荐)

表结构:

java 复制代码
create table common_region
(
 region_id int(11),
 pr_region_id int(11),
 region_name varchar(60)

)ENGINE=InnoDB charset=utf8;
java 复制代码
INSERT INTO common_region
(region_id, pr_region_id, region_name)
VALUES(1, 0, '江苏省');
INSERT INTO common_region
(region_id, pr_region_id, region_name)
VALUES(2, 0, '江西省');
INSERT INTO common_region
(region_id, pr_region_id, region_name)
VALUES(3, 1, '南京市');
INSERT INTO common_region
(region_id, pr_region_id, region_name)
VALUES(4, 1, '苏州市');
INSERT INTO common_region
(region_id, pr_region_id, region_name)
VALUES(5, 2, '南昌市');

mapper:

java 复制代码
 List<Map<String,Object>> query( );

xxMapper.xml

java 复制代码
<?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.xy.dao.RegionDao">

    <resultMap id="BaseResultMap" type="map">
        <!--@Table common_region_cn-->
        <result property="regionId" column="region_id" jdbcType="INTEGER"/>
        <result property="prRegionId" column="pr_region_id" jdbcType="INTEGER"/>
        <result property="regionName" column="region_name" jdbcType="VARCHAR"/>

      <!--使用mybatis  collection 进行集合查询  以下说明:-->
        -- collection 即为嵌套的List配置
        -- property 为 private List list 的字段名 list
        -- ofType 为private List list 的类型MetaThemeTree
        -- select 为要递归的sql语句
        -- column 上一条语句查询的结果作为下一条语句的参数 -->
     <collection property="children" javaType="java.util.List" ofType="map">
            <result property="regionId" column="region_id2" jdbcType="INTEGER"/>
            <result property="regionName" column="region_name2" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>


    <!--查询指定行数据-->
    <select id="query" resultMap="BaseResultMap">
        select crc.region_id ,crc.region_name ,crc2.region_id as region_id2 ,crc2.region_name as region_name2
        from common_region crc join common_region crc2
                                       on crc.region_id  = crc2.pr_region_id
    </select>


</mapper>

这种写法,注意collection里面的字段不能和外面的相同,所以这里取了别名region_name2,但是属性是可以重复的。

像这种树的,要求的字段返回不会特别多,所以ofType用map就可以。

2、递归查询(不推荐)

mapper配置:

java 复制代码
 <!--使用mybatis  collection 进行集合查询  以下说明:-->
        -- collection 即为嵌套的List配置
        -- property 为 private List list 的字段名 list
        -- ofType 为private List list 的类型MetaThemeTree
        -- select 为要递归的sql语句
        -- column 上一条语句查询的结果作为下一条语句的参数 -->
    <resultMap id="ProvinceAndCityResultMap2" type="map">
        <result property="commonRegionId" column="COMMON_REGION_ID" jdbcType="INTEGER"/>
        <result property="regionName" column="REGION_NAME" jdbcType="VARCHAR"/>
        <collection property="children" javaType="java.util.List" ofType="map" select="selectChildren" column="COMMON_REGION_ID">
        </collection>
    </resultMap>
    <select id="selectParent" resultMap="ProvinceAndCityResultMap2">
        select crc.COMMON_REGION_ID,crc.REGION_NAME from common_region_cn crc
        where crc.REGION_TYPE = 1100 and crc.par_region_id =8100000
    </select>
    <select id="selectChildren" resultMap="ProvinceAndCityResultMap2">
        select crc.COMMON_REGION_ID,crc.REGION_NAME 
        from common_region_cn crc
        where crc.par_region_id = #{commonRegionId}
    </select>

mapper配置:

collection的参数:

property:子属性的字段名

javaType:属性类型,就是List

ofType: List中的类型

select:要递归的sql语句

column :上一条语句查询的结果作为下一条语句的参数,注意是数据库字段名。

由于递归的方法会多次访问数据库,所以不推荐。它的原理就是先查询selectParent的语句,然后再去查询selectChildren。

相关推荐
小徐敲java3 小时前
通用mybatis-plus查询封装(QueryGenerator)
mybatis
OEC小胖胖4 小时前
Spring Boot + MyBatis 项目中常用注解详解(万字长篇解读)
java·spring boot·后端·spring·mybatis·web
计算机学姐5 小时前
基于SpringBoot+Vue的在线投票系统
java·vue.js·spring boot·后端·学习·intellij-idea·mybatis
落落落sss5 小时前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis
罗曼蒂克在消亡8 小时前
2.3MyBatis——插件机制
java·mybatis·源码学习
cyt涛9 小时前
MyBatis 学习总结
数据库·sql·学习·mysql·mybatis·jdbc·lombok
OLDERHARD14 小时前
Java - MyBatis(上)
java·oracle·mybatis
计算机学姐1 天前
基于SpringBoot+Vue的高校运动会管理系统
java·vue.js·spring boot·后端·mysql·intellij-idea·mybatis
我是浮夸1 天前
MyBatisPlus——学习笔记
java·spring boot·mybatis
编程、小哥哥1 天前
手写mybatis之Mapper XML的解析和注册使用
xml·java·mybatis