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。

相关推荐
magic 2457 小时前
MyBatis的缓存、逆向工程、使用PageHelper、使用PageHelper
java·spring·maven·mybatis
XiaoLeisj8 小时前
【图书管理系统】深入解析基于 MyBatis 数据持久化操作:全栈开发图书管理系统:查询图书属性接口(注解实现)、修改图书属性接口(XML 实现)
xml·java·数据库·spring boot·sql·java-ee·mybatis
烟沙九洲9 小时前
MyBatis-Plus 的 FieldStrategy 属性
java·mybatis
三天不学习21 小时前
Lucene.Net 分词器选择指南:盘古分词 vs 结巴分词
.net·mybatis·lucene
要天天开心啊1 天前
mybatis的第四天学习笔记中
笔记·学习·mybatis
Determined_man1 天前
Mybatis-plus listObjs()
mybatis
Yharim2 天前
MyBatis中如何实现自定义插件
面试·mybatis
Y第五个季节2 天前
Redis 持久化
数据库·redis·mybatis
八股文领域大手子2 天前
《从 MyBatis-Plus 到 Elasticsearch:一个后端的性能优化踩坑实录》
elasticsearch·性能优化·mybatis