天天看點

工作中用到的一些瑣碎知識(Mybatis日志列印)

工作日常的記錄

在idea中安裝Mybatis X 和 Mybatis Log插件

  1. 下載下傳Mybatis X插件,打開idea的settings設定,找到plugins,搜尋MyBatis X,點選install進行下載下傳。
    工作中用到的一些瑣碎知識(Mybatis日志列印)
  2. 下載下傳Mybatis Log插件,現在idea中安裝Mybatis Log插件需要破解,點如下連結進行下載下傳:

    連結: https://pan.baidu.com/s/1V24NMk4VQodlMcJQ5u2zuQ 密碼: fb0r

    下載下傳成功之後,點選圖上所示區域,然後選擇通過以上連結下載下傳好的jar檔案,點選确定,重新開機idea。

    工作中用到的一些瑣碎知識(Mybatis日志列印)
    重新開機idea之後,在配置檔案中添加如下配置:
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
           

至此,mybatis log安裝成功,在程式運作時,便可檢視對應的sql語句的log了。

使用jackson序列化深拷貝對象(泛型)
//使用jackson序列化深拷貝List對象
    public static <T,E> List<E> paramList2EntityList(List<T> t,Class<E> e) {
        List<E> entities = new ArrayList<>();
        for(T param : t) {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                E entity = objectMapper.readValue(objectMapper.writeValueAsString(param),e);
                entities.add(entity);
            } catch (JsonProcessingException ex) {
                ex.printStackTrace();
            }
        }
        return entities;
    }

    //單一深拷貝
    public static <T, E> E param2Entity(T t, Class<E> e) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            E entity = objectMapper.readValue(objectMapper.writeValueAsString(t), e);
            return entity;
        } catch (JsonProcessingException ex) {
            ex.printStackTrace();
        }
        return null;
    }
           

注意:深拷貝時如果源對象和目标對象是不同的類,要保證源對象和目标對象的變量的數量,名稱,資料類型一緻。

List 倒序
//反轉List(倒序排列)
	Collections.reverse(investmentList);
           
List按指定字段排序
/**
     * 加上.reversed()時是倒序,反之亦反
     */
	Collections.sort(investmentList,Comparator.comparingLong(Investment::getCreateTime); //升序
	investmentList.sort(Comparator.comparingLong(Investment::getCreateTime));  //升序

	Collections.sort(investmentList,Comparator.comparingLong(Investment::getCreateTime).reversed()); //降序
	investmentList.sort(Comparator.comparingLong(Investment::getCreateTime).reversed());  //降序

	//多字段排序可以用thenComparing
	Collections.sort(investmentList,Comparator.comparingLong(Investment::getCreateTime).thenComparing(Rounds::getName).reversed());  //時間升序,名稱降序