首页 > 其他分享 >多表查询之一对一

多表查询之一对一

时间:2022-12-28 19:22:39浏览次数:37  
标签:dep 多表 一对一 查询 EmpDao did emp mybatis

1对1
1对多
多对1
多对多

 

员工表

 

 

 

部门表

 

 

 

dao

 public interface EmpDao {
    Emp selectEmpByEid(int eid);
    List<Emp> selectAllEmps();
 }

映射文件

 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="com.woniuxy.dao.EmpDao">
    <select id="selectEmpByEid" resultMap="empMapper">
        select * from emp e ,dep d where e.did=d.did and eid=#{eid}
    </select>
    <resultMap id="empMapper" type="Emp">
        <id column="eid" property="eid"/>
        <result column="ename" property="ename"/>
        <!-- 对一 -->
        <association property="dep" javaType="Dep">
            <id column="did" property="did"/>
            <result column="dname" property="dname"/>
        </association>
    </resultMap>
    <select id="selectAllEmps" resultMap="empMapper">
        select * from emp e ,dep d where e.did=d.did
    </select>
 </mapper>

 

一对一

 

 

 

 

 

一对一操作流程

1.先在数据库中建表emp和dep表

 

 

2.建立实体类

 

 

 

3.建立EmpDao

 

 

4.在主配置文件中修改配置别名

 

 

5.建立EmpDao的映射文件xml

 

 

6.测试类

 

 

 

 

标签:dep,多表,一对一,查询,EmpDao,did,emp,mybatis
From: https://www.cnblogs.com/huangjiangyang/p/17011083.html

相关文章