多个接口可以封装成一个接口处理

在开发中会遇到这样的修求,可能一个页面会根据不同的情况会请求多个接口,返回一样的结果。如果各个接口分别编写一次,先的很搂,并且代码冗余。那么分装成一个方法来请求。
解决办法:其实就是将写一个方法,return请求接口,用另一个方法来接收结果

分装后的代码

import {saleDeptAnalysis,userAnalysis,contractIndustry} from '@/api/bi/customer'  //接口
/**
     * 查询表格数据,接口接口返回的数据
     * @param parmas
     */
    getList(parmas) {
      this.getRequestHead(parmas)
        .then(res => {
          if (res.code === 0) {
            this.tableData = res.data.list
          }
        })
    }
    
    /**
     * 设置请求接口
     * @param parmas
     */
    getRequestHead(parmas) {
      this.loading = true
      this.tableData = []
      if (parmas === 'sale') {
        return saleDeptAnalysis(this.listPostParams)
      } else if (parmas === 'employees') {
        return userAnalysis(this.listPostParams)
      } else if (parmas === 'contract') {
        return contractIndustry(this.listPostParams)
      }
    },

没有封装

 /**
     * 查询表格数据
     * @param parmas
     */
    getList(parmas){
      this.loading = true
      this.tableData = []
      if (parmas === 'sale') {
        saleDeptAnalysis(this.listPostParams)
          .then( res => {
            if (res.code === 0) {
              this.tableData = res.data.list
            }
          })
      } else if (parmas === 'employees') {
        this.listPostParamsif (parmas === 'employees')
        userAnalysis(this.listPostParams)
          .then( res => {
            if (res.code === 0) {
             this.tableData = res.data.list
            }
          })
      } else if (parmas === 'contract') {
        contractIndustry(this.listPostParams)
          .then( res => {
            if (res.code === 0) {
             this.tableData = res.data.list
            }
          })
      }
    },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 MyBatis 中,可以使用 @Param 注解来声明 mapper 接口中的多个参数。例如: public interface UserMapper { List<User> selectUsersByAgeAndGender(@Param("age") int age, @Param("gender") String gender); } 在这个例子中,我们使用 @Param 注解来声明了两个参数 age 和 gender。这样,在 SQL 语句中就可以使用 #{age} 和 #{gender} 来引用这两个参数了。 ### 回答2: 在MyBatis中,Mapper接口的方法可以接受多个参数。当我们需要传递多个参数时,可以选择以下几种方式来声明参数: 1.使用@Param注解:可以在Mapper方法的参数前使用@Param注解来声明每个参数的名称,同时在SQL语句中通过参数名称来引用对应的参数。例如: ``` public User getUserByIdAndName(@Param("id") int id, @Param("name") String name); ``` 在对应的Mapper XML文件中使用#{id}和#{name}来引用这两个参数。 2.使用Map类型参数:可以将所有的参数封装一个Map对象作为方法的参数。在Mapper方法中可以使用Map的key来获取对应的参数值。例如: ``` public User getUserByIdAndName(Map<String, Object> params); ``` 在对应的Mapper XML文件中使用#{id}和#{name}来引用Map中相应的key。 3.使用JavaBean对象参数:可以将多个参数封装一个JavaBean对象,并将该对象作为方法的参数。在Mapper方法中可以使用对象的属性名来获取对应的参数值。例如: ``` public User getUser(UserParams params); ``` 在对应的Mapper XML文件中使用#{id}和#{name}来引用JavaBean对象的属性。 以上是MyBatis中声明Mapper接口多个参数的几种方式,根据具体的业务需求选择合适的方式来声明和传递参数。 ### 回答3: 在MyBatis中,如果Mapper接口的方法需要传入多个参数,可以使用以下两种方式声明参数。 第一种方式是通过使用`@Param`注解来声明参数,示例代码如下: ```java public interface UserMapper { // 使用@Param注解声明多个参数 User getUserById(@Param("id") int id, @Param("name") String name); } ``` 在XML配置文件中,可以使用`#{}`或`${}`占位符来引用这些参数,示例如下: ```xml <select id="getUserById" resultType="User"> SELECT * FROM user WHERE id = #{id} AND name = #{name} </select> ``` 第二种方式是使用JavaBean对象作为参数,示例代码如下: ```java public interface UserMapper { // 使用JavaBean对象作为参数 User getUserById(User user); } ``` 在XML配置文件中,可以直接引用JavaBean对象的属性,示例如下: ```xml <select id="getUserById" parameterType="User" resultType="User"> SELECT * FROM user WHERE id = #{id} AND name = #{name} </select> ``` 需要注意的是,当Mapper接口方法只有一个参数时,参数可以是任意类型,可以是基本类型、String、JavaBean对象等。但当Mapper接口方法有多个参数时,必须使用`@Param`注解或使用JavaBean对象作为参数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值