mybatis的一對一和一對多查詢

一對一html

需求:查詢出員工的部門+員工的信息(一個員工對應一個部門)java

一、建表app

         部門表(department)測試

 

 

           員工表(employee)spa

 

 

 二、新建兩個類(Department和Employee)和接口3d

 

 

 

public class Department {
    private Integer deptId;
    private String deptName;
    private LocalDate createDate = LocalDate.now();
    private String deptLoc;

  

public class Employee {
    private Integer empId;
    private String username;
    private String password;
    private String realName;
    private LocalDate bornDate;
    private LocalDate hireDate=LocalDate.now();
    private float salary;
   private Department department;

  

public interface EmployeeMapper {
    /**
     * create by: 
     * description:根據id查詢員工信息
     * create time: 2020/8/11
     *
      * @Param: null
     * @return
     */
    Employee efindById(int id);
}

 

三、配置映射表、code

對於結果的處理有兩種方式:嵌套結果、嵌套查詢
xml

嵌套結果htm

<!--嵌套結果-->
    <resultMap id="employee" type="Employee">
        <id property="empId" column="emp_id"></id>
        <result property="hireDate" column="entry_date"></result>
        <result property="username" column="username"></result>
        <result property="password" column="password"></result>
        <result property="bornDate" column="born_date"></result>
        <result property="salary" column="salary"></result>
        <!--assocaition來映射一對一的關聯關係-->
        <association property="department" column="dept_id" javaType="Department">
            <id property="deptId" column="dept_id"></id>
            <result property="deptName" column="dept_name"></result>
            <result property="createDate" column="create_Date"></result>
            <result property="deptLoc" column="dept_loc"></result>
        </association>
    </resultMap>
    <select id="efindById" parameterType="int" resultMap="employee">
       select d.* ,e.* from department d,employee e where d.dept_id=e.dept_id and e.emp_id=#{empId};
    </select>

嵌套查詢對象

   <!--嵌套查詢-->
    <select id="efindById" resultMap="employee2">
        select emp_id,entry_date,username,password,realname,dept_id from employee where emp_id = #{id}
    </select>
    <resultMap id="employee2" type="Employee">
        <id property="empId" column="emp_id"></id>
        <result property="hireDate" column="entry_date"></result>
        <association property="department" select="edu.cduestc.book.Dao.DepartmentDao.findById" column="dept_id"> //這個column表示傳遞的參數。由於findById查詢要使用dept_id這個值
        </association>
    </resultMap>
    <select id="findById" resultMap="employee2">
        select * from department where dept_id = #{dept_id}
    </select>

 四、測試

 

一對多

需求:查詢一個部門下有多少員工

一、表和上面同樣不變

二、創建類和接口方法

 

1 public class Department {
2     private Integer deptId;
3     private String deptName;
4     private LocalDate createDate = LocalDate.now();
5     private String deptLoc;
6     private List<Employee> employees;
1 public class Employee {
2     private Integer empId;
3     private String username;
4     private String password;
5     private String realName;
6     private LocalDate bornDate;
7     private LocalDate hireDate=LocalDate.now();
8     private float salary;
1 public interface DepartmentDao {
2          Department findAllEmployee(int dept_id);
3          Department findAllEmployee2(int dept_id);
4 }    

三、配置xml文件

  嵌套結果

 1 <!--嵌套結果查詢-->
 2     <select id="findAllEmployee" resultMap="getEmployeelist">
 3         select e.*,d.dept_name 
 4             from employee e,department d 
 5                 where e.dept_id=d.dept_id 
 6                     and e.dept_id=#{dept_id}
 7     </select>
 8     <resultMap id="getEmployeelist" type="Department">
 9         <id property="deptId" column="dept_id"></id>
10         <result property="deptName" column="dept_name"></result>
11         <collection property="employees" ofType="Employee">
12             <id property="empId" column="emp_id"></id>
13             <result property="username" column="username"></result>
14             <result property="password" column="password"></result>
15             <result property="realName" column="realname"></result>
16             <result property="hireDate" column="entry_date"></result>
17             <result property="bornDate" column="born_date"></result>
18             <result property="salary" column="salary"></result>
19         </collection>
20     </resultMap>

嵌套查詢

 1 <!--    嵌套查詢-->
 2     <select id="findAllEmployee2" resultMap="getEmployeelist2" parameterType="int">
 3         select dept_id,dept_name
 4             from department
 5                 where dept_id=#{dept_id} 
 6     </select>
 7     <select id="getEmployees" resultType="Employee" parameterType="int">
 8         select *
 9             from employee
10                 where dept_id=#{dept_id}
11     </select>
12     <resultMap id="getEmployeelist2" type="Department">
13         <id property="deptId" column="dept_id"></id>
14         <result property="deptName" column="dept_name"></result>
15         <collection property="employees" ofType="Employee" column="dept_id" select="getEmployees">
16 
17         </collection>
18     </resultMap>

 

若是不太記得resultMap的具體使用能夠參考如下的介紹

  

<!--column不作限制,能夠爲任意表的字段,而property須爲type 定義的pojo屬性-->
<resultMap id="惟一的標識" type="映射的pojo對象">
  <id column="表的主鍵字段,或者能夠爲查詢語句中的別名字段" jdbcType="字段類型" property="映射pojo對象的主鍵屬性" />
  <result column="表的一個字段(能夠爲任意表的一個字段)" jdbcType="字段類型" property="映射到pojo對象的一個屬性(須爲type定義的pojo對象中的一個屬性)"/>
  <association property="pojo的一個對象屬性" javaType="pojo關聯的pojo對象">
    <id column="關聯pojo對象對應表的主鍵字段" jdbcType="字段類型" property="關聯pojo對象的主席屬性"/>
    <result  column="任意表的字段" jdbcType="字段類型" property="關聯pojo對象的屬性"/>
  </association>
  <!-- 集合中的property須爲oftype定義的pojo對象的屬性-->
  <collection property="pojo的集合屬性" ofType="集合中的pojo對象">
    <id column="集合中pojo對象對應的表的主鍵字段" jdbcType="字段類型" property="集合中pojo對象的主鍵屬性" />
    <result column="能夠爲任意表的字段" jdbcType="字段類型" property="集合中的pojo對象的屬性" />  
  </collection>
</resultMap>
collection標籤使用的是嵌套查詢
<collection column="傳遞給嵌套查詢語句的字段參數" property="pojo對象中集合屬性" ofType="集合屬性中的pojo對象" select="嵌套的查詢語句" > </collection>

 

參考:

https://blog.51cto.com/wuqinglong/1726152

https://blog.51cto.com/wuqinglong/1726099

http://www.javashuo.com/article/p-cbycfsdr-cq.html

相關文章
相關標籤/搜索