热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

mybatis一对一、一对多查询、多对多(使用注解)

1、创建数据库表职员表:岗位信息表:2、创建对应实体类岗位实体类packagecom.hzsh.eomc.common.zhch.nyglgws

1、创建数据库表

职员表:

岗位信息表:
在这里插入图片描述

2、创建对应实体类
岗位实体类

package com.hzsh.eomc.common.zhch.nyglgwsb.entity;import java.util.List;import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.hzsh.eomc.common.zhch.ryzzwh.entity.EomcRyzzwhEntity;
import com.hzsh.eomc.common.zhch.ziyuan.entity.EomcZhchZiyuanEntity;import lombok.Data;@Data
public class EomcNyglgwsbEntity {private String id;/*** 岗位名称*/private String postName;/*** 岗位类型*/private String postType;/*** 岗位所属部门*/private String department;/*** 岗位描述*/private String description;/*** 岗位任职要求*/private String requirements;/*** 岗位对应任职人员&#xff08;一对多&#xff0c;一个岗位可能有多个任职人员&#xff09;*/private List<EomcRyzzwhEntity> ryzzwhEntityList;}

职员实体类

package com.hzsh.eomc.common.zhch.ryzzwh.entity;import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.hzsh.eomc.common.zhch.nyglgwsb.entity.EomcNyglgwsbEntity;import lombok.Data;&#64;Data
public class EomcRyzzwhEntity {/*** 任职人员编号*/private String id;/*** 人员姓名*/private String employeeName;/*** 聘任证书*/private String certificate;/*** 备案表*/private String recordForm;/*** 资质证明*/private String qualification;/*** 人员所属岗位信息*/private String postId;private EomcNyglgwsbEntity eomcNyglgwsbEntity;}

3、创建持久层

查询职员信息&#xff0c;并获取职员对应的岗位&#xff08;一对一&#xff09;

package com.hzsh.eomc.zhch.nlyys.ryzzwh.mapper;import java.util.List;import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.annotations.One;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hzsh.eomc.common.zhch.nyglgwsb.entity.EomcNyglgwsbEntity;
import com.hzsh.eomc.common.zhch.ryzzwh.entity.EomcRyzzwhEntity;&#64;Mapper
public interface EomcZhchRyzzwhMapper extends BaseMapper<EomcRyzzwhEntity>{/*** 查询所有任职人员&#xff0c;并关联岗位信息* &#64;return*/&#64;Select("select * from eomc_zhch_ryzzwh")&#64;Results(id &#61; "ryzzwhMap", value &#61; {&#64;Result(id &#61; true,column &#61; "id",property &#61; "id"),&#64;Result(column &#61; "employee_name",property &#61; "employeeName"),&#64;Result(column &#61; "certificate",property &#61; "certificate"),&#64;Result(column &#61; "record_form",property &#61; "recordForm"),&#64;Result(column &#61; "qualification",property &#61; "qualification"),&#64;Result(column &#61; "post_id",property &#61; "postId"),&#64;Result(column &#61; "post_id", property &#61; "eomcNyglgwsbEntity", one &#61; &#64;One(select &#61; "com.hzsh.eomc.zhch.nlyys.ryzzwh.mapper.EomcZhchGwsbMapper.findPostById",fetchType &#61; FetchType.EAGER))})List<EomcRyzzwhEntity> findRyzzwhAll();/*** 按条件查询任职人员&#xff0c;并关联岗位信息* &#64;param postName* &#64;param postType* &#64;param department* &#64;param employeeName* &#64;return*/&#64;Select("select a.*,b.* from eomc_zhch_ryzzwh a,eomc_zhch_nyglgwsb b where a.post_id &#61; b.id and (#{postName} &#61; &#39;&#39; or post_name like &#39;%${postName}%&#39;) "&#43; "and (#{postType} &#61; &#39;&#39; or post_type &#61; #{postType}) and (#{department} &#61; &#39;&#39; or department &#61; #{department})"&#43; " and (#{employeeName} &#61; &#39;&#39; or employee_name like &#39;%${employeeName}%&#39;) ")&#64;ResultMap(value &#61; "ryzzwhMap")List<EomcRyzzwhEntity> findRyzzwhByCondition(String postName,String postType,String department,String employeeName);&#64;Select("select * from eomc_zhch_ryzzwh where post_id &#61; #{postId} ")EomcRyzzwhEntity findRyzzzwhByPostId(String postId);}

4、测试
测试的话可以写个简单controller(省略service层),

一对多查询&#xff08;一个职位信息对应多个职员&#xff09;

&#64;Controller
&#64;RequestMapping("/hzsh/eomc-zhch/nyglgwsb")
public class EomcZhchNyglgwsbController {&#64;Autowiredprivate EomcZhchNyglgwsbMapper eomcZhchNyglgwsbMapper;/*** 查询能源管理相关的岗位信息(并关联任职人员信息)* &#64;param eomcNyglgwsbEntity* &#64;return*/&#64;ResponseBody&#64;RequestMapping("/list")public List<EomcNyglgwsbEntity> queryNyglgwsbList(&#64;RequestBody(required &#61; false) EomcNyglgwsbEntity eomcNyglgwsbEntity) {QueryWrapper<EomcNyglgwsbEntity> queryWrapper &#61; new QueryWrapper<EomcNyglgwsbEntity>();queryConditionSet(eomcNyglgwsbEntity, queryWrapper);List<EomcNyglgwsbEntity> nyglgwsbList &#61; eomcZhchNyglgwsbMapper.findPostAll();System.out.println(nyglgwsbList.get(0));for(EomcNyglgwsbEntity nyglgwsbEntity:nyglgwsbList) {System.out.println(nyglgwsbEntity);}return nyglgwsbList;}
}

查询结果如下&#xff1a;&#xff08;一个岗位对应一个或多个职员信息&#xff09;
在这里插入图片描述

一对一&#xff08;一个任职人员对应一个岗位&#xff09;

&#64;Controller
&#64;RequestMapping("/hzsh/eomc-zhch/ryzzwh")
public class EomcZhchRyzzwhController {&#64;Autowiredprivate EomcZhchRyzzwhMapper eomcZhchRyzzwhMapper;&#64;Autowiredprivate EomcZhchGwsbMapper eomcZhchGwsbMapper;/*** 查询任职人员信息&#xff08;和对应能源岗位一起显示&#xff09;&#xff0c;当前端有传参数时&#xff0c;即按条件查询&#xff0c;当无参数时即查询出全部数据* * &#64;param eomcRyzzwhEntity* &#64;return*/&#64;ResponseBody&#64;RequestMapping("/list")public List<EomcRyzzwhEntity> queryRyzzwhList(&#64;RequestBody(required &#61; false) EomcRyzzwhEntity eomcRyzzwhEntity) {String employeeName &#61; eomcRyzzwhEntity.getEmployeeName();String postName &#61; eomcRyzzwhEntity.getEomcNyglgwsbEntity().getPostName();String postType &#61; eomcRyzzwhEntity.getEomcNyglgwsbEntity().getPostType();String department &#61; eomcRyzzwhEntity.getEomcNyglgwsbEntity().getDepartment();List<EomcRyzzwhEntity> ryzzwhListByCondition &#61; eomcZhchRyzzwhMapper.findRyzzwhByCondition(postName, postType,department, employeeName);for(EomcRyzzwhEntity ryzzwhEntity:ryzzwhListByCondition) {System.out.println("**"&#43;ryzzwhEntity);}return ryzzwhListByCondition;}
}

查询结果如下&#xff1a;&#xff08;一个职员对应一个岗位信息&#xff09;
在这里插入图片描述

二、若是多对多关系&#xff0c;即一个岗位对应多个任职人员&#xff0c;一个职员也可以任职多个岗位


  1. 此时将职员实体类修改为如下&#xff1a;

package com.hzsh.eomc.common.zhch.ryzzwh.entity;import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.hzsh.eomc.common.zhch.nyglgwsb.entity.EomcNyglgwsbEntity;import lombok.Data;&#64;Data
public class EomcRyzzwhEntity {/*** 任职人员编号*/private String id;/*** 人员姓名*/private String employeeName;/*** 聘任证书*/private String certificate;/*** 备案表*/private String recordForm;/*** 资质证明*/private String qualification;/*** 人员所属岗位信息*/private String postId;private List<EomcNyglgwsbEntity> eomcNyglgwsbEntityList;

2.数据库职员表和岗位表之间需要建立一张关联表&#xff0c;用于映射支援和岗位之间的关系
在这里插入图片描述

3.持久层代码如下&#xff1a;

package com.hzsh.eomc.zhch.nlyys.nyglgwsb.mapper;import java.util.List;import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hzsh.eomc.common.zhch.nyglgwsb.entity.EomcNyglgwsbEntity;/*** * 查询岗位的接口*/
&#64;Mapper
public interface EomcZhchNyglgwsbMapper extends BaseMapper<EomcNyglgwsbEntity>{/*** 查询所有岗位信息&#xff0c;并携带该岗位所有任职人员信息* &#64;return*/&#64;Select("select * from eomc_zhch_nyglgwsb where flg_del &#61; &#39;0&#39; and (#{postName} &#61; &#39;&#39; or post_name like &#39;%${postName}%&#39;) "&#43; "and (#{postType} &#61; &#39;&#39; or post_type &#61; #{postType}) "&#43; "and (#{department} &#61; &#39;&#39; or department &#61; #{department})")&#64;Results(id &#61; "postMap" ,value &#61; {&#64;Result(id &#61; true,column &#61; "id",property &#61; "id"),&#64;Result(column &#61; "post_name",property &#61; "postName"),&#64;Result(column &#61; "post_type",property &#61; "postType"),&#64;Result(column &#61; "department",property &#61; "department"),&#64;Result(column &#61; "description",property &#61; "description"),&#64;Result(column &#61; "requirements",property &#61; "requirements"),&#64;Result(column &#61; "id",property &#61; "ryzzwhEntityList", many &#61; &#64;Many (select &#61; "com.hzsh.eomc.zhch.nlyys.ryzzwh.mapper.EomcZhchRyzzwhMapper.findEmployeeByPostId")) })List<EomcNyglgwsbEntity> findPost(String postName,String postType,String department);/*** 查询所有岗位信息&#xff0c;并携带该岗位所有任职人员信息* &#64;return*/&#64;Select("select a.* from eomc_zhch_nyglgwsb a,(select * from eomc_zhch_postAndEmployConnect where flg_del &#61; &#39;0&#39; and employee_id&#61;#{employeeId}) b "&#43; "where a.id &#61; b.post_id")List<EomcNyglgwsbEntity> findPostByEmployeeId(String employeeId);

package com.hzsh.eomc.zhch.nlyys.ryzzwh.mapper;import java.util.List;import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.annotations.One;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hzsh.eomc.common.zhch.nyglgwsb.entity.EomcNyglgwsbEntity;
import com.hzsh.eomc.common.zhch.ryzzwh.entity.EomcRyzzwhEntity;
/*** 查询职员的接口* */
&#64;Mapper
public interface EomcZhchRyzzwhMapper extends BaseMapper<EomcRyzzwhEntity> {/*** 查询所有职员&#xff0c;并携带其所任职的一个或多个岗位信息* &#64;return*/&#64;Select("select * from eomc_zhch_ryzzwh where flg_del &#61; &#39;0&#39; and (#{employeeName} &#61; &#39;&#39; or employee_name &#61; #{employeeName})"&#43; "and (#{department} &#61; &#39;&#39; or department &#61; #{department})")&#64;Results(id &#61; "ryzzwhMap", value &#61; { &#64;Result(id &#61; true, column &#61; "id", property &#61; "id"),&#64;Result(column &#61; "employee_name", property &#61; "employeeName"),&#64;Result(column &#61; "department", property &#61; "department"),&#64;Result(column &#61; "certificate", property &#61; "certificate"),&#64;Result(column &#61; "record_form", property &#61; "recordForm"),&#64;Result(column &#61; "qualification", property &#61; "qualification"),&#64;Result(column &#61; "id", property &#61; "eomcNyglgwsbEntityList", many &#61; &#64;Many(select &#61; "com.hzsh.eomc.zhch.nlyys.nyglgwsb.mapper.EomcZhchNyglgwsbMapper.findPostByEmployeeId", fetchType &#61; FetchType.EAGER)) })List<EomcRyzzwhEntity> findRyzzwh(String employeeName, String department);/*** 根据岗位id查询出任职人员信息* &#64;return*/&#64;Select("select c.* from eomc_zhch_ryzzwh c,(select * from eomc_zhch_postAndEmployConnect where flg_del &#61; &#39;0&#39; and post_id&#61;#{postId}) b "&#43; "where c.id &#61; b.employee_id")List<EomcRyzzwhEntity> findEmployeeByPostId(String postId);}

推荐阅读
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Java String与StringBuffer的区别及其应用场景
    本文主要介绍了Java中String和StringBuffer的区别,String是不可变的,而StringBuffer是可变的。StringBuffer在进行字符串处理时不生成新的对象,内存使用上要优于String类。因此,在需要频繁对字符串进行修改的情况下,使用StringBuffer更加适合。同时,文章还介绍了String和StringBuffer的应用场景。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • 开发笔记:Java是如何读取和写入浏览器Cookies的
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Java是如何读取和写入浏览器Cookies的相关的知识,希望对你有一定的参考价值。首先我 ... [详细]
  • 在springmvc框架中,前台ajax调用方法,对图片批量下载,如何弹出提示保存位置选框?Controller方法 ... [详细]
  • Java自带的观察者模式及实现方法详解
    本文介绍了Java自带的观察者模式,包括Observer和Observable对象的定义和使用方法。通过添加观察者和设置内部标志位,当被观察者中的事件发生变化时,通知观察者对象并执行相应的操作。实现观察者模式非常简单,只需继承Observable类和实现Observer接口即可。详情请参考Java官方api文档。 ... [详细]
  • 数组的排序:数组本身有Arrays类中的sort()方法,这里写几种常见的排序方法。(1)冒泡排序法publicstaticvoidmain(String[]args ... [详细]
  • 面向对象之3:封装的总结及实现方法
    本文总结了面向对象中封装的概念和好处,以及在Java中如何实现封装。封装是将过程和数据用一个外壳隐藏起来,只能通过提供的接口进行访问。适当的封装可以提高程序的理解性和维护性,增强程序的安全性。在Java中,封装可以通过将属性私有化并使用权限修饰符来实现,同时可以通过方法来访问属性并加入限制条件。 ... [详细]
  • (三)多表代码生成的实现方法
    本文介绍了一种实现多表代码生成的方法,使用了java代码和org.jeecg框架中的相关类和接口。通过设置主表配置,可以生成父子表的数据模型。 ... [详细]
  • 安卓select模态框样式改变_微软Office风格的多端(Web、安卓、iOS)组件库——Fabric UI...
    介绍FabricUI是微软开源的一套Office风格的多端组件库,共有三套针对性的组件,分别适用于web、android以及iOS,Fab ... [详细]
author-avatar
东莞家装_670
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有