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:
url: jdbc:mysql://localhost:3306/mybatis_test?useSSL=false&
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis:
type-aliases-package: com.mye.hl07mybatis.api.pojo
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
map-underscore-to-camel-case: true
lazy-loading-enabled: true
aggressive-lazy-loading: false
lazy-load-trigger-methods: ""
cache-enabled: 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;
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);
@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;
@Test
public void getUserAndIdcardInfo() {
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;
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)
@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);
@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.编写执行方法,获取用户信息和权限列表(一对多关联查询)
@Test
public void getUserAndRolesInfo() {
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的区别
|