【java】【MyBatisPlus】【五】MyBatisPlus一些实战总结(GET请求特殊string、批量更新)

目录

一、实体

二、get请求传参list

1、postman设置

2、controller层

3、service

4、serviceImpl

5、mapper

6、 前端vue

 7、调用后端接口

三、批量更新(controller实现方式,通过实体id)

四、批量更新(service实现方式循环通过实体id)

1、controller层

2、service

3、serviceImpl

五、批量更新(service实现方式循环非ID方式)

1、serviceImpl

2、vue页面

3、调用后端接口


前言:在使用mybatis-plus过程当中遇到的一些问题总结下来,本文有get请求传参、批量更新(controller和service层两种不同实现方式)

一、实体

package com.bocai.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;


import jakarta.validation.constraints.NotEmpty;
import lombok.Data;

/**
 * 
 * @TableName sysparam
 */
@TableName(value ="sysparam")
@Data
public class Sysparam implements Serializable {
    /**
     * ID
     */
    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 参数代码
     */
    @NotEmpty  //值不能为空,并且内容不能为空
    private String paramcode;

    /**
     * 参数名称
     */
    @NotEmpty  //值不能为空,并且内容不能为空
    private String paramname;

    /**
     * 参数值
     */
    @NotEmpty  //值不能为空,并且内容不能为空
    private String paramvalue;



    @TableField(exist = false)
    private static final long serialVersionUID = 1L;
}

二、get请求传参list

1、postman设置

【java】【MyBatisPlus】【五】MyBatisPlus一些实战总结(GET请求特殊string、批量更新)_第1张图片

2、controller层

package com.bocai.controller;

import com.bocai.common.Result;
import com.bocai.pojo.Emp;
import com.bocai.pojo.Sysparam;
import com.bocai.service.SysparamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Validated
@RequestMapping("/sysparam")
public class SysparamController {

    @Autowired
    private SysparamService sysparamService;

    /**
     * 查询系统参数设置
     * @return
     */
    @GetMapping
    public Result queryLarkRobotAddress(@RequestParam("paramcode") String list){
        log.info("查询系统参数:{}",list);
        List larkRobotAddress = sysparamService.getLarkRobotAddress(list);
        return Result.success(larkRobotAddress);
    }


}

3、service

package com.bocai.service;

import com.bocai.pojo.Sysparam;
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.List;
import java.util.Map;

/**
* @author cheng
* @description 针对表【sysparam】的数据库操作Service
* @createDate 2023-12-12 13:53:59
*/
public interface SysparamService extends IService {


    /**
     * 查询系统参数
     *
     * @param list
     * @return
     */
    List getLarkRobotAddress(String list);


    
}

4、serviceImpl

package com.bocai.controller;

import com.bocai.common.Result;
import com.bocai.pojo.Emp;
import com.bocai.pojo.Sysparam;
import com.bocai.service.SysparamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Validated
@RequestMapping("/sysparam")
public class SysparamController {

    @Autowired
    private SysparamService sysparamService;

    /**
     * 查询系统参数设置
     * @return
     */
    @GetMapping
    public Result queryLarkRobotAddress(@RequestParam("paramcode") String list){
        log.info("查询系统参数:{}",list);
        List larkRobotAddress = sysparamService.getLarkRobotAddress(list);
        return Result.success(larkRobotAddress);
    }

  
}

5、mapper

package com.bocai.mapper;

import com.bocai.pojo.Sysparam;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.Map;

/**
* @author cheng
* @description 针对表【sysparam】的数据库操作Mapper
* @createDate 2023-12-12 13:53:59
* @Entity com.bocai.pojo.Sysparam
*/
@Mapper
public interface SysparamMapper extends BaseMapper {


}




6、 前端vue


 7、调用后端接口

//导入request.js请求工具
import request from '@/utils/request.js'



// 查询系统参数
export const sysParamGetService = (paramCodes)=>{
   
    return request.get('/sysParam?paramCode='+paramCodes);
    
   }

三、批量更新(controller实现方式,通过实体id)

因为使用的更新方式是id,后续我pass掉了

package com.bocai.controller;

import com.bocai.common.Result;
import com.bocai.pojo.Emp;
import com.bocai.pojo.Sysparam;
import com.bocai.service.SysparamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Validated
@RequestMapping("/sysparam")
public class SysparamController {

    @Autowired
    private SysparamService sysparamService;

     /**
     * 批量更新
     * @param sysparamList
     * @return
     */

    @PutMapping
    public  Result update(@RequestBody List sysparamList){
        log.info("需要更新的数据为{}",sysparamList);
        sysparamService.updateBatchById(sysparamList);
        return Result.success();
    }

}

四、批量更新(service实现方式循环通过实体id)

1、controller层

package com.bocai.controller;

import com.bocai.common.Result;
import com.bocai.pojo.Emp;
import com.bocai.pojo.Sysparam;
import com.bocai.service.SysparamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@Validated
@RequestMapping("/sysparam")
public class SysparamController {

    @Autowired
    private SysparamService sysparamService;

    /**
     * 查询系统参数设置
     * @return
     */
    @GetMapping
    public Result queryLarkRobotAddress(@RequestParam("paramcode") String list){
        log.info("查询系统参数:{}",list);
        List larkRobotAddress = sysparamService.getLarkRobotAddress(list);
        return Result.success(larkRobotAddress);
    }

    /**
     * 批量更新
     * @param sysparamList
     * @return
     */
    @PutMapping
    public  Result update(@RequestBody List sysparamList){
        log.info("需要更新的数据为{}",sysparamList);
        sysparamService.updateSysparamList(sysparamList);
   
        return Result.success();
    }

}

2、service

package com.bocai.service;

import com.bocai.pojo.Sysparam;
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.List;
import java.util.Map;

/**
* @author cheng
* @description 针对表【sysparam】的数据库操作Service
* @createDate 2023-12-12 13:53:59
*/
public interface SysparamService extends IService {


    /**
     * 查询系统参数
     *
     * @param list
     * @return
     */
    List getLarkRobotAddress(String list);


    /**
     * 批量更新, 目前没有使用上
     * @param sysparamList
     */
    void updateSysparamList(List sysparamList);
}

3、serviceImpl

package com.bocai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.Sysparam;
import com.bocai.service.SysparamService;
import com.bocai.mapper.SysparamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* @author cheng
* @description 针对表【sysparam】的数据库操作Service实现
* @createDate 2023-12-12 13:53:59
*/
@Service
public class SysparamServiceImpl extends ServiceImpl
    implements SysparamService {

    @Autowired
    private SysparamMapper sysparamMapper;

    /**
     * 查询全部参数
     *
     * @param list
     * @return
     */
    @Override
    public List getLarkRobotAddress(String list) {
        List params = Arrays.asList(list.split(",")); // 将列表分解成多个参数
        LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Sysparam::getParamcode, params);
        return sysparamMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 批量更新,目前没有使用上
     * @param sysparamList
     */
    @Override
    public void updateSysparamList(List sysparamList) {
        // 映射请求体中的参数列表到Java对象中
        for (Sysparam parameter : sysparamList) {
            // 根据参数值更新数据库中的数据
            sysparamMapper.updateById(parameter);
        }

    }
}



五、批量更新(service实现方式循环非ID方式)

1、serviceImpl

package com.bocai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bocai.pojo.Sysparam;
import com.bocai.service.SysparamService;
import com.bocai.mapper.SysparamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* @author cheng
* @description 针对表【sysparam】的数据库操作Service实现
* @createDate 2023-12-12 13:53:59
*/
@Service
public class SysparamServiceImpl extends ServiceImpl
    implements SysparamService {

    @Autowired
    private SysparamMapper sysparamMapper;

    /**
     * 查询全部参数
     *
     * @param list
     * @return
     */
    @Override
    public List getLarkRobotAddress(String list) {
        List params = Arrays.asList(list.split(",")); // 将列表分解成多个参数
        LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(Sysparam::getParamCode, params);
        return sysparamMapper.selectList(lambdaQueryWrapper);
    }

    /**
     * 批量更新,
     * @param sysparamList
     */
    @Override
    public void updateSysparamList(List sysparamList) {
        // 映射请求体中的参数列表到Java对象中
        for (Sysparam parameter : sysparamList) {
            // 根据参数值更新数据库中的数据
//            sysparamMapper.updateById(parameter);
            LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(parameter.getParamCode() != null,Sysparam::getParamCode,parameter.getParamCode());
            sysparamMapper.update(parameter,lambdaQueryWrapper);
        }

    }
}



2、vue页面


3、调用后端接口

//导入request.js请求工具
import request from '@/utils/request.js'



// 查询系统参数
export const sysParamGetService = (paramCodes)=>{
   
    return request.get('/sysParam?paramCode='+paramCodes);
    
   }




   // 更新系统参数
export const sysParamUpdateService=(sysParam)=>{    
    return request.put('/sysParam',sysParam)
}

你可能感兴趣的:(java,mybatis-plus,java,开发语言)