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

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;
	/**
	 * 岗位对应任职人员(一对多,一个岗位可能有多个任职人员)
	 */
	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;

@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、创建持久层

查询职员信息,并获取职员对应的岗位(一对一)

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;

@Mapper
public interface EomcZhchRyzzwhMapper extends BaseMapper<EomcRyzzwhEntity>{

	/**
	 * 查询所有任职人员,并关联岗位信息
	 * @return
	 */
	@Select("select * from eomc_zhch_ryzzwh")
	@Results(id = "ryzzwhMap", value = {
			@Result(id = true,column = "id",property = "id"),
			@Result(column = "employee_name",property = "employeeName"),
			@Result(column = "certificate",property = "certificate"),
			@Result(column = "record_form",property = "recordForm"),
			@Result(column = "qualification",property = "qualification"),
			@Result(column = "post_id",property = "postId"),
			@Result(column = "post_id", property = "eomcNyglgwsbEntity", one = @One(select = "com.hzsh.eomc.zhch.nlyys.ryzzwh.mapper.EomcZhchGwsbMapper.findPostById",fetchType = FetchType.EAGER))
			})
    List<EomcRyzzwhEntity> findRyzzwhAll();
    
	/**
	 * 按条件查询任职人员,并关联岗位信息
	 * @param postName
	 * @param postType
	 * @param department
	 * @param employeeName
	 * @return
	 */
	@Select("select a.*,b.* from eomc_zhch_ryzzwh a,eomc_zhch_nyglgwsb b where a.post_id = b.id and (#{postName} = '' or post_name like '%${postName}%') "
			+ "and (#{postType} = '' or post_type = #{postType}) and (#{department} = '' or department = #{department})"
			+ " and (#{employeeName} = '' or employee_name like '%${employeeName}%') ")
	@ResultMap(value = "ryzzwhMap")
    List<EomcRyzzwhEntity> findRyzzwhByCondition(String postName,String postType,String department,String employeeName);
	
	@Select("select * from eomc_zhch_ryzzwh where post_id = #{postId} ")
	 EomcRyzzwhEntity findRyzzzwhByPostId(String postId);


}


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

一对多查询(一个职位信息对应多个职员)

@Controller
@RequestMapping("/hzsh/eomc-zhch/nyglgwsb")
public class EomcZhchNyglgwsbController {

	@Autowired
	private EomcZhchNyglgwsbMapper eomcZhchNyglgwsbMapper;
    /**
     * 查询能源管理相关的岗位信息(并关联任职人员信息)
     * @param eomcNyglgwsbEntity
     * @return
     */
	@ResponseBody
	@RequestMapping("/list")
	public List<EomcNyglgwsbEntity> queryNyglgwsbList(@RequestBody(required = false) EomcNyglgwsbEntity eomcNyglgwsbEntity) {
			
		QueryWrapper<EomcNyglgwsbEntity> queryWrapper = new QueryWrapper<EomcNyglgwsbEntity>();
		queryConditionSet(eomcNyglgwsbEntity, queryWrapper);
		List<EomcNyglgwsbEntity> nyglgwsbList = eomcZhchNyglgwsbMapper.findPostAll();
		System.out.println(nyglgwsbList.get(0));
		for(EomcNyglgwsbEntity nyglgwsbEntity:nyglgwsbList) {
			System.out.println(nyglgwsbEntity);
		}
		return nyglgwsbList;
	}
}

查询结果如下:(一个岗位对应一个或多个职员信息)
在这里插入图片描述

一对一(一个任职人员对应一个岗位)

@Controller
@RequestMapping("/hzsh/eomc-zhch/ryzzwh")
public class EomcZhchRyzzwhController {

	@Autowired
	private EomcZhchRyzzwhMapper eomcZhchRyzzwhMapper;
	@Autowired
	private EomcZhchGwsbMapper eomcZhchGwsbMapper;

	/**
	 * 查询任职人员信息(和对应能源岗位一起显示),当前端有传参数时,即按条件查询,当无参数时即查询出全部数据
	 * 
	 * @param eomcRyzzwhEntity
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/list")
	public List<EomcRyzzwhEntity> queryRyzzwhList(@RequestBody(required = false) EomcRyzzwhEntity eomcRyzzwhEntity) {

		String employeeName = eomcRyzzwhEntity.getEmployeeName();
		String postName = eomcRyzzwhEntity.getEomcNyglgwsbEntity().getPostName();
		String postType = eomcRyzzwhEntity.getEomcNyglgwsbEntity().getPostType();
		String department = eomcRyzzwhEntity.getEomcNyglgwsbEntity().getDepartment();

		List<EomcRyzzwhEntity> ryzzwhListByCondition = eomcZhchRyzzwhMapper.findRyzzwhByCondition(postName, postType,
				department, employeeName);
   for(EomcRyzzwhEntity ryzzwhEntity:ryzzwhListByCondition) {
        	System.out.println("**"+ryzzwhEntity);
        }
		return ryzzwhListByCondition;
	}
}


查询结果如下:(一个职员对应一个岗位信息)
在这里插入图片描述

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

  1. 此时将职员实体类修改为如下:
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;

@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.数据库职员表和岗位表之间需要建立一张关联表,用于映射支援和岗位之间的关系
在这里插入图片描述

3.持久层代码如下:

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;
  /**
    * 
    * 查询岗位的接口
    */
@Mapper
public interface EomcZhchNyglgwsbMapper extends BaseMapper<EomcNyglgwsbEntity>{
   /**
    * 查询所有岗位信息,并携带该岗位所有任职人员信息
    * @return
    */
	  @Select("select * from eomc_zhch_nyglgwsb  where flg_del = '0' and (#{postName} = '' or post_name like '%${postName}%') "
	  		+ "and (#{postType} = '' or post_type = #{postType}) "
	  		+ "and (#{department} = '' or department = #{department})")
	  @Results(id = "postMap" ,value = {
				@Result(id = true,column = "id",property = "id"),
				@Result(column = "post_name",property = "postName"),
				@Result(column = "post_type",property = "postType"),
				@Result(column = "department",property = "department"),
				@Result(column = "description",property = "description"),
				@Result(column = "requirements",property = "requirements"),
				@Result(column = "id",property = "ryzzwhEntityList", many = @Many (select = "com.hzsh.eomc.zhch.nlyys.ryzzwh.mapper.EomcZhchRyzzwhMapper.findEmployeeByPostId"))	  
	  })
	  List<EomcNyglgwsbEntity> findPost(String postName,String postType,String department);
		
 /**
    * 查询所有岗位信息,并携带该岗位所有任职人员信息
    * @return
    */
		@Select("select a.* from eomc_zhch_nyglgwsb a,(select *  from eomc_zhch_postAndEmployConnect where flg_del = '0' and employee_id=#{employeeId}) b "
				+ "where a.id = 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;
/**
	 * 查询职员的接口
	 * 
	 */
@Mapper
public interface EomcZhchRyzzwhMapper extends BaseMapper<EomcRyzzwhEntity> {

	/**
	 * 查询所有职员,并携带其所任职的一个或多个岗位信息
	 * @return
	 */
	@Select("select * from eomc_zhch_ryzzwh where flg_del = '0' and (#{employeeName} = '' or employee_name = #{employeeName})"
			+ "and (#{department} = '' or department = #{department})")
	@Results(id = "ryzzwhMap", value = { @Result(id = true, column = "id", property = "id"),
			@Result(column = "employee_name", property = "employeeName"),
			@Result(column = "department", property = "department"),
			@Result(column = "certificate", property = "certificate"),
			@Result(column = "record_form", property = "recordForm"),
			@Result(column = "qualification", property = "qualification"),
			@Result(column = "id", property = "eomcNyglgwsbEntityList", many = @Many(select = "com.hzsh.eomc.zhch.nlyys.nyglgwsb.mapper.EomcZhchNyglgwsbMapper.findPostByEmployeeId", fetchType = FetchType.EAGER)) })
	List<EomcRyzzwhEntity> findRyzzwh(String employeeName, String department);

/**
	 * 根据岗位id查询出任职人员信息
	 * @return
	 */
	@Select("select c.* from eomc_zhch_ryzzwh c,(select *  from eomc_zhch_postAndEmployConnect where flg_del = '0' and post_id=#{postId}) b "
			+ "where c.id = b.employee_id")
	List<EomcRyzzwhEntity> findEmployeeByPostId(String postId);
	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值