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。

相关推荐
柯南二号19 小时前
【Java后端】MyBatis-Plus 原理解析
java·开发语言·mybatis
Easocen19 小时前
Mybatis学习笔记(五)
笔记·学习·mybatis
qq_三哥啊1 天前
【IDEA】设置Debug调试时调试器不进入特定类(Spring框架、Mybatis框架)
spring·intellij-idea·mybatis
柯南二号1 天前
【Java后端】Spring Boot 集成 MyBatis-Plus 全攻略
java·spring boot·mybatis
记忆不曾留1 天前
Mybatis 源码解读-SqlSession 会话源码和Executor SQL操作执行器源码
mybatis·二级缓存·sqlsession会话·executor执行器·一级缓存localcache
昵称为空C3 天前
SpringBoot 实现DataSource接口实现多租户数据源切换方案
后端·mybatis
isyangli_blog3 天前
(2-10-1)MyBatis的基础与基本使用
java·开发语言·mybatis
_码农121383 天前
Mybatis简单练习注解sql和配置文件sql+注解形式加载+配置文件加载
mybatis
期待のcode3 天前
Maven
java·spring·maven·mybatis
独泪了无痕3 天前
一文搞懂MyBatis中的TypeHandler
数据库·后端·mybatis