增刪改查中的查詢操作,對所有的普通管理員進行查詢操作。
效果展示:
不僅可以在打開頁面時進行對管理員的自動查詢操作,還可以在輸入框進行查詢。
首先是前端向后端發送POST請求,后端接收到請求,如果是有參數傳到后端那就是搜索框查詢,如果沒有參數,就是頁面加載所有管理員的整體查詢。
見前端代碼:?
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 {@AutowiredAdminService adminService;@PostMapping("/admins/")CommonData returnResult(@RequestBody Admin admin) {CommonData commonData=adminService.findAdmins(admin);return commonData;}
}
分別調用Service層,Dao層,最后通過MyBatis查詢。?
數據庫建表如下:?一共三個表,管理員表,角色表,管理員角色關系表。
對管理員角色表為什么要單獨列出來的解釋:一個管理員可以擁有多個角色,并不是一對一的關系,所以不能進行管理員表和角色表的關聯查詢。
MyBatis寫法:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//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">SELECTa.id,a.account,a.gender,a.admin_phone,a.address,a.type,a.oper_time,oa.account operaccountFROMadmin aLEFT JOIN admin oaON oa.id = a.adminidWHERE 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">SELECTr.nameFROMROLE rLEFT JOIN admin_role arON r.id = ar.roleidWHERE ar.adminid = #{id};</select>
</mapper>