几个常见的utils
Posted zengjiao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了几个常见的utils相关的知识,希望对你有一定的参考价值。
PublicUtil:对空值的判断
package com.cn.utils; import java.util.Collection; import java.util.Map; /** * 空判断的实现 */ public class PublicUtil { /** * 判断对象是否Empty(null或元素为0) * 实用于对如下对象做判断:String Collection及其子类 Map及其子类 * @param pObj 待检查对象 * @return boolean 返回的布尔值 */ public static boolean isEmpty(Object pObj) { if (pObj == null) { return true; } if (pObj == "") { return true; } if (pObj instanceof String) { return ((String) pObj).length() == 0; } else if (pObj instanceof Collection) { return ((Collection) pObj).isEmpty(); } else if (pObj instanceof Map) { return ((Map) pObj).size() == 0; } return false; } /** * 判断对象是否为NotEmpty(!null或元素大于0) * 实用于对如下对象做判断:String Collection及其子类 Map及其子类 * @param pObj 待检查对象 * @return boolean 返回的布尔值 */ public static boolean isNotEmpty(Object pObj) { if (pObj == null) { return false; } if (pObj == "") { return false; } if (pObj instanceof String) { return ((String) pObj).length() != 0; } else if (pObj instanceof Collection) { return !((Collection) pObj).isEmpty(); } else if (pObj instanceof Map) { return ((Map) pObj).size() != 0; } return true; } }
TreeNode:节点的属性
package com.cn.utils; import lombok.Data; import java.util.List; @Data public class TreeNode { /** * 节点编码 */ private String nodeCode; /** * 节点名称 */ private String nodeName; /** * ID */ private Long id; /** * 父ID */ private Long pid; /** * 孩子节点信息 */ private List<TreeNode> children; }
TreeUtil:获得父节点、子节点信息
package com.cn.utils; import java.util.ArrayList; import java.util.List; import java.util.Objects; public class TreeUtil { /** 得到所有节点列表 */ public static List<TreeNode> getChildTreeNodes(List<TreeNode> list, Long parentId) { List<TreeNode> returnList = new ArrayList<>(); for (TreeNode treeNode : list) { if (treeNode.getPid() == null) { continue; } if (Objects.equals(treeNode.getPid(), parentId)) { recursionFn(list, treeNode); returnList.add(treeNode); } } return returnList; } /** * 递归列表 */ private static void recursionFn(List<TreeNode> list, TreeNode node) { List<TreeNode> childList = getChildList(list, node); if (PublicUtil.isEmpty(childList)) { return; } node.setChildren(childList); for (TreeNode tChild : childList) { recursionFn(list, tChild); } } /** * 得到子节点列表 */ private static List<TreeNode> getChildList(List<TreeNode> list, TreeNode t) { List<TreeNode> tList = new ArrayList<>(); for (TreeNode treeNode : list) { if (PublicUtil.isEmpty(treeNode.getPid())) { continue; } if (Objects.equals(treeNode.getPid(), t.getId())) { tList.add(treeNode); } } return tList; } }
ResultCodeEnum:异常枚举类
package com.cn.common.consts; import lombok.Getter; /** * 统一返回结果 */ @Getter public enum ResultCodeEnum { SUCCESS(true, 20000, "成功"), UNKNOWN_REASON(false, 20001, "未知错误"), DATA_NOT_FOUND(false, 20001, "数据不存在"), DATA_NOT_FULL(false, 20001, "数据不完整"), BAD_SQL_GRAMMAR(false, 21001, "sql语法错误"), JSON_PARSE_ERROR(false, 21002, "json解析异常"), PARAM_ERROR(false, 21003, "参数不正确"), FILE_UPLOAD_ERROR(false, 21004, "文件上传错误"), VIDEO_UPLOAD_ERROR(false, 21007, "视频上传错误"), VIDEO_DELETE_ERROR(false, 21008, "视频删除失败"), FETCH_VIDEO_PLAYAUTH_ERROR(false, 21009, "获取视频凭证失败"), REFRESH_VIDEO_PLAYAUTH_ERROR(false, 21010, "刷新视频凭证失败"), EXCEL_DATA_IMPORT_ERROR(false, 21005, "Excel数据导入错误"); /** 响应是否成功 */ private Boolean success; /** 返回码 */ private Integer code; /** 返回信息 */ private String message; ResultCodeEnum(Boolean success, Integer code, String message) { this.success = success; this.code = code; this.message = message; }}
ResultCode:全局统一返回结果,用于controller上
package com.cn.common.consts; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import java.util.HashMap; import java.util.Map; @Data @ApiModel(value = "全局统一返回结果") public class ResultCode { @ApiModelProperty(value = "是否成功") private Boolean success; @ApiModelProperty(value = "返回码") private Integer code; @ApiModelProperty(value = "返回消息") private String message; @ApiModelProperty(value = "返回数据") private Map<String, Object> data = new HashMap<String, Object>(); public ResultCode() { } public static ResultCode ok() { ResultCode r = new ResultCode(); r.setSuccess(ResultCodeEnum.SUCCESS.getSuccess()); r.setCode(ResultCodeEnum.SUCCESS.getCode()); r.setMessage(ResultCodeEnum.SUCCESS.getMessage()); return r; } public static ResultCode error() { ResultCode r = new ResultCode(); r.setSuccess(ResultCodeEnum.UNKNOWN_REASON.getSuccess()); r.setCode(ResultCodeEnum.UNKNOWN_REASON.getCode()); r.setMessage(ResultCodeEnum.UNKNOWN_REASON.getMessage()); return r; } public static ResultCode setResult(ResultCodeEnum resultCodeEnum) { ResultCode r = new ResultCode(); r.setSuccess(resultCodeEnum.getSuccess()); r.setCode(resultCodeEnum.getCode()); r.setMessage(resultCodeEnum.getMessage()); return r; } public ResultCode success(Boolean success) { this.setSuccess(success); return this; } public ResultCode message(String message) { this.setMessage(message); return this; } public ResultCode code(Integer code) { this.setCode(code); return this; } public ResultCode data(String key, Object value) { this.data.put(key, value); return this; } public ResultCode data(Map<String, Object> map) { this.setData(map); return this; } }
BusinessException:全局异常处理类
package com.cn.exception; import lombok.Getter; import lombok.Setter; @Getter @Setter public class BusinessException extends RuntimeException { /** * 异常码 */ protected int code; public BusinessException() { } public BusinessException(Throwable cause) { super(cause); } public BusinessException(String message) { super(message); } public BusinessException(String message, Throwable cause) { super(message, cause); } public BusinessException(int code, String message) { super(message); this.code = code; } public BusinessException(int code, String msgFormat, Object... args) { super(String.format(msgFormat, args)); this.code = code; } public BusinessException(ErrorCodeEnum codeEnum, Object... args) { super(String.format(codeEnum.msg(), args)); this.code = codeEnum.code(); } }
BaseService接口:通用的接口,具体类的接口只需要实现这个接口即可,这个接口里面的sql语句可以删除掉。
package com.cn.mybatis; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.session.RowBounds; import org.springframework.transaction.annotation.Transactional; import java.util.List; /** * 通用接口 */ public interface BaseService<T> { /** * 根据实体中的属性值进行查询, 查询条件使用等号 */ List<T> select(T record); /** * 根据主键字段进行查询, 方法参数必须包含完整的主键属性, 查询条件使用等号 */ T selectByKey(Object key); /** * 查询全部结果, select(null)方法能达到同样的效果 */ List<T> selectAll(); /** * 根据实体中的属性进行查询, 只能有一个返回值, 有多个结果是抛出异常, 查询条件使用等号 */ T selectOne(T record); /** * 根据实体中的属性查询总数, 查询条件使用等号 */ int selectCount(T record); /** * 保存一个实体, null的属性不会保存, 会使用数据库默认值 */ int save(T record); /** * 批量保存 */ @Transactional(rollbackFor = Exception.class) int batchSave(List<T> list); /** * 根据主键更新属性不为null的值 */ int update(T entity); /** * 根据实体属性作为条件进行删除, 查询条件使用等号 */ int delete(T record); /** * 批量删除 */ @Transactional(rollbackFor = Exception.class) int batchDelete(List<T> list); /** * 根据主键字段进行删除, 方法参数必须包含完整的主键属性 */ int deleteByKey(Object key); /** * 这个查询支持通过Example类指定查询列, 通过selectProperties方法指定查询列 */ List<T> selectByExample(Object example); /** * 根据Example条件进行查询总数 */ int selectCountByExample(Object example); /** * 根据Example条件更新实体record包含的不是null的属性值 */ int updateByExample(@Param("record") T record, @Param("example") Object example); /** * 根据Example条件删除数据 */ int deleteByExample(Object example); /** * 根据实体属性和RowBounds进行分页查询 */ List<T> selectByRowBounds(T record, RowBounds rowBounds); /** * 根据example条件和RowBounds进行分页查询 */ List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds); }
BaseServiceImpl:通用接口的实现类,对应的具体实现类继承该实现类并且实现相关的接口即可,如public class MdcAddressImpl extends BaseServiceImpl<PcMdcAddress> implements MdcAddressService
package com.cn.mybatis; import com.cn.exception.BusinessException; import org.apache.ibatis.session.RowBounds; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import tk.mybatis.mapper.common.Mapper; import java.util.List; /** * 通用接口实现类 */ public abstract class BaseServiceImpl<T> implements BaseService<T> { /** * The Logger. */ protected final Logger logger = LoggerFactory.getLogger(this.getClass()); /** * The Mapper. */ @Autowired protected Mapper<T> mapper; /** * Gets mapper. * * @return the mapper */ public Mapper<T> getMapper() { return mapper; } /** * Select list. * * @param record the record * * @return the list */ @Override public List<T> select(T record) { return mapper.select(record); } /** * Select by key t. * * @param key the key * * @return the t */ @Override public T selectByKey(Object key) { return mapper.selectByPrimaryKey(key); } /** * Select all list. * * @return the list */ @Override public List<T> selectAll() { return mapper.selectAll(); } /** * Select one t. * * @param record the record * * @return the t */ @Override public T selectOne(T record) { return mapper.selectOne(record); } /** * Select count int. * * @param record the record * * @return the int */ @Override public int selectCount(T record) { return mapper.selectCount(record); } /** * Select by example list. * * @param example the example * * @return the list */ @Override public List<T> selectByExample(Object example) { return mapper.selectByExample(example); } /** * Save int. * * @param record the record * * @return the int */ @Override public int save(T record) { return mapper.insertSelective(record); } /** * Batch save int. * * @param list the list * * @return the int */ @Override public int batchSave(List<T> list) { int result = 0; for (T record : list) { int count = mapper.insertSelective(record); result += count; } return result; } /** * Update int. * * @param entity the entity * * @return the int */ @Override public int update(T entity) { return mapper.updateByPrimaryKeySelective(entity); } /** * Delete int. * @param record the record * * @return the int */ @Override public int delete(T record) { return mapper.delete(record); } /** * Delete by key int. * * @param key the key * * @return the int */ @Override public int deleteByKey(Object key) { return mapper.deleteByPrimaryKey(key); } /** * Batch delete int. * * @param list the list * * @return the int */ @Override public int batchDelete(List<T> list) { int result = 0; for (T record : list) { int count = mapper.delete(record); if (count < 1) { logger.error("删除数据失败"); throw new BusinessException("删除数据失败!"); } result += count; } return result; } /** * Select count by example int. * * @param example the example * * @return the int */ @Override public int selectCountByExample(Object example) { return mapper.selectCountByExample(example); } /** * Update by example int. * * @param record the record * @param example the example * * @return the int */ @Override public int updateByExample(T record, Object example) { return mapper.updateByExampleSelective(record, example); } /** * Delete by example int. * * @param example the example * * @return the int */ @Override public int deleteByExample(Object example) { return mapper.deleteByPrimaryKey(example); } /** * Select by row bounds list. * * @param record the record * @param rowBounds the row bounds * * @return the list */ @Override public List<T> selectByRowBounds(T record, RowBounds rowBounds) { return mapper.selectByRowBounds(record, rowBounds); } /** * Select by example and row bounds list. * * @param example the example * @param rowBounds the row bounds * * @return the list */ @Override public List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds) { return mapper.selectByExampleAndRowBounds(example, rowBounds); } }
MyMapper:和dao层有关,具体的dao层只需要继承该类即可。
package com.cn.mybatis; import tk.mybatis.mapper.common.Mapper; import tk.mybatis.mapper.common.mysqlMapper; /** * @author 今夜无月 * @version V1.0 * @Package com.cn.mybatis * @date 2019-12-09 16:52 * @Copyright xiao */ public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> { }
以上是关于几个常见的utils的主要内容,如果未能解决你的问题,请参考以下文章
js代码片段: utils/lcoalStorage/cookie