使用SpringBoot集成MyBatis对管理员的查询操作

增删改查中的查询操作,对所有的普通管理员进行查询操作。

效果展示:

不仅可以在打开页面时进行对管理员的自动查询操作,还可以在输入框进行查询。

首先是前端向后端发送POST请求,后端接收到请求,如果是有参数传到后端那就是搜索框查询,如果没有参数,就是页面加载所有管理员的整体查询。

见前端代码:

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

后端接收响应:

java 复制代码
@RestController
@RequestMapping("/admin/admin")
public class AdminController {
    @Autowired
    AdminService adminService;

    @PostMapping("/admins/")
    CommonData returnResult(@RequestBody Admin admin) {
        CommonData commonData=adminService.findAdmins(admin);
        return commonData;
    }
}

分别调用Service层,Dao层,最后通过MyBatis查询。

数据库建表如下: 一共三个表,管理员表,角色表,管理员角色关系表。

对管理员角色表为什么要单独列出来的解释:一个管理员可以拥有多个角色,并不是一对一的关系,所以不能进行管理员表和角色表的关联查询。

MyBatis写法:

XML 复制代码
<?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>
相关推荐
你的人类朋友11 分钟前
✍️Node.js CMS框架概述:Directus与Strapi详解
javascript·后端·node.js
面朝大海,春不暖,花不开30 分钟前
自定义Spring Boot Starter的全面指南
java·spring boot·后端
HelloWord~1 小时前
SpringSecurity+vue通用权限系统
vue.js·spring boot
钡铼技术ARM工业边缘计算机1 小时前
【成本降40%·性能翻倍】RK3588边缘控制器在安防联动系统的升级路径
后端
wangjinjin1801 小时前
使用 IntelliJ IDEA 安装通义灵码(TONGYI Lingma)插件,进行后端 Java Spring Boot 项目的用户用例生成及常见问题处理
java·spring boot·intellij-idea
CryptoPP2 小时前
使用WebSocket实时获取印度股票数据源(无调用次数限制)实战
后端·python·websocket·网络协议·区块链
白宇横流学长2 小时前
基于SpringBoot实现的大创管理系统设计与实现【源码+文档】
java·spring boot·后端
草捏子2 小时前
状态机设计:比if-else优雅100倍的设计
后端
考虑考虑4 小时前
Springboot3.5.x结构化日志新属性
spring boot·后端·spring
涡能增压发动积4 小时前
一起来学 Langgraph [第三节]
后端