欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot集成MyBatis對管理員的查詢操作

 更新時間:2023年11月23日 09:04:33   作者:Adellle  
本文主要介紹了SpringBoot集成MyBatis對管理員的查詢操作,實現(xiàn)增刪改查中的查詢操作,對所有的普通管理員進行查詢操作,感興趣的可以了解一下

增刪改查中的查詢操作,對所有的普通管理員進行查詢操作。

效果展示:

不僅可以在打開頁面時進行對管理員的自動查詢操作,還可以在輸入框進行查詢。

首先是前端向后端發(fā)送POST請求,后端接收到請求,如果是有參數(shù)傳到后端那就是搜索框查詢,如果沒有參數(shù),就是頁面加載所有管理員的整體查詢。

見前端代碼: 

		methods: {
			adminFind(){
				this.$http.post("admin/admin/admins/",this.form).then(resp => {
					this.tableData = resp.data.data;
				})
			},
		},
		mounted() {
			this.adminFind();
		}

 后端接收響應:

@RestController
@RequestMapping("/admin/admin")
public class AdminController {
    @Autowired
    AdminService adminService;
 
    @PostMapping("/admins/")
    CommonData returnResult(@RequestBody Admin admin) {
        CommonData commonData=adminService.findAdmins(admin);
        return commonData;
    }
}

分別調(diào)用Service層,Dao層,最后通過MyBatis查詢。 

數(shù)據(jù)庫建表如下: 一共三個表,管理員表,角色表,管理員角色關(guān)系表。

對管理員角色表為什么要單獨列出來的解釋:一個管理員可以擁有多個角色,并不是一對一的關(guān)系,所以不能進行管理員表和角色表的關(guān)聯(lián)查詢。

MyBatis寫法:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ffyc.news.dao.AdminDao">
    <resultMap id="findAdmins" type="Admin">
        <id property="id" column="id"></id>
        <result property="account" column="account"></result>
        <result property="gender" column="gender"></result>
        <result property="adminPhone" column="admin_phone"></result>
        <result property="address" column="address"></result>
        <result property="type" column="type"></result>
        <result property="operTime" column="oper_time"></result>
        <!--封裝操作人-->
        <association property="admin" javaType="Admin">
            <result property="account" column="operaccount"></result>
        </association>
        <collection property="roles" javaType="list" ofType="Role" select="findRolesById" column="id"></collection>
    </resultMap>
 
    <select id="findAdmins" resultMap="findAdmins">
        SELECT
        a.id,
        a.account,
        a.gender,
        a.admin_phone,
        a.address,
        a.type,
        a.oper_time,
        oa.account operaccount
        FROM
        admin a
        LEFT JOIN admin oa
        ON oa.id = a.adminid
        WHERE a.type = 1
        <if test="account!=''">and a.account = #{account}</if>
        <if test="gender!=''">and a.gender = #{gender}</if>
    </select>
    <select id="findRolesById" resultType="Role">
        SELECT
        r.name
        FROM
        ROLE r
        LEFT JOIN admin_role ar
        ON r.id = ar.roleid
        WHERE ar.adminid = #{id};
    </select>
</mapper>

到此這篇關(guān)于SpringBoot集成MyBatis對管理員的查詢操作的文章就介紹到這了,更多相關(guān)SpringBoot MyBatis查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論