Skip to content

idea使用

This page demonstrates usage of some of the runtime APIs provided by VitePress.

去掉sql背景色(第一步有可能一次成功):

Title

Title

Title

maven配置

Title

idea常用模板代码

配置模板代码Title

controller

md
```java
@PostMapping(name = "新增$module$",value = "/addEntity")
public R addEntity(@RequestBody $param$ params){
    try {
        this.$service$.add(params);
        return R.ok();
    }catch (Exception e){
        log.error("新增$module$失败",e);
        if (e instanceof TipException){
            return R.fail(e.getMessage());
        }
        return R.fail("系统繁忙请稍后再试");
    }
}

@PostMapping(name = "更新$module$",value = "/updateEntity")
public R updateEntity(@RequestBody $param$ params){
    try {
        this.$service$.update(params);
        return R.ok();
    }catch (Exception e){
        log.error("更新$module$失败",e);
        if (e instanceof TipException){
            return R.fail(e.getMessage());
        }
        return R.fail("系统繁忙请稍后再试");
    }
}

@PostMapping(name = "删除$module$",value = "/delEntity")
public R delEntity(@RequestBody $param$ params){
    try {
        this.$service$.delEntity(params);
        return R.ok();
    }catch (Exception e){
        log.error("删除$module$失败",e);
        if (e instanceof TipException){
            return R.fail(e.getMessage());
        }
        return R.fail("系统繁忙请稍后再试");
    }
}

@PostMapping(name = "更新$module$状态",value = "/updateStatus")
public R updateStatus(@RequestBody $param$ params){
    try {
        this.$service$.updateStatus(params);
        return R.ok();
    }catch (Exception e){
        log.error("更新$module$状态失败",e);
        if (e instanceof TipException){
            return R.fail(e.getMessage());
        }
        return R.fail("系统繁忙请稍后再试");
    }
}

@PostMapping(name = "查询$module$列表",value = "/listByPage")
public R listByPage(@RequestBody $param$ params){
    try {
        PageInfo pageInfo = this.$service$.listByPage(params);
        return R.ok(pageInfo);
    }catch (Exception e){
        log.error("分页查询$module$列表异常",e);
        if (e instanceof TipException){
            return R.fail(e.getMessage());
        }
        return R.fail("系统繁忙请稍后再试");
    }
}

@PostMapping(name = "根据id查询",value = "/findById")
public R findById(@RequestBody $param$ params){
    try {
        $vo$ byId = this.$service$.findById(params);
        return R.ok(byId);
    }catch (Exception e){
        log.error("根据id查询失败",e);
        if (e instanceof TipException){
            return R.fail(e.getMessage());
        }
        return R.fail("系统繁忙请稍后再试");
    }
}
```

单个controller

md
```java
@PostMapping(name = "$second$",value = "/$first$")
public R $first$(@RequestBody $params$ params){
    try {

        return R.ok();
    }catch (Exception e){
        log.error("$second$失败",e);
        return R.fail(e.getMessage());
    }
}
```

controller头部

md
```java
@Slf4j
@RestController
@RequestMapping("/$first$")
```

service

md
```java
public void add($param$ param){
    FuncUtil.justOrEmpty(param)
            .doOnNext(v -> {
                
            })
            .doOnNext(v -> {
                
            });
}

public void update($param$ param){
    FuncUtil.justOrEmpty(param)
            .doOnNext(v -> {
                
            })
            .doOnNext(v -> {
                
            })
            .subscribe();
}

public List<$vo$> list($param$ param){
    return FuncUtil.justOrEmpty(param)
                   .map(v -> {
                       
                   })
                   .get();
}

public PageInfo listByPage($param$ param){
    return FuncUtil.justOrEmpty(param)
                   .map(v -> {
                       
                   })
                   .get();
}

public void delEntity($param$ param){
    FuncUtil.justOrEmpty(param)
            .doOnNext(v -> {
                
            })
            .subscribe();
}

public void updateStatus($param$ param){
    FuncUtil.justOrEmpty(param)
            .doOnNext(v -> {
                
            })
            .subscribe();
}

public $vo$ findById($param$ param){
    return FuncUtil.justOrEmpty(param)
            .map(v -> {
                
            })
            .get();
}

private void checkInput($param$ v){

}
```

单个service

md
```java
public void $second$($first$ param){
    FuncUtil.justOrEmpty(param)
            .doOnNext(v -> {
                
            })
            .subscribe();
}
```
md
```java
public $third$ $first$($second$ param){
    return FuncUtil.justOrEmpty(param)
           .map(v -> {
               
           })
           .get();
}
```

service头部

md
```java
@Slf4j
@Service
```

函数式编程工具

md
```java
/**
 * 函数式工具类
 * @param <T>
 */
public final class FuncUtil<T> {

    private T sources;

    private FuncUtil(T value){
        sources = value;
    }

    public static <T> FuncUtil<T> justOrEmpty(T sources){
        return new FuncUtil<>(sources);
    }

    /**
     * 执行下一步操作
     * @param consumer
     * @return
     */
    public FuncUtil<T> doOnNext(Consumer<T> consumer){
        consumer.accept(sources);
        return this;
    }

    /**
     * 并行操作,注意线程安全问题
     */
    public FuncUtil<T> parallelOnNext(Consumer<T>... consumers){
        Objects.requireNonNull(consumers,"并行消费者不能为空");
        Stream.of(consumers)
                .parallel()
                .forEach(a -> {
                    a.accept(sources);
                });
        return this;
    }

    /**
     * 对象转换
     * @param function
     * @param <R>
     * @return
     */
    public <R> FuncUtil<R> map(Function<T,R> function){
        return new FuncUtil<R>(function.apply(sources));
    }

    /**
     * if分支操作
     * @param predicate
     * @param c1
     * @param c2
     * @return
     */
    public FuncUtil<T> ifOnNext(Predicate<T> predicate,Consumer<T> c1,Consumer<T> c2){
        if (predicate.test(sources)){
            c1.accept(sources);
            return this;
        }
        c2.accept(sources);
        return this;
    }

    /**
     * if分支对象转换
     */
    public <R> FuncUtil<R> ifMap(Predicate<T> predicate,Function<T,R> f1,Function<T,R> f2){
        if (predicate.test(sources)){
            return new FuncUtil<>(f1.apply(sources));
        }
        return new FuncUtil<>(f2.apply(sources));
    }

    /**
     * 消费型策略操作
     */
    public FuncUtil<T> strategyByC(Function<T,Object> types,Consumer<Map<Object,Consumer<T>>> handle){
        HashMap<Object, Consumer<T>> map = new HashMap<>();
        handle.accept(map);
        Consumer<T> consumer = map.get(types.apply(sources));
        Objects.requireNonNull(consumer,"未找到消费型处理器");
        consumer.accept(sources);
        return this;
    }

    /**
     * 函数型策略操作
     */
    public <R> FuncUtil strategyByF(Function<T,Object> types,Consumer<Map<Object,Function<T,R>>> handle){
        HashMap<Object,Function<T, R>> map = new HashMap<>();
        handle.accept(map);
        Function<T, R> function = map.get(types.apply(sources));
        Objects.requireNonNull(function,"未找到函数型处理器");
        return new FuncUtil<R>(function.apply(sources));
    }

    /**
     * 同步订阅
     * @param consumer
     */
    public void subscribe(Consumer<T> consumer){
        consumer.accept(sources);
    }

    public void subscribe(){}

    /**
     * 异步订阅(虽然可以实现异步调度,但是使用的时候还是需要谨慎)
     * @param executor
     * @param consumer
     */
    public void asyncSubscribe(ThreadPoolExecutor executor,Consumer<T> consumer){
        String name = Thread.currentThread().getName();
        executor.submit(() -> {
            Thread.currentThread().setName(name);
            consumer.accept(sources);
        });
    }

    public T get(){return sources;}
}
```

More

Check out the documentation for the full list of runtime APIs.