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。

相关推荐
CircleMouse1 天前
springboot如何通过提供的注解方式来操作Redis
java·spring boot·redis·spring·mybatis
荔枝吻1 天前
【抽丝剥茧知识讲解】引入mybtis-plus后,mapper实现方式
java·sql·mybatis
Allen Bright2 天前
【MyBatis-9】MyBatis分页插件PageHelper深度解析与实践指南
mybatis
柴薪之王、睥睨众生2 天前
(自用)Java学习-5.8(总结,springboot)
java·开发语言·spring boot·学习·mybatis
唐僧洗头爱飘柔95273 天前
【SSM-SSM整合】将Spring、SpringMVC、Mybatis三者进行整合;本文阐述了几个核心原理知识点,附带对应的源码以及描述解析
java·spring·mybatis·springmvc·动态代理·ioc容器·视图控制器
意倾城3 天前
浅说MyBatis-Plus 的 saveBatch 方法
java·mybatis
Brilliant Nemo3 天前
五、框架实战:SSM整合原理和实战
maven·mybatis
小赵面校招3 天前
Spring Boot整合MyBatis全攻略:原理剖析与最佳实践
java·spring boot·mybatis
小赵面校招3 天前
SpringBoot整合MyBatis-Plus:零XML实现高效CRUD
xml·spring boot·mybatis
悟空打码4 天前
MyBatis源码解读5(3.1、缓存简介)
缓存·mybatis