mybatis关联查询(个人笔记)

一对一(association)

pojo实体类:

public class Employee implements Serializable {
	private static final long serialVersionUID = 1L;
	private Integer empId;
	private String name;
	private Integer gender;
	private Date birthday;
	private String mobile;
	private String email;
	private String position;
	private String note;
	private WorkCard workcard;
...
}

接口:

public Employee selectEmployeeByid(int id);

mapper映射文件:

<!-- 一对一关联查询 -->
<select id="selectEmployeeByid" parameterType="integer" 
resultMap="employeeByid">
	select employee.*,workcard.realname,workcard.address from employee,workcard where employee.empid=workcard.empid and employee.empid=#{empid};
</select>

<resultMap type="employee" id="employeeByid">
<id property="empId" column="empid"/>
<result property="name" column="name"/>
<result property="gender" column="gender"/>
<result property="birthday" column="birthday"/>
<result property="mobile" column="mobile"/>
<result property="email" column="email"/>
<result property="note" column="note"/>
<association property="workcard"  javaType="com.zwh.pojo.WorkCard">
<id property="cardid" column="cardid"/>
<result property="empid" column="empid"/>
<result property="realname" column="realname"/>
<result property="address" column="address"/>
</association>
</resultMap>

一对多(collection)

pojo实体类:

public class EmployeeList implements Serializable{

	private static final long serialVersionUID = 1L;
	private Integer empId;
	private String name;
	private Integer gender;
	private Date birthday;
	private String mobile;
	private String email;
	private String position;
	private String note;
	private List<salary> salaries;
...
}

接口:

public EmployeeList selectEmployeeList(int id);

mapper映射文件:

<!-- 一对多关联查询 -->
<select id="selectEmployeeList" parameterType="integer" resultMap="relationmap">
	SELECT employee.*,employee_salary.month,employee_salary.salary 
	FROM employee LEFT JOIN employee_salary ON employee.empid=employee_salary.empid 
	where employee.empid=#{id}
</select>

<resultMap type="employeelist" id="relationmap">
<id property="empId" column="empid"/>
<result property="name" column="name"/>
<result property="gender" column="gender"/>
<result property="birthday" column="birthday"/>
<result property="mobile" column="mobile"/>
<result property="email" column="email"/>
<result property="note" column="note"/>
<collection property="salaries" ofType="salary" >
<id property="salaryid" column="salaryid"/>
<result property="empid" column="empid"/>
<result property="month" column="month"/>
<result property="salary" column="salary"/>
</collection>
</resultMap>

多对多

一个员工可以对应多个角色
一个角色也可以对应多个员工
所以他们时多对多关系

当我们查询员工时,应该映射出该员工的多个角色
当我们查询角色时,应该映射出该角色对应的所有员工
所以我们要再两个实体类中添加对应的List< Object >属性

pojo实体类(两个):

EmployeeDdd

public class EmployeeDdd implements Serializable {

	private static final long serialVersionUID = 8284315305946783432L;
	private Integer empId;
	private String name;
	private Integer gender;
	private Date birthday;
	private String mobile;
	private String email;
	private String position;
	private String note;
	private List<Role> roles;
...
}

Role

public class Role implements Serializable{

	private static final long serialVersionUID = -149256487064553912L;
	private int roleid;
	private int empid;
	private String rolename;
	private List<Employee> employees;
...
}

接口(查询员工):

public EmployeeDdd selectEmployeeddd(int id);

mapper映射文件(查询员工):

<!-- 多对多 -->
<select id="selectEmployeeddd" parameterType="integer" resultMap="dddmap">
SELECT * FROM employee,role WHERE employee.empid=role.empid and employee.empid=#{id}
</select>

<resultMap type="employeeddd" id="dddmap">
<id property="empId" column="empid"/>
<result property="name" column="name"/>
<result property="gender" column="gender"/>
<result property="birthday" column="birthday"/>
<result property="mobile" column="mobile"/>
<result property="email" column="email"/>
<result property="note" column="note"/>
<collection property="roles" ofType="role">
<id property="roleid" column="roleid"/>
<result property="empid" column="empid"/>
<result property="rolename" column="rolename"/>
</collection>
</resultMap>

按照角色查询出员工信息也是类似,这里就不写了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis是一种Java持久层框架,它提供了强大的关联查询功能。根据表与表间的关联关系的不同,关联查询分为四种:一对一关联查询、一对多关联查询、多对一关联查询和多对多关联查询[^1]。 一对一关联查询是指两个表之间存在一对一的关系,可以通过在查询语句中使用嵌套查询或者使用关联映射来实现。嵌套查询是指在主查询中嵌套子查询,通过子查询获取关联表的数据。关联映射是指在主查询的结果集中包含关联表的数据,通过配置关联映射来实现。以下是一个示例代码演示了如何在Mybatis中进行一对一关联查询: ```xml <!-- 定义关联映射 --> <resultMap id="userMap" type="User"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="address" column="address"/> <association property="card" javaType="Card"> <id property="id" column="card_id"/> <result property="cardNo" column="card_no"/> </association> </resultMap> <!-- 执行关联查询 --> <select id="getUserWithCard" resultMap="userMap"> SELECT u.id, u.name, u.address, c.id as card_id, c.card_no FROM user u INNER JOIN card c ON u.card_id = c.id WHERE u.id = #{id} </select> ``` 以上代码中,定义了一个关联映射`userMap`,其中包含了`User`和`Card`两个实体类的属性映射关系。在执行关联查询时,通过`INNER JOIN`将`user`表和`card`表关联起来,并通过`WHERE`条件限定查询结果。最终将查询结果映射到`User`对象中,其中`User`对象中的`card`属性也会被自动填充。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值