Mybatis使用@one和@Many实现一对一及一对多关联查询

一、准备工作

1.创建springboot项目,项目结构如下

在这里插入图片描述

2.添加pom.xml配置信息

<dependencies>

<dependency>

<groupId>org.mybatis</groupId>

<artifactId>mybatis</artifactId>

<version>3.4.2</version>

</dependency>

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.3.0</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.34</version>

</dependency>

</dependencies>

3.配置相关信息

将默认的application.properties文件的后缀修改为“.yml”,即配置文件名称为:application.yml,并配置以下信息:

spring:

#DataSource数据源

datasource:

url: jdbc:mysql://localhost:3306/mybatis_test?useSSL=false&amp

username: root

password: root

driver-class-name: com.mysql.jdbc.Driver

#MyBatis配置

mybatis:

type-aliases-package: com.mye.hl07mybatis.api.pojo #别名定义

configuration:

log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #指定 MyBatis 所用日志的具体实现,未指定时将自动查找

map-underscore-to-camel-case: true #开启自动驼峰命名规则(camel case)映射

lazy-loading-enabled: true #开启延时加载开关

aggressive-lazy-loading: false #将积极加载改为消极加载(即按需加载),默认值就是false

lazy-load-trigger-methods: "" #阻挡不相干的操作触发,实现懒加载

cache-enabled: true #打开全局缓存开关(二级环境),默认值就是true

二、使用@One注解实现一对一关联查询

需求:获取用户信息,同时获取一对多关联的权限列表

1.在MySQL数据库中创建用户信息表(tb_user)

-- 判断数据表是否存在,存在则删除

DROP TABLE IF EXISTS tb_user;

-- 创建“用户信息”数据表

CREATE TABLE IF NOT EXISTS tb_user

(

user_id INT AUTO_INCREMENT PRIMARY KEY COMMENT '用户编号',

user_account VARCHAR(50) NOT NULL COMMENT '用户账号',

user_password VARCHAR(50) NOT NULL COMMENT '用户密码',

blog_url VARCHAR(50) NOT NULL COMMENT '博客地址',

remark VARCHAR(50) COMMENT '备注'

) COMMENT = '用户信息表';

-- 添加数据

INSERT INTO tb_user(user_account,user_password,blog_url,remark) VALUES('拒绝熬夜啊的博客','123456','https://blog.csdn.net/weixin_43296313/','您好,欢迎访问拒绝熬夜啊的博客');

2.在MySQL数据库中创建身份证信息表(tb_idcard)

-- 判断数据表是否存在,存在则删除

DROP TABLE IF EXISTS tb_idcard;

-- 创建“身份证信息”数据表

CREATE TABLE IF NOT EXISTS tb_idcard

(

id INT AUTO_INCREMENT PRIMARY KEY COMMENT '身份证ID',

user_id INT NOT NULL COMMENT '用户编号',

idCard_code VARCHAR(45) COMMENT '身份证号码'

) COMMENT = '身份证信息表';

-- 添加数据

INSERT INTO tb_idcard(user_id,idCard_code) VALUE(1,'123456789');

3.创建用户信息持久化类(UserInfo.java)

@Data

@AllArgsConstructor

@NoArgsConstructor

public class UserInfo {

private int userId; //用户编号

private String userAccount; //用户账号

private String userPassword; //用户密码

private String blogUrl; //博客地址

private String remark; //备注

private IdcardInfo idcardInfo; //身份证信息

}

4.创建身份证信息持久化类(IdcardInfo.java)

@Data

@AllArgsConstructor

@NoArgsConstructor

public class IdcardInfo {

public int id; //身份证ID

public int userId; //用户编号

public String idCardCode; //身份证号码

}

5.创建UserMapper接口(用户信息Mapper动态代理接口)

@Repository

@Mapper

public interface UserMapper {

/**

* 获取用户信息和身份证信息

* 一对一关联查询

*/

@Select("SELECT * FROM tb_user WHERE user_id = #{userId}")

@Results(id = "userAndIdcardResultMap", value = {

@Result(property = "userId", column = "user_id", javaType = Integer.class, jdbcType = JdbcType.INTEGER, id = true),

@Result(property = "userAccount", column = "user_account",javaType = String.class, jdbcType = JdbcType.VARCHAR),

@Result(property = "userPassword", column = "user_password",javaType = String.class, jdbcType = JdbcType.VARCHAR),

@Result(property = "blogUrl", column = "blog_url",javaType = String.class, jdbcType = JdbcType.VARCHAR),

@Result(property = "remark", column = "remark",javaType = String.class, jdbcType = JdbcType.VARCHAR),

@Result(property = "idcardInfo",column = "user_id",

one = @One(select = "com.mye.hl07mybatis.api.mapper.UserMapper.getIdcardInfo", fetchType = FetchType.LAZY))

})

UserInfo getUserAndIdcardInfo(@Param("userId")int userId);

/**

* 根据用户ID,获取身份证信息

*/

@Select("SELECT * FROM tb_idcard WHERE user_id = #{userId}")

@Results(id = "idcardInfoResultMap", value = {

@Result(property = "id", column = "id"),

@Result(property = "userId", column = "user_id"),

@Result(property = "idCardCode", column = "idCard_code")})

IdcardInfo getIdcardInfo(@Param("userId")int userId);

}

6.实现实体类和数据表的映射关系

在SpringBoot启动类中加 @MapperScan(basePackages = “com.mye.hl07mybatis.api.mapper”) 注解。

@SpringBootApplication

@MapperScan(basePackages = "com.mye.hl07mybatis.api.mapper")

public class Hl07MybatisApplication {

public static void main(String[] args) {

SpringApplication.run(Hl07MybatisApplication.class, args);

}

}

7.编写执行方法,获取用户信息和身份证信息(一对一关联查询)

@SpringBootTest(classes = Hl07MybatisApplication.class)

@RunWith(SpringRunner.class)

public class Hl07MybatisApplicationTests {

@Autowired

private UserMapper userMapper;

/**

* 获取用户信息和身份证信息

* 一对一关联查询

* @author pan_junbiao

*/

@Test

public void getUserAndIdcardInfo() {

//执行Mapper代理对象的查询方法

UserInfo userInfo = userMapper.getUserAndIdcardInfo(1);

//打印结果

if(userInfo!=null) {

System.out.println("用户编号:" + userInfo.getUserId());

System.out.println("用户账号:" + userInfo.getUserAccount());

System.out.println("用户密码:" + userInfo.getUserPassword());

System.out.println("博客地址:" + userInfo.getBlogUrl());

System.out.println("备注信息:" + userInfo.getRemark());

System.out.println("-----------------------------------------");

//获取身份证信息

IdcardInfo idcardInfo = userInfo.getIdcardInfo();

if(idcardInfo!=null) {

System.out.println("身份证ID:" + idcardInfo.getId());

System.out.println("用户编号:" + idcardInfo.getUserId());

System.out.println("身份证号码:" + idcardInfo.getIdCardCode());

}

}

}

}

执行结果:

在这里插入图片描述

三、使用@Many注解实现一对多关联查询

需求:获取用户信息,同时获取一对多关联的权限列表

1.在MySQL数据库创建权限信息表(tb_role)

-- 判断数据表是否存在,存在则删除

DROP TABLE IF EXISTS tb_role;

-- 创建“权限信息”数据表

CREATE TABLE IF NOT EXISTS tb_role

(

id INT AUTO_INCREMENT PRIMARY KEY COMMENT '权限ID',

user_id INT NOT NULL COMMENT '用户编号',

role_name VARCHAR(50) NOT NULL COMMENT '权限名称'

) COMMENT = '权限信息表';

INSERT INTO tb_role(user_id,role_name) VALUES(1,'系统管理员'),(1,'新闻管理员'),(1,'广告管理员');

2.创建权限信息持久化类(RoleInfo.java)

@Data

@AllArgsConstructor

@NoArgsConstructor

public class RoleInfo {

private int id; //权限ID

private int userId; //用户编号

private String roleName; //权限名称

}

3.修改用户信息持久化类(UserInfo.java),添加权限列表的属性字段

@Data

@AllArgsConstructor

@NoArgsConstructor

public class UserInfo {

private int userId; //用户编号

private String userAccount; //用户账号

private String userPassword; //用户密码

private String blogUrl; //博客地址

private String remark; //备注

private IdcardInfo idcardInfo; //身份证信息

private List<RoleInfo> roleInfoList; //权限列表

}

4.编写用户信息Mapper动态代理接口(UserMapper.java)

/**

* 获取用户信息和权限列表

* 一对多关联查询

* @author pan_junbiao

*/

@Select("SELECT * FROM tb_user WHERE user_id = #{userId}")

@Results(id = "userAndRolesResultMap", value = {

@Result(property = "userId", column = "user_id", javaType = Integer.class, jdbcType = JdbcType.INTEGER, id = true),

@Result(property = "userAccount", column = "user_account",javaType = String.class, jdbcType = JdbcType.VARCHAR),

@Result(property = "userPassword", column = "user_password",javaType = String.class, jdbcType = JdbcType.VARCHAR),

@Result(property = "blogUrl", column = "blog_url",javaType = String.class, jdbcType = JdbcType.VARCHAR),

@Result(property = "remark", column = "remark",javaType = String.class, jdbcType = JdbcType.VARCHAR),

@Result(property = "roleInfoList",column = "user_id", many = @Many(select = "com.pjb.mapper.UserMapper.getRoleList", fetchType = FetchType.LAZY))

})

public UserInfo getUserAndRolesInfo(@Param("userId")int userId);

/**

* 根据用户ID,获取权限列表

* @author pan_junbiao

*/

@Select("SELECT * FROM tb_role WHERE user_id = #{userId}")

@Results(id = "roleInfoResultMap", value = {

@Result(property = "id", column = "id"),

@Result(property = "userId", column = "user_id"),

@Result(property = "roleName", column = "role_name")})

public List<RoleInfo> getRoleList(@Param("userId")int userId);

5.编写执行方法,获取用户信息和权限列表(一对多关联查询)

/**

* 获取用户信息和权限列表

* 一对多关联查询

* @author pan_junbiao

*/

@Test

public void getUserAndRolesInfo() {

//执行Mapper代理对象的查询方法

UserInfo userInfo = userMapper.getUserAndRolesInfo(1);

//打印结果

if(userInfo!=null) {

System.out.println("用户编号:" + userInfo.getUserId());

System.out.println("用户账号:" + userInfo.getUserAccount());

System.out.println("用户密码:" + userInfo.getUserPassword());

System.out.println("博客地址:" + userInfo.getBlogUrl());

System.out.println("备注信息:" + userInfo.getRemark());

System.out.println("-----------------------------------------");

//获取权限列表

List<RoleInfo> roleInfoList = userInfo.getRoleInfoList();

if(roleInfoList!=null && roleInfoList.size()>0) {

System.out.println("用户拥有的权限:");

for (RoleInfo roleInfo : roleInfoList) {

System.out.println(roleInfo.getRoleName());

}

}

}

}

执行结果:

在这里插入图片描述

四、FetchType.LAZY 和 FetchType.EAGER的区别

FetchType.LAZY:懒加载,加载一个实体时,定义懒加载的属性不会马上从数据库中加载。

FetchType.EAGER:急加载,加载一个实体时,定义急加载的属性会立即从数据库中加载。

到此这篇关于Mybatis使用@one和@Many实现一对一及一对多关联查询的文章就介绍到这了,更多相关Mybatis 一对一及一对多关联查询内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

以上是 Mybatis使用@one和@Many实现一对一及一对多关联查询 的全部内容, 来源链接: utcz.com/p/249173.html

回到顶部