概述
开发代码注意事项
1.如果是涉及到修改与删除的,要对参数进行非空判断,或者写特定语句,常用的是断言类或者抛异常
eg:1.注意如果字段是Integer类型传值为0时,在Mapper.xml中被 条件过滤
Integer类型只做“null”的判断,不做空的判断
<if test="param.isFinalGenerate !=null" >
AND is_final_generate = #{param.isFinalGenerate}
</if>
eg:2.断言类
/*
* Copyright 2019-2029 geekidea(https://github.com/geekidea)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.yunping.framework.common.api;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.api.IErrorCode;
import com.baomidou.mybatisplus.extension.exceptions.ApiException;
import com.yunping.framework.common.exception.BusinessException;
import com.yunping.framework.common.exception.VerifyException;
import org.springframework.util.StringUtils;
import java.util.Collection;
import java.util.Map;
/**
* <p>
* REST API 业务断言<br>
* 参考:org.junit.Assert
* </p>
*
* @author geekidea
* @since 2018-11-08
*/
public class Assert {
protected Assert() {
// to do noting
}
/**
* 大于O
*/
public static void gtZero(Integer num, IErrorCode errorCode) {
if (num == null || num >= 0) {
Assert.fail(errorCode);
}
}
public static void gtZero(Integer num, ApiCode errorCode) {
if (num == null || num > 0) {
Assert.failCode(errorCode);
}
}
/**
* 功能描述 小于
* @Author Solming
* @Date 17:51 2021/4/7
* @Param [num, errorCode]
* @return void
**/
public static void ltZero(Integer num, ApiCode errorCode) {
if (num == null || num < 0) {
Assert.failCode(errorCode);
}
}
public static void ltZero(Long num, ApiCode errorCode) {
if (num == null || num < 0) {
Assert.failCode(errorCode);
}
}
/**
* 大于等于O
*/
public static void geZero(Integer num, IErrorCode errorCode) {
if (num == null || num < 0) {
Assert.fail(errorCode);
}
}
/**
* num1大于num2
*/
public static void gt(Integer num1, Integer num2, String tigMsg) {
if (num1 > num2) {
Assert.verifyFail(tigMsg);
}
}
/**
* num1大于等于num2
*/
public static void ge(Integer num1, Integer num2, IErrorCode errorCode) {
if (num1 < num2) {
Assert.fail(errorCode);
}
}
/**
* obj1 eq obj2
*/
public static void eq(Object obj1, Object obj2, IErrorCode errorCode) {
if (!obj1.equals(obj2)) {
Assert.fail(errorCode);
}
}
public static void isTrue(boolean condition, IErrorCode errorCode) {
if (!condition) {
Assert.fail(errorCode);
}
}
public static void isTrue(boolean condition, ApiCode apiCode) {
if (condition) {
Assert.failCode(apiCode);
}
}
public static void isTrue(boolean condition, String tipMsg) {
if (condition) {
Assert.verifyFail(tipMsg);
}
}
public static void isFalse(boolean condition, IErrorCode errorCode) {
if (condition) {
Assert.fail(errorCode);
}
}
public static void isFalse(boolean condition, String tipMsg) {
if (!condition) {
Assert.verifyFail(tipMsg);
}
}
public static void isFalse(boolean condition, ApiCode apiCode) {
if (!condition) {
Assert.failCode(apiCode);
}
}
public static void isNull(IErrorCode errorCode, Object... conditions) {
if (ObjectUtils.isNotNull(conditions)) {
Assert.fail(errorCode);
}
}
public static void isNull(Object obj,String tigMsg) {
if (ObjectUtils.isNull(obj)) {
Assert.verifyFail(tigMsg);
}
}
public static void isNull(Object obj,ApiCode apiCode) {
if (ObjectUtils.isNull(obj)) {
Assert.failCode(apiCode);
}
}
public static void notNull(IErrorCode errorCode, Object... conditions) {
if (ObjectUtils.isNull(conditions)) {
Assert.fail(errorCode);
}
}
public static void notNull(Object obj,String tigMsg) {
if (ObjectUtils.isNotNull(obj)) {
Assert.verifyFail(tigMsg);
}
}
public static void notNull(Object obj,ApiCode apiCode) {
if (ObjectUtils.isNotNull(obj)) {
Assert.failCode(apiCode);
}
}
/**
* <p>
* 失败结果
* </p>
*
* @param errorCode 异常错误码
*/
public static void fail(IErrorCode errorCode) {
throw new ApiException(errorCode);
}
public static void verifyFail(String message) {
throw new VerifyException(message);
}
public static void failCode(ApiCode apiCode) {
throw new BusinessException(apiCode);
}
public static void fail(boolean condition, IErrorCode errorCode) {
if (condition) {
Assert.fail(errorCode);
}
}
public static void fail(String message) {
throw new ApiException(message);
}
public static void fail(boolean condition, String message) {
if (condition) {
Assert.fail(message);
}
}
public static void notEmpty(Object[] array, IErrorCode errorCode) {
if (ObjectUtils.isEmpty(array)) {
Assert.fail(errorCode);
}
}
public static void noNullElements(Object[] array, IErrorCode errorCode) {
if (array != null) {
for (Object element : array) {
if (element == null) {
Assert.fail(errorCode);
}
}
}
}
public static void notEmpty(Collection<?> collection, IErrorCode errorCode) {
if (CollectionUtils.isNotEmpty(collection)) {
Assert.fail(errorCode);
}
}
public static void isEmpty(String str, ApiCode apiCode) {
if (StringUtils.isEmpty(str)) {
Assert.failCode(apiCode);
}
}
public static void isEmpty(String str, String tipMsg) {
if (StringUtils.isEmpty(str)) {
Assert.verifyFail(tipMsg);
}
}
public static void isEmpty(Collection collection, String tipMsg) {
if (CollectionUtils.isEmpty(collection)) {
Assert.verifyFail(tipMsg);
}
}
public static void isEmpty(Collection collection, ApiCode apiCode) {
if (CollectionUtils.isEmpty(collection)) {
Assert.failCode(apiCode);
}
}
public static void notEmpty(Map<?, ?> map, IErrorCode errorCode) {
if (ObjectUtils.isEmpty(map)) {
Assert.fail(errorCode);
}
}
public static void isInstanceOf(Class<?> type, Object obj, IErrorCode errorCode) {
Assert.notNull(errorCode, type);
if (!type.isInstance(obj)) {
Assert.fail(errorCode);
}
}
public static void isAssignable(Class<?> superType, Class<?> subType, IErrorCode errorCode) {
Assert.notNull(errorCode, superType);
if (subType == null || !superType.isAssignableFrom(subType)) {
Assert.fail(errorCode);
}
}
}
eg:3.抛异常
if (vo.getMessContentType()==null || StringUtils.isBlank(vo.getMessContent())){
throw new RuntimeException("参数缺失");
}
2.sql语句不能用$防止sql注入
3.如果你是微服务,不要嫌麻烦要根据三层架构模式,不然后面扩展很麻烦,都写在Controller层显然不对
最后
以上就是忧郁早晨为你收集整理的开发代码注意事项开发代码注意事项的全部内容,希望文章能够帮你解决开发代码注意事项开发代码注意事项所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复