mybatis之创建项目 mybatis07,使用嵌套查询完成用户角色资源的查询
在上面项目 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> resource;
@Override
public String toString() {
return "Role{" +
"id=" + id +
", name='" + name + '\'' +
", remark='" + remark + '\'' +
", createTime=" + createTime +
", updateTime=" + updateTime +
", resource=" + resource +
'}';
}
public List<Resource> getResource() {
return resource;
}
public void setResource(List<Resource> 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<Resource> queryByRoleId();
}
三、资源表resources下的mapper文件下的xml配置文件
①userDaoMapper.xml代码:
<?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="org.example.mapper.UserDaoMapper">
<!--嵌套查询-->
<resultMap id="userResultMap1" type="org.example.entity.User">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="login_name" property="loginName"/>
<result column="login_password" property="loginPassword"/>
<result column="role_id" property="roleId"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
<association property="role" column="role_id" select="org.example.mapper.RoleDaoMapper.roleQueryById">
</association>
</resultMap>
<select id="queryById1" resultMap="userResultMap1">
select id, name, login_name, login_password, role_id, create_time, update_time from user where id=#{id}
</select>
</mapper>
②roleDaoMapper.xml代码:
<?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="org.example.mapper.RoleDaoMapper">
<resultMap id="RoleMap" type="org.example.entity.Role">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="remark" property="remark" jdbcType="VARCHAR"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
<collection property="resource" column="id" ofType="org.example.entity.Resource"
select="org.example.mapper.ResourceDaoMapper.queryByRoleId"></collection>
</resultMap>
<select id="roleQueryById" resultType="org.example.entity.Role" resultMap="RoleMap">
select id, name, remark, create_time, update_time from role where id=#{id}
</select>
</mapper>
③resourceDaoMapper.xml代码:
<?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="org.example.mapper.ResourceDaoMapper">
<resultMap id="ResourceMap" type="org.example.entity.Resource">
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="url" property="url" jdbcType="VARCHAR"/>
<result column="pid" property="pid" jdbcType="INTEGER"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
<result column="order_number" property="orderNumber" jdbcType="INTEGER"/>
</resultMap>
<select id="queryByRoleId" resultMap="ResourceMap">
select id,name,url,pid,create_time,update_time,order_number from resource re left join role_resource rr on re.id=rr.resource_id where rr.role_id=#{id}
</select>
</mapper>
四、mybatis数据库配置文件
mybatis-conf.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/userDaoMapper.xml"></mapper>
<mapper resource="mapper/roleDaoMapper.xml"></mapper>
<mapper resource="mapper/resourceDaoMapper.xml"></mapper>
</mappers>
</configuration>
五、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代码:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mybatis</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mybatis07</artifactId>
<name>mybatis07</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
七、父模块mybatis里面pom.xml配置文件
mybatis的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>mybatis01</module>
<module>fruit01</module>
<module>mybatis02</module>
<module>mybatis04</module>
<module>mybatis05</module>
<module>pet01</module>
<module>fruit02</module>
<module>fruit03</module>
<module>mybatis06</module>
<module>fruit04</module>
<module>mybatis07</module>
<module>mybatis08</module>
<module>mybatis09</module>
</modules>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.6</version>
</dependency>
</dependencies>
</project>
八、测试结果如下:
①查询单个角色时将其角色下的所有资源全部查询出来。
②查询单个用户时将其所属的角色以及角色拥有的资源全部查询出来。