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& 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 一对一及一对多关联查询内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!