欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot和MybatisPlus實(shí)現(xiàn)通用Controller示例

 更新時(shí)間:2025年03月14日 08:31:30   作者:呱呱123#  
本文主要介紹了SpringBoot和MybatisPlus實(shí)現(xiàn)通用Controller示例,只需創(chuàng)建實(shí)體類和mapper接口,就可以實(shí)現(xiàn)單表的增刪改查操作,具有一定的參考價(jià)值,感興趣的可以了解一下

基于SpringBoot和MybatisPlus實(shí)現(xiàn)通用Controller,只需要?jiǎng)?chuàng)建實(shí)體類和mapper接口,單表增刪改查接口就已經(jīng)實(shí)現(xiàn),提升開發(fā)效率

1.定義通用controller

package com.xian.controller;


import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xian.common.alias.*;
import com.xian.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@RestController
@RequestMapping("/api/v1/data")
public class BaseController<T extends Serializable> {
    @Autowired
    private ApplicationContext applicationContext;

    private T entity;

    // 使用泛型和IService來處理通用CRUD操作
    protected <S extends BaseMapper<T>> S getMapper(String entityName) throws Exception {
        String serviceName = entityName + "Mapper"; // 假設(shè)服務(wù)名與實(shí)體名相同
        return (S) applicationContext.getBean(serviceName);
    }

    @GetMapping("/{entityName}/get/{id}")
    public Result get(@PathVariable String entityName, @PathVariable Long id) throws Exception {
        BaseMapper<T> mapper = getMapper(entityName);
        return Result.success(mapper.selectById(id));
    }

    @GetMapping("/{entityName}/all")
    public Result get(@PathVariable String entityName) throws Exception {
        BaseMapper<T> mapper = getMapper(entityName);
        return Result.success(mapper.selectList(new QueryWrapper<>()));
    }


    @PostMapping("/{entityName}/insert")
    public Result insert(@PathVariable String entityName,@RequestBody T entity) throws Exception {
        BaseMapper<T> baseMapper =  getMapper(entityName);
        ValidateService<T> validateService = new ValidateServiceImpl<>();
        validateService.validate(entity,baseMapper);
        baseMapper.insert(entity);
        return Result.success();
    }
    @PutMapping("/{entityName}/update")
    public Result update(@PathVariable String entityName,@RequestBody T entity) throws Exception {
        BaseMapper<T> baseMapper =  getMapper(entityName);
        // 使用Spring注入驗(yàn)證服務(wù)
        // 驗(yàn)證數(shù)據(jù)
        ValidateService<T> validateService = new ValidateServiceImpl<>();
        validateService.validate(entity, baseMapper);
        baseMapper.updateById(entity);
        return Result.success();
    }


    @PutMapping("/{entityName}/delete/{id}")
    public Result update(@PathVariable String entityName,@PathVariable Long id) throws Exception {
        BaseMapper<T> baseMapper =  getMapper(entityName);
        baseMapper.deleteById(id);
        return Result.success();
    }


    @PutMapping("/{entityName}/deleteByIds")
    public Result update(@PathVariable String entityName,@RequestBody Collection<Long> ids) throws Exception {
        BaseMapper<T> baseMapper =  getMapper(entityName);
        baseMapper.deleteBatchIds(ids);
        return Result.success();
    }

    // 可以添加其他通用的增刪改查方法...
    @PostMapping("/{entityName}/list")
    public Result update(@PathVariable String entityName, @RequestBody PageRequestVo pageRequest) throws Exception {
        BaseMapper<T> baseMapper =  getMapper(entityName);
        System.out.println("pageRequest = " + pageRequest);
        PageHelper.startPage(pageRequest.getPage(), pageRequest.getSize());
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        List<String> sort = pageRequest.getSorts();
        if (sort!=null&& !sort.isEmpty()) {
            sort.forEach(o -> {
                if (o.endsWith("Asc")) {
                    queryWrapper.orderByAsc(o.replace("Asc", ""));
                }else if (o.endsWith("Desc")) {
                    queryWrapper.orderByDesc(o.replace("Desc", ""));
                }else {
                    queryWrapper.orderByAsc(o);
                }
            });
        }
        if (!MapUtil.isEmpty(pageRequest.getParams())){
            // 處理查詢參數(shù)
            pageRequest.getParams().forEach((field, values) -> {
                if (values != null && !values.isEmpty()) {
                    if (field.endsWith("Like")) {
                        for (Object value : values) {
                            queryWrapper.like(field.replace("Like",""), value);
                        }
                    }else if (field.endsWith("Is")){
                        for (Object value : values) {
                            queryWrapper.eq(field.replace("Like",""), value);
                        }
                    }else if (field.endsWith("Between")){
                        queryWrapper.between(field.replace("Between",""), values.get(0), values.get(1));
                    }else if (field.endsWith("IsNull")){
                        queryWrapper.isNull(field.replace("IsNull",""));
                    }else if (field.endsWith("IsNotNull")){
                        queryWrapper.isNotNull(field.replace("IsNotNull",""));
                    }else if (field.endsWith("NotIn")){
                        queryWrapper.notIn(field.replace("NotIn",""), values);
                    }else if (field.endsWith("In")){
                        queryWrapper.in(field.replace("In",""), values);
                    }else if (field.endsWith("Gt")){
                        queryWrapper.gt(field.replace("Gt",""), values.get(0));
                    }else if (field.endsWith("Ge")){
                        queryWrapper.ge(field.replace("Ge",""), values.get(0));
                    }else if (field.endsWith("Lt")){
                        queryWrapper.lt(field.replace("Lt",""), values.get(0));
                    }else if (field.endsWith("Le")){
                        queryWrapper.le(field.replace("Le",""), values.get(0));
                    }else if (field.endsWith("Eq")){
                        for (Object value : values) {
                            queryWrapper.eq(field.replace("Eq",""), value);
                        }
                    }else if (field.endsWith("Ne")){
                        queryWrapper.ne(field.replace("Ne",""), values.get(0));
                    }else if (field.endsWith("NotBetween")){
                        queryWrapper.notBetween(field.replace("NotBetween",""), values.get(0), values.get(1));
                    }else {
                        for (Object value : values) {
                            queryWrapper.eq(field, value);
                        }
                    }
                }
            });
        }
        return Result.success(PageInfo.of(baseMapper.selectList(queryWrapper)));
    }

}

2.創(chuàng)建業(yè)務(wù)實(shí)體和mapper接口,

@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class User extends Account {

    @TableId(type = IdType.AUTO)
    private Integer id;
    private String username;
    private String password;
    private String name;
    private String avatar;
    private String role;
    private String sex;
    private String phone;
    private String email;
    private String info;
    private String birth;

    @TableField(exist = false)
    private Integer blogCount;
    @TableField(exist = false)
    private Integer likesCount;
    @TableField(exist = false)
    private Integer collectCount;

}

mapper接口:

@Mapper
public interface UserMapper extends BaseMapper<User> {
}

postman測(cè)試

到此這篇關(guān)于SpringBoot和MybatisPlus實(shí)現(xiàn)通用Controller的文章就介紹到這了,更多相關(guān)SpringBoot MybatisPlus 通用Controller內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java 在Word文檔中添加藝術(shù)字的示例

    Java 在Word文檔中添加藝術(shù)字的示例

    這篇文章主要介紹了Java 在Word文檔中添加藝術(shù)字的示例,幫助大家使用Java處理word文檔,感興趣的朋友可以了解下
    2020-09-09
  • Idea配置maven-tomcat-plugin插件實(shí)現(xiàn)項(xiàng)目部署

    Idea配置maven-tomcat-plugin插件實(shí)現(xiàn)項(xiàng)目部署

    今天小編就為大家分享一篇關(guān)于Idea配置maven-tomcat-plugin插件實(shí)現(xiàn)項(xiàng)目部署,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • Java利用Netty時(shí)間輪實(shí)現(xiàn)延時(shí)任務(wù)

    Java利用Netty時(shí)間輪實(shí)現(xiàn)延時(shí)任務(wù)

    時(shí)間輪是一種可以執(zhí)行定時(shí)任務(wù)的數(shù)據(jù)結(jié)構(gòu)和算法。本文將為大家詳細(xì)講解一下Java如何利用Netty時(shí)間輪算法實(shí)現(xiàn)延時(shí)任務(wù),感興趣的小伙伴可以了解一下
    2022-08-08
  • RestTemplate實(shí)現(xiàn)多種底層HTTP客戶端類庫的切換用法

    RestTemplate實(shí)現(xiàn)多種底層HTTP客戶端類庫的切換用法

    這篇文章主要為大家詳細(xì)的講解了RestTemplate實(shí)現(xiàn)多種底層HTTP客戶端類庫的切換示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助祝大家多多進(jìn)步
    2022-03-03
  • 解決HashMap多線程操作導(dǎo)致死循環(huán)問題

    解決HashMap多線程操作導(dǎo)致死循環(huán)問題

    文章主要講述了在多線程環(huán)境下,HashMap的并發(fā)操作可能導(dǎo)致的死循環(huán)問題,包括鏈表/紅黑樹結(jié)構(gòu)破壞、擴(kuò)容過程中的混亂以及讀寫不一致等,為了解決這些問題,文章建議使用線程安全的ConcurrentHashMap替代HashMap,并介紹了其分段鎖機(jī)制和優(yōu)化方案
    2025-01-01
  • 你知道jdk竟有4個(gè)random嗎

    你知道jdk竟有4個(gè)random嗎

    這篇文章主要給大家介紹了關(guān)于jdk中4個(gè)random的相關(guān)資料,分別是Random、ThreadLocalRandom、SecureRandom以及SplittableRandom,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-06-06
  • spring cloud 的監(jiān)控turbine-rabbitmq的示例

    spring cloud 的監(jiān)控turbine-rabbitmq的示例

    這篇文章主要介紹了spring cloud 的監(jiān)控turbine-rabbitmq的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-05-05
  • 解決Springboot @WebFilter攔截器未生效問題

    解決Springboot @WebFilter攔截器未生效問題

    這篇文章主要介紹了解決Springboot @WebFilter攔截器未生效問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • 解析ConcurrentHashMap: get、remove方法分析

    解析ConcurrentHashMap: get、remove方法分析

    ConcurrentHashMap是由Segment數(shù)組結(jié)構(gòu)和HashEntry數(shù)組結(jié)構(gòu)組成。Segment的結(jié)構(gòu)和HashMap類似,是一種數(shù)組和鏈表結(jié)構(gòu),今天給大家普及java面試常見問題---ConcurrentHashMap知識(shí),一起看看吧
    2021-06-06
  • SpringAOP實(shí)現(xiàn)自定義接口權(quán)限控制

    SpringAOP實(shí)現(xiàn)自定義接口權(quán)限控制

    本文主要介紹了SpringAOP實(shí)現(xiàn)自定義接口權(quán)限控制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-11-11

最新評(píng)論