mybatis、elementui后端传值前端展示问题

我们应该有这样一个常识,当后台向前台返回数据时,除了普通类型字段之外(如文本),所有有关联关系的,我们都应该返回一个对象;
比如我数据库中有个requence字段(该字段数据库中存储为varchar等普通字段),这个字段关联另一个对象的主键,也就是说通过这个字段可以找到另外一个对象(对象a),我们称之为映射;
而现在我只需要a中的name,如下所示,我们不应该返回name,而是应该将整个对象返回,你如果返回name的话当我在前端修改这行数据时,岂不是要根据这个name去查到id?因为我数据库中保存的是id啊,这不是麻烦了吗
在这里插入图片描述
然后,前台拿到这个对象后直接.name就能展示了:

<el-table-column prop="customerSource.name" label="客户来源" width="170">

这是最简单的做法,

=============================
如果要添加分页等功能的话,因为有多个对象你需要用List将你的对象保存起来:

    @Override
    public PageResult<T> selectPageByQuery(BaseQuery baseQuery) {
        Long total = getBaseMapper().getTotalDataCount(baseQuery);
        if (total == 0L) {
            return new PageResult<>();
        }
        List<T> result = getBaseMapper().selectPageByQuery(baseQuery);
        return new PageResult<>(total, result);
    }

关联对象通过association来映射

    <select id="selectPageByQuery" resultMap="customerResultMap">
        SELECT c.id,c.name,c.gender,c.tel,c.seller,i.requence,
        i.id iid,i.name iname,e.sn ssn,e.id sid,e.username susername
        FROM t_customer c
        LEFT JOIN t_systemdictionaryitem i ON c.customerSource = i.requence
        LEFT JOIN t_employee e ON c.seller = e.sn
        <include refid="whereSql"/>
        LIMIT #{begin},#{pageSize}
    </select>
    <resultMap id="customerResultMap" type="Customer">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="gender" property="gender"/>
        <result column="tel" property="tel"/>
        <association property="seller" javaType="Employee">
            <id column="sid" property="id"/>
            <result column="susername" property="username"/>
            <result column="ssn" property="sn"/>
        </association>
        <!-- 查数据字典明细中查出来的值封装到实体类的customerSource属性中去 -->
        <!-- 注意这两者的属性必须一致 -->
        <association property="customerSource" javaType="SystemDictionaryItem">
            <id column="iid" property="id"/>
            <result column="requence" property="requence"/>
            <result column="iname" property="name"/>
        </association>
    </resultMap>

后端返回的分页对象:

public class PageResult<T> {
    /* 返回总条数 */
    private Long total;
    /* 返回分页数据 */
    private List<T> result = new ArrayList<>();

前端将后端返回的对象解析保存到指定的数组中:

                this.$http.patch("/customer/selectPageByQuery", param).then(res => {
                    /* 这种东西我们一般将res打印出来找到我们需要的属性来填充就好了 */
                    this.customers = res.data.result;
                    this.total = res.data.total;
                    this.listLoading = false;
                });

打印customers看看:
在这里插入图片描述
在这里插入图片描述
当前所有值都是存在customers中的
而我们前端展示的列表是和customers绑定的,于是就可以直接点了:

        <!-- 列表 -->
        <el-table :data="customers" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
                  style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="100" label="id">
            </el-table-column>
            <el-table-column prop="name" label="客户名称" width="170">
            </el-table-column>
            <!-- :formatter将0\1分别转换成女和男显示 -->
            <el-table-column prop="gender" label="性别" :formatter="genderFormatter" width="170">
            </el-table-column>
            <el-table-column prop="tel" label="电话" width="170">
            </el-table-column>
            <el-table-column prop="seller.username" label="营销人员" width="170">
            </el-table-column>
            <el-table-column prop="customerSource.name" label="客户来源" width="170">
            </el-table-column>
            <el-table-column label="操作" width="240">
                <template slot-scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要在customer下新建一个cfg文件夹,并在文件夹下新建一个名为MybatisPlusConfig.java的文件。在这个文件中,我们需要导入相关的包,包括com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor、org.springframework.context.annotation.Bean、org.springframework.context.annotation.Configuration和org.springframework.transaction.annotation.EnableTransactionManagement。我们还需要在类上添加@EnableTransactionManagement和@Configuration注解。 在MybatisPlusConfig类中,我们需要添加一个名为paginationInterceptor的Bean,使用@Bean注解来标识这是一个Bean。通过PaginationInterceptor类的实例化,我们可以配置分页插件的一些属性,例如设置请求页面大于最大页后的操作,设置最大单页面限制数量等。在这里,我们可以参考中的代码示例进行配置。 接下来,我们需要在dao层的CustomerTableDao中添加Mapper注解,以便与数据库进行交互。具体的代码示例可以参考中的博客。 最后,如果我们需要向表中添加数据,可以使用POST请求,并在实体类中添加@TableId(type = IdType.AUTO)注解来实现id的自增。这样,我们就可以通过向后端接口发送POST请求来向表中添加数据。具体的代码示例可以参考中的引用内容。 综上所述,这是实现springboot整合mybatis后端接口的一种方法。可以按照以上的步骤和示例代码来实现后端接口的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [SpringBoot整合mybatis实现后端接口](https://blog.csdn.net/weixin_45297286/article/details/107703220)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值