【java】【MyBatisPlus】【五】MyBatisPlus一些实战总结(get传list、批量更新)

2023-12-13 05:29:18

目录

一、实体

二、get请求传参list

1、postman设置

2、controller层

3、service

4、serviceImpl

5、mapper

三、批量更新(controller实现方式)

四、批量更新(service实现方式)

1、controller层

2、service

3、serviceImpl


前言:在使用mybatis-plus过程当中遇到的一些问题总结下来,本文有get请求传参使list方式、批量更新(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设置

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<Sysparam> {


    /**
     * 查询系统参数
     *
     * @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<Sysparam> {


}




三、批量更新(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;

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

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

}

四、批量更新(service实现方式)

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<Sysparam> 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<Sysparam> {


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


    /**
     * 批量更新, 目前没有使用上
     * @param sysparamList
     */
    void updateSysparamList(List<Sysparam> 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<SysparamMapper, Sysparam>
    implements SysparamService {

    @Autowired
    private SysparamMapper sysparamMapper;

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

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

    }
}



文章来源:https://blog.csdn.net/legend818/article/details/134952181
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。