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

本文介绍了如何在前端通过POST请求与后端接口交互,实现对管理员的查询操作,包括页面加载时的自动查询和搜索框输入的动态查询。后端利用SpringMVC和MyBatis处理请求,通过分表设计和SQL查询实现复杂的数据关联。
摘要由CSDN通过智能技术生成

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

效果展示:

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

首先是前端向后端发送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 {
    @Autowired
    AdminService 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 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>

  • 12
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Adellle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值