mybatis之(经典的一对多查询:角色资源查询)创建项目 mybatis07,使用嵌套查询完成用户角色资源的查询

mybatis之(经典的一对多查询:角色资源查询)创建项目 mybatis07,使用嵌套查询完成用户角色资源的查询,第1张

mybatis之(经典的一对多查询:角色资源查询)创建项目 mybatis07,使用嵌套查询完成用户角色资源的查询

mybatis之创建项目 mybatis07,使用嵌套查询完成用户角色资源的查询
    • 一、实体类代码:
      • ①Resource实体类:
      • ②Role实体类:
      • ③User实体类:
    • 二、实体对应mapper文件夹下的接口代码:
      • ①UserDaoMapper代码:
      • ②RoleDaoMapper代码:
      • ③ResourceDaoMapper代码:
    • 三、资源表resources下的mapper文件下的xml配置文件
      • ①userDaoMapper.xml代码:
      • ②roleDaoMapper.xml代码:
      • ③resourceDaoMapper.xml代码:
    • 四、mybatis数据库配置文件
      • mybatis-conf.xml代码:
    • 五、test里面AppTest测试
      • AppTest代码:
    • 六、mybatis07的pom.xml配置文件
      • mybatis07的pom.xml代码:
    • 七、父模块mybatis里面pom.xml配置文件
      • mybatis的pom.xml文件:
    • 八、测试结果如下:
      • ①查询单个角色时将其角色下的所有资源全部查询出来。
      • ②查询单个用户时将其所属的角色以及角色拥有的资源全部查询出来。

在上面项目 mybatis07 中,使用嵌套查询完成用户角色资源的查询。完成如下功能:数据准备

  一、查询单个角色时将其角色下的所有资源全部查询出来。
  二、查询单个用户时将其所属的角色以及角色拥有的资源全部查询出来。

参考代码

一、实体类代码: ①Resource实体类:
package org.example.entity;


import java.util.Date;

public class Resource {

  private Integer id;
  private String name;
  private String url;
  private Integer pid;
  private Date createTime;
  private Date updateTime;
  private Integer orderNumber;

  @Override
  public String toString() {
    return "nResource{" +
            "id=" + id +
            ", name='" + name + ''' +
            ", url='" + url + ''' +
            ", pid=" + pid +
            ", createTime=" + createTime +
            ", updateTime=" + updateTime +
            ", orderNumber=" + orderNumber +
            '}';
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getUrl() {
    return url;
  }

  public void setUrl(String url) {
    this.url = url;
  }

  public Integer getPid() {
    return pid;
  }

  public void setPid(Integer pid) {
    this.pid = pid;
  }

  public Date getCreateTime() {
    return createTime;
  }

  public void setCreateTime(Date createTime) {
    this.createTime = createTime;
  }

  public Date getUpdateTime() {
    return updateTime;
  }

  public void setUpdateTime(Date updateTime) {
    this.updateTime = updateTime;
  }

  public Integer getOrderNumber() {
    return orderNumber;
  }

  public void setOrderNumber(Integer orderNumber) {
    this.orderNumber = orderNumber;
  }
}

②Role实体类:
package org.example.entity;


import java.util.Date;
import java.util.List;

public class Role {

  private Integer id;
  private String name;
  private String remark;
  private Date createTime;
  private Date updateTime;
  private List resource;

  @Override
  public String toString() {
    return "Role{" +
            "id=" + id +
            ", name='" + name + ''' +
            ", remark='" + remark + ''' +
            ", createTime=" + createTime +
            ", updateTime=" + updateTime +
            ", resource=" + resource +
            '}';
  }

  public List getResource() {
    return resource;
  }

  public void setResource(List resource) {
    this.resource = resource;
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getRemark() {
    return remark;
  }

  public void setRemark(String remark) {
    this.remark = remark;
  }

  public Date getCreateTime() {
    return createTime;
  }

  public void setCreateTime(Date createTime) {
    this.createTime = createTime;
  }

  public Date getUpdateTime() {
    return updateTime;
  }

  public void setUpdateTime(Date updateTime) {
    this.updateTime = updateTime;
  }
}

③User实体类:
package org.example.entity;


import java.util.Date;

public class User {

  private Integer id;
  private String name;
  private String loginName;
  private String loginPassword;
  private Integer roleId;
  private Date createTime;
  private Date updateTime;
  private Role role;

  public Integer getRoleId() {
    return roleId;
  }

  public void setRoleId(Integer roleId) {
    this.roleId = roleId;
  }

  @Override
  public String toString() {
    return "User{" +
            "id=" + id +
            ", name='" + name + ''' +
            ", loginName='" + loginName + ''' +
            ", loginPassword='" + loginPassword + ''' +
            ", roleId=" + roleId +
            ", createTime=" + createTime +
            ", updateTime=" + updateTime +
            ", role=" + role +
            '}';
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getLoginName() {
    return loginName;
  }

  public void setLoginName(String loginName) {
    this.loginName = loginName;
  }

  public String getLoginPassword() {
    return loginPassword;
  }

  public void setLoginPassword(String loginPassword) {
    this.loginPassword = loginPassword;
  }

  public Date getCreateTime() {
    return createTime;
  }

  public void setCreateTime(Date createTime) {
    this.createTime = createTime;
  }

  public Date getUpdateTime() {
    return updateTime;
  }

  public void setUpdateTime(Date updateTime) {
    this.updateTime = updateTime;
  }

  public Role getRole() {
    return role;
  }

  public void setRole(Role role) {
    this.role = role;
  }
}

二、实体对应mapper文件夹下的接口代码: ①UserDaoMapper代码:
package org.example.mapper;

import org.example.entity.User;

public interface UserDaoMapper {
    User queryById1(int id);
}

②RoleDaoMapper代码:
package org.example.mapper;

import org.example.entity.Role;

public interface RoleDaoMapper {
    Role roleQueryById(int id);
}

③ResourceDaoMapper代码:
package org.example.mapper;

import org.example.entity.Resource;

import java.util.List;

public interface ResourceDaoMapper {
    List queryByRoleId();
}

三、资源表resources下的mapper文件下的xml配置文件 ①userDaoMapper.xml代码:




    
        
        
        
        
        
        
        
        
        
    
    
        select id, name, remark, create_time, update_time from role where id=#{id}
    

③resourceDaoMapper.xml代码:



    
        
        
        
        
        
        
        
    
    

四、mybatis数据库配置文件 mybatis-conf.xml代码:



    
        
            
            
                
                
                
                
            
        
    
    
        
        
        
    

五、test里面AppTest测试 AppTest代码:
package org.example;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.entity.Role;
import org.example.entity.User;
import org.example.mapper.RoleDaoMapper;
import org.example.mapper.UserDaoMapper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;

public class AppTest {
    SqlSession session;

    @Before
    public void init() throws IOException {
        Reader reader = Resources.getResourceAsReader("mybatis-conf.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        session = sessionFactory.openSession(true);
    }

    @Test
    public void queryRoleById() {
        RoleDaoMapper mapper = session.getMapper(RoleDaoMapper.class);
        Role role = mapper.roleQueryById(2);
        System.out.println(role);
    }

    @Test
    public void queryUserById() {
        UserDaoMapper mapper = session.getMapper(UserDaoMapper.class);
        User user = mapper.queryById1(87);
        System.out.println(user);
    }

    @After
    public void close() {
        session.close();
    }
}

六、mybatis07的pom.xml配置文件 mybatis07的pom.xml代码:



    
        mybatis
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    mybatis07

    mybatis07
    
    http://www.example.com

    
        UTF-8
        1.8
        1.8
    

    
        
            junit
            junit
            4.11
            test
        
    

    
        
            
                
                
                    maven-clean-plugin
                    3.1.0
                
                
                
                    maven-resources-plugin
                    3.0.2
                
                
                    maven-compiler-plugin
                    3.8.0
                
                
                    maven-surefire-plugin
                    2.22.1
                
                
                    maven-jar-plugin
                    3.0.2
                
                
                    maven-install-plugin
                    2.5.2
                
                
                    maven-deploy-plugin
                    2.8.2
                
                
                
                    maven-site-plugin
                    3.7.1
                
                
                    maven-project-info-reports-plugin
                    3.0.0
                
            
        
    


七、父模块mybatis里面pom.xml配置文件 mybatis的pom.xml文件:


    4.0.0
    org.example
    mybatis
    1.0-SNAPSHOT
    
        mybatis01
        fruit01
        mybatis02
        mybatis04
        mybatis05
        pet01
        fruit02
        fruit03
        mybatis06
        fruit04
        mybatis07
        mybatis08
        mybatis09
    
    pom
    
        
            org.mybatis
            mybatis
            3.4.6
        
        
            mysql
            mysql-connector-java
            5.1.48
        
        
            ch.qos.logback
            logback-classic
            1.2.6
        
    

八、测试结果如下: ①查询单个角色时将其角色下的所有资源全部查询出来。


②查询单个用户时将其所属的角色以及角色拥有的资源全部查询出来。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5686562.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存