溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SpringBoot如何實現多表查詢功能

發布時間:2021-09-14 17:29:49 來源:億速云 閱讀:414 作者:小新 欄目:開發技術

這篇文章主要介紹SpringBoot如何實現多表查詢功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

實體類:

Emp 類:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
    private int id;
    private String lastname;
    private String email;
    private int gender;
    private int did;
    private Dept dept;
    private Date birth = new Date();
}

Dept類:

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {
    private int id;
    private String dname;
}

Mapper接口

EmpMapper:

//這個注解表示這是一個mybatis的mapper類
@Mapper
@Repository
public interface EmpMapper {
    void addEmp(Emp emp);
    void deleteEmp(int id);
    void updateEmp(Emp emp);
    Emp queryEmpById(int id);
    List<Emp> queryEmpList();
}

DeptMapper:

@Mapper
@Repository
public interface DeptMapper {
    List<Dept> queryDeptList(@Param("cid") int cid);
}

EmpMapper.xml 配置文件

<mapper namespace="com.acoffee.mapper.EmpMapper">

    <resultMap id="EmpMap" type="Emp">
        <id column="id" property="id"></id>
        <id column="lastname" property="lastname"></id>
        <id column="email" property="email"></id>
        <id column="gender" property="gender"></id>
        <association property="dept" select="com.acoffee.mapper.DeptMapper.queryDeptList" column="did"></association>
    </resultMap>

    <select id="queryEmpList" resultMap="EmpMap">
        select * from emp_dept.employees
    </select>

</mapper>

DeptMapper.xml配置文件

<mapper namespace="com.acoffee.mapper.DeptMapper">

    <select id="queryDeptList" resultType="Dept">
        select * from emp_dept.department where id = #{id};
    </select>
    
</mapper>

前端頁面(部分)

<tr th:each="emp:${emps}">
	  <td th:text="${emp.getId()}"></td>
	  <td th:text="${emp.getLastname()}"></td>
	  <td th:text="${emp.getEmail()}"></td>
	  <td th:text="${emp.getGender()==0?'女':'男'}"></td>
	  <td th:text="${emp.dept.getDname()}"></td>
	  <td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}"></td>
	  <td>
	      <a class="btn btn-sm btn-primary" th:href="@{/emp/{id}/(id=${emp.getId()})}" rel="external nofollow" >編輯</a>
	      <a class="btn btn-sm btn-danger" th:href="@{/delemp/{id}/(id=${emp.getId()})}" rel="external nofollow" >刪除</a>
	  </td>
</tr>

查詢結果:

SpringBoot如何實現多表查詢功能

上述我們采用的是分步查詢。

我們下面使用association嵌套映射

其實這里查詢會出現一個奇怪的事情,

SpringBoot如何實現多表查詢功能
SpringBoot如何實現多表查詢功能

因為我們現在兩個表中都有id這個字段,所以我們在映射時使用<result property="id" column="id"></result>去查找部門的id時就發現查出來的是員工的id,就是因為員工的id與部門的id字段名重名了?

EmpMapper:

<mapper namespace="com.acoffee.mapper.EmpMapper">

    <resultMap id="EmpMap" type="Emp">
        <id column="id" property="id"></id>
        <id column="lastname" property="lastname"></id>
        <id column="email" property="email"></id>
        <id column="gender" property="gender"></id>
        <association property="dept" javaType="com.acoffee.pojo.Dept">
            <result property="id" column="id"></result>
            <result property="dname" column="dname"></result>
        </association>
    </resultMap>

    <select id="queryEmpList" resultMap="EmpMap">
        select emp.*,dept.* from emp_dept.employees emp,emp_dept.department dept where emp.did = dept.id
    </select>
</mapper>

上述執行結果如下

SpringBoot如何實現多表查詢功能

我們發現是員工id,我們現在將數據庫中部門表的id改為pid

SpringBoot如何實現多表查詢功能

修改配置文件以及實體類

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {
    private int pid;
    private String dname;
}
<mapper namespace="com.acoffee.mapper.EmpMapper">

    <resultMap id="EmpMap" type="Emp">
        <id column="id" property="id"></id>
        <id column="lastname" property="lastname"></id>
        <id column="email" property="email"></id>
        <id column="gender" property="gender"></id>
        <association property="dept" javaType="com.acoffee.pojo.Dept">
            <result property="pid" column="pid"></result>
            <result property="dname" column="dname"></result>
        </association>
    </resultMap>

    <select id="queryEmpList" resultMap="EmpMap">
        select emp.*,dept.* from emp_dept.employees emp,emp_dept.department dept where emp.did = dept.pid
    </select>

</mapper>

執行結果:

SpringBoot如何實現多表查詢功能

我們就發現查詢的就是部門的id了

SpringBoot如何實現多表查詢功能

但是針對上面這個問題我們將部門的id字段名改為did (did在員工表中也存在) 此時我們再去查詢又發現還是可以把部門id查詢出來

SpringBoot如何實現多表查詢功能

這個問題就離譜,難道是因為id是主鍵,did不是主鍵的原因?就離譜

以上是“SpringBoot如何實現多表查詢功能”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女