美文网首页SpringBoot
mybatis多表联合查询

mybatis多表联合查询

作者: WebGiser | 来源:发表于2020-10-13 17:15 被阅读0次

开发环境:postgresql数据库、idea工具、easy code插件、springboot+mybatis
数据库表:一个用户对应一个角色,一个角色对应多个用户。用户:角色=1:N
login_user 用户表


image.png

login_role 角色表


image.png

做几条测试数据:


image.png image.png

业务场景:

1、查询所有的用户和用户的角色信息
2、查询所有角色对应的所有用户

新建springboot+mybatis项目结构,并通过easy code插件,生成entity、service、controller、mapper文件

image.png

pom.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com</groupId>
    <artifactId>mybatis_test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis_test</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.16</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.properties代码:


# 数据库配置
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/test
spring.datasource.username=postgres
spring.datasource.password=WZF19920318

# mybatis配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.mybatis_test.entity

# showsql
logging.level.com.mybatis_test.dao = debug

LoginRoleDao.xml代码

    <!--方式一:代码复用性高, 主表分页查询正确-->
    <resultMap type="com.mybatis_test.entity.LoginRole" id="RoleUserMap">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="roleName" column="role_name" jdbcType="VARCHAR"/>
        <!--先查出主表的结果, 然后主表记录数是几条,就执行几次 collection 的select-->
        <collection property="loginUserList" javaType="java.util.ArrayList" ofType="com.mybatis_test.entity.LoginUser"
                    select="com.mybatis_test.dao.LoginUserDao.queryAll" column="{roleId=id}">

        </collection>
    </resultMap>

    <!--方式二:只需要执行一次sql查询, 主表分页查询不正确-->
    <resultMap type="com.mybatis_test.entity.LoginRole" id="RoleUserMap2">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="roleName" column="role_name" jdbcType="VARCHAR"/>
        <collection property="loginUserList" javaType="java.util.ArrayList" ofType="com.mybatis_test.entity.LoginUser">
            <id property="id" column="uid" jdbcType="VARCHAR"/>
            <result property="username" column="username" jdbcType="VARCHAR"/>
            <result property="password" column="password" jdbcType="VARCHAR"/>
            <result property="roleId" column="role_id" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>

    <!--查询某个角色的所有用户-->
    <select id="selectAllUserByRoleName" resultMap="RoleUserMap">
        select
            id, role_name
        from public.login_role
    </select>
    <!--查询某个角色的所有用户2-->
    <select id="selectAllUserByRoleName2" resultMap="RoleUserMap2">
        select
            r.id, role_name, u.id as uid, username, password, role_id
        from public.login_user u
            LEFT JOIN public.login_role r ON r.id=u.role_id
    </select>

LoginUserDao.xml代码

<resultMap type="com.mybatis_test.entity.LoginUser" id="UserRoleMap">
        <result property="id" column="id" jdbcType="VARCHAR"/>
        <result property="username" column="username" jdbcType="VARCHAR"/>
        <result property="password" column="password" jdbcType="VARCHAR"/>
        <result property="roleId" column="role_id" jdbcType="VARCHAR"/>
        <association property="loginRole" javaType="com.mybatis_test.entity.LoginRole">
            <id property="id" column="rid"/>
            <result property="roleName" column="role_name"/>
        </association>
    </resultMap>

    <!--查询所有用户详细信息-->
    <select id="queryInfoById" resultMap="UserRoleMap">
        select
            u.id id, username, password, role_id,
            r.id rid, role_name
        from public.login_user u
            LEFT JOIN public.login_role r ON u.role_id=r.id
    </select>

     <!--resultType设置为HashMap-->
    <select id="queryInfoById2" resultType="java.util.HashMap">
        select
        u.id id, username, password, role_id,
        r.id rid, role_name
        from public.login_user u
        LEFT JOIN public.login_role r ON u.role_id=r.id
    </select>

接口测试

image.png
image.png image.png
image.png

相关文章

  • mybatis联合多表查询

    数据库表 pms_user_tea表保存教师用户信息 pms_exp表保存实验室信息查询信息:所有教师下的所有实验...

  • mybatis多表联合查询

    开发环境:postgresql数据库、idea工具、easy code插件、springboot+mybatis数...

  • 5/10day51_查询&多表

    回顾 优化测试方法 mybatis查询和多表 一 Mybatis单表查询 1.1 resultMap标签 如果数据...

  • mysql---多表查询

    判断数据表中的分数是否及格: 多表联合查询---左连接 统计个数 嵌套查询: 多表查询:

  • 多表联合查询

    MySQL中基本的多表连接查询教程Mysql脚本之家 var tougao="goldensun";var lmn...

  • Mybatis多表查询

    mybatis的连接池 我们在实际开发中都会用到连接池,因为他可以减少我们获取连接消耗的时间。 mybatis提供...

  • mybatis-plus配置xml进行多表查询

    mybatis-plus多表查询,需自己写xml进行查询。 在mapper中定义,如需分页查询可添加page。 在...

  • 不可置信!SQL 优化终于干掉了“distinct”

    sql 优化之多表联合查询干掉 “distinct” 去重关键字 所以需要把多表的子查询的 sql 结构进行优化。...

  • DQL -- 多表联合查询

    现实生活中,我们需要查询的数据并不独立存在在某一张表中,此时就需要进行多表联合,进行查询操作了。多表联合主分四种:...

  • sqlalchemy多表联合查询

    sqlalchemy多表联合查询(inner outer join 左右连接)详解 按用户名摸糊查询 左外联接(l...

网友评论

    本文标题:mybatis多表联合查询

    本文链接:https://www.haomeiwen.com/subject/rfivpktx.html