美文网首页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多表联合查询

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