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。

相关推荐
李少兄5 小时前
解决MyBatis参数绑定中参数名不一致导致的错误问题
mybatis
异常君10 小时前
MyBatis 中 SqlSessionFactory 和 SqlSession 的线程安全性深度分析
java·面试·mybatis
bing_1581 天前
跨多个微服务使用 Redis 共享数据时,如何管理数据一致性?
redis·微服务·mybatis
云之兕1 天前
MyBatis 的动态 SQL
数据库·sql·mybatis
loser.loser1 天前
QQ邮箱发送验证码(Springboot)
java·spring boot·mybatis
毅航2 天前
Trae复刻Mybatis之旅(一):创建SqlSession会话,构建代理
后端·mybatis·trae
潮流coder2 天前
mybatis的if判断==‘1‘不生效,改成‘1‘.toString()才生效的原因
java·tomcat·mybatis
BillKu2 天前
Java + Spring Boot + Mybatis 实现批量插入
java·spring boot·mybatis
dog shit2 天前
web第十次课后作业--Mybatis的增删改查
android·前端·mybatis
emo了小猫2 天前
Mybatis #{} 和 ${}区别,使用场景,LIKE模糊查询避免SQL注入
数据库·sql·mysql·mybatis