日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java - 通用 CRUD(增、删、改、查)工具类,代码高效复用

發布時間:2023/12/18 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java - 通用 CRUD(增、删、改、查)工具类,代码高效复用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

前些天發現了一個巨牛的人工智能學習網站,通俗易懂,風趣幽默,忍不住分享一下給大家。點擊跳轉到教程。

1. 基本 CRUD 方法實現:

package com.xxx.xxx.ls.xxx.utils;import com.alibaba.fastjson.JSON; import com.google.common.collect.ImmutableMap; import com.xxx.xxx.ls.xxx.dto.LSResultDTO; import com.xxx.xxx.ls.xxx.model.BaseModel; import com.xxx.xxx.ls.xxx.dto.BaseDTO; import com.xxx.xxx.ls.xxx.dto.LSExceptionResultDTO; import com.xxx.xxx.ls.xxx.enums.ResponseStatusEnum; import com.xxx.xxx.ls.xxx.exceptions.InstitutionException; import com.xxx.xxx.ls.xxx.model.BaseInfo; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.cxf.common.util.CollectionUtils; import tk.mybatis.mapper.common.Mapper; import tk.mybatis.mapper.entity.Example;import java.util.Date; import java.util.List;@Slf4j public class BaseMethodUtils {/*** 根據id查詢數據** @param id* @param tClass* @param mapper* @param <T>* @param <E>* @return*/public static <T, E> LSResultDTO<T> queryById(String id, Class<T> tClass, Mapper<E> mapper) {log.info("根據id查詢:{}", id);if (StringUtils.isBlank(id)) {return LSExceptionResultDTO.fail(ResponseStatusEnum.PARAMETERS_ERROR);}try {E e = mapper.selectByPrimaryKey(id);log.info("查詢的信息:{}", JSON.toJSONString(e));T t = JSON.parseObject(JSON.toJSONString(e), tClass);if (t == null) {return LSExceptionResultDTO.fail(ResponseStatusEnum.WRONG_CONFIGURATION);}return LSResultDTO.ok(t);} catch (Exception e) {return LSExceptionResultDTO.fail(ResponseStatusEnum.EXEC_FAILURE);}}/*** 查數據列表** @param tClass* @param mapper* @param <T>* @param <E>* @return*/public static <T, E> List<T> queryAll(Class<T> tClass, Mapper<E> mapper) {List<E> infoList = mapper.selectAll();return JsonTransUtils.list2OtherList(infoList, tClass);}/*** 根據條件查數據列表,條件為“等于”** @param tClass* @param eClass* @param map 封裝條件* @param mapper* @param <T>* @param <E>* @return*/public static <T, E> List<T> queryAll(Class<T> tClass, Class<E> eClass, ImmutableMap<String, Object> map, Mapper<E> mapper) {Example e = new Example(eClass);Example.Criteria criteria = e.createCriteria();map.forEach(criteria::andEqualTo);List<E> infoList = mapper.selectByExample(e);return JsonTransUtils.list2OtherList(infoList, tClass);}/*** 根據條件查單條數據** @param queryModel* @param tClass 返回類對象* @param eClass 數據庫映射類對象* @param mapper* @param <T>* @param <E>* @return*/public static <T, E> T queryOne(BaseModel queryModel, Class<T> tClass, Class<E> eClass, Mapper<E> mapper) {log.info("根據條件查詢一條,參數query:{}", JSON.toJSONString(queryModel));if (queryModel == null) {throw new InstitutionException(400, "請求參數有誤");}E e = JSON.parseObject(JSON.toJSONString(queryModel), eClass);E returnInfo = null;try {returnInfo = mapper.selectOne(e);} catch (Exception e1) {throw new InstitutionException(500, "執行異常,數據庫可能存在多個相同條件的數據");}if (returnInfo == null) {log.error("沒找到該條件的配置信息");throw new InstitutionException(404, "該條件的配置不存在");}return JSON.parseObject(JSON.toJSONString(returnInfo), tClass);}/*** 根據條件查詢列表** @param queryModel* @param eClass 映射類對象* @param tClass 返回類對象* @param mapper* @param <T>* @param <E>* @return*/public static <T, E> List<T> queryListByWhere(BaseModel queryModel, Class<E> eClass, Class<T> tClass, Mapper<E> mapper) {log.info("根據條件查詢,參數AppQuery:{}", JSON.toJSONString(queryModel));if (queryModel == null) {throw new InstitutionException(400, "請求參數有誤");}E e = JSON.parseObject(JSON.toJSONString(queryModel), eClass);List<E> infoList = mapper.select(e);if (CollectionUtils.isEmpty(infoList)) {log.error("沒找到該條件的配置信息");throw new InstitutionException(404, "該條件的配置不存在");}return JsonTransUtils.list2OtherList(infoList, tClass);}/*** 保存** @param baseDTO* @param eClass 映射類對象* @param mapper* @param <E>* @return*/public static <E extends BaseInfo> String save(BaseDTO baseDTO, Class<E> eClass, Mapper<E> mapper) {log.info("保存DTO:{}", JSON.toJSONString(baseDTO));if (baseDTO == null) {throw new InstitutionException(400, "請求參數異常");}try {E e = JSON.parseObject(JSON.toJSONString(baseDTO), eClass);e.setGmtCreate(new Date());e.setGmtUpdate(new Date());e.setUserCreate(baseDTO.getUserCreate());e.setUserUpdate(baseDTO.getUserCreate());mapper.insert(e);return e.getId();} catch (Exception e) {throw new InstitutionException(500, "保存失敗");}}/*** 保存,null值取數據庫默認值** @param baseDTO* @param eClass 映射類對象* @param mapper* @param <E>* @return*/public static <E extends BaseInfo> String saveSelective(BaseDTO baseDTO, Class<E> eClass, Mapper<E> mapper) {log.info("保存DTO:{}", JSON.toJSONString(baseDTO));if (baseDTO == null) {throw new InstitutionException(400, "請求參數異常");}try {E e = JSON.parseObject(JSON.toJSONString(baseDTO), eClass);e.setGmtCreate(new Date());e.setGmtUpdate(new Date());e.setUserCreate(baseDTO.getUserCreate());e.setUserUpdate(baseDTO.getUserCreate());mapper.insertSelective(e);return e.getId();} catch (Exception e) {throw new InstitutionException(500, "保存失敗");}}/*** 更新** @param baseDTO* @param eClass* @param mapper* @param <E>* @return*/public static <E extends BaseInfo> Integer update(BaseDTO baseDTO, Class<E> eClass, Mapper<E> mapper) {log.info("更新AppDTO:{}", JSON.toJSONString(baseDTO));if (baseDTO == null) {throw new InstitutionException(400, "請求參數異常");}if (StringUtils.isBlank(baseDTO.getId())) {throw new InstitutionException(400, "id不能為空");}try {E e = JSON.parseObject(JSON.toJSONString(baseDTO), eClass);e.setGmtUpdate(new Date());e.setUserUpdate(baseDTO.getUserUpdate());return mapper.updateByPrimaryKey(e);} catch (Exception e) {throw new InstitutionException(500, "更新失敗");}}/*** 更新,null值取數據庫默認值** @param baseDTO* @param eClass* @param mapper* @param <E>* @return*/public static <E extends BaseInfo> Integer updateSelective(BaseDTO baseDTO, Class<E> eClass, Mapper<E> mapper) {log.info("更新AppDTO:{}", JSON.toJSONString(baseDTO));if (baseDTO == null) {throw new InstitutionException(400, "請求參數異常");}if (StringUtils.isBlank(baseDTO.getId())) {throw new InstitutionException(400, "id不能為空");}try {E e = JSON.parseObject(JSON.toJSONString(baseDTO), eClass);e.setGmtUpdate(new Date());e.setUserUpdate(baseDTO.getUserUpdate());return mapper.updateByPrimaryKeySelective(e);} catch (Exception e) {throw new InstitutionException(500, "更新失敗");}}/*** 根據id刪除** @param id* @param mapper* @param <E>* @return*/public static <E> Integer deleteById(String id, Mapper<E> mapper) {log.info("刪除id:{}", id);return mapper.deleteByPrimaryKey(id);} }

2. json 轉換類工具類:

package com.xxx.xxx.xxx.xxx.utils;import com.alibaba.fastjson.JSON;import java.util.ArrayList; import java.util.List;public class JsonTransUtils{/*** 轉為新列表(對象屬性名要相同)* @param originList 原列表* @param tClass 新列表類對象* @param <T>* @return*/public static <T> List<T> list2OtherList(List originList,Class<T> tClass){List<T> list = new ArrayList<>();for (Object info : originList) {T t = JSON.parseObject(JSON.toJSONString(info),tClass);list.add(t);}return list;} }

3. sql 工具類:

package com.xxx.xxx.xxx.xxx.utils;import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import tk.mybatis.mapper.entity.Example;import java.lang.reflect.Field;@Slf4j public class SqlUtils {/*** 封裝模糊查詢條件,排序條件(不排序傳“”或null)** @param t 封裝查詢條件類* @param orderByContent* @param <T>* @return* @throws IllegalAccessException*/public static <T> Example getSelectExample(T t, String orderByContent) throws IllegalAccessException {Example example = new Example(t.getClass());Example.Criteria criteria = example.createCriteria();if (!StringUtils.isBlank(orderByContent)) {example.setOrderByClause(orderByContent);}Field[] declaredFields = t.getClass().getDeclaredFields();for (Field field : declaredFields) {field.setAccessible(true);if (field.get(t) != null && !"serialVersionUID".equals(field.getName())) {criteria.andLike(field.getName(), "%" + field.get(t).toString() + "%");}}return example;} }

?

總結

以上是生活随笔為你收集整理的java - 通用 CRUD(增、删、改、查)工具类,代码高效复用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。