天天看點

Code Craft

代碼篇

如果要随機産生一個a到b(包括a和b)之間的整數,可以使用下面的公式:
int num = (int)( Math.random() * ( b – a + 1 )) + a;
 
        // 随機産生一個a到b(包括a和b)之間的整數
        int a = 0;
        int b = 10;
        int num = (int) ((Math.random()*Math.abs(a-b))+Math.min(a,b));

           
List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        //計算 List 集合中,所有元素的和
        int total = list.stream().map(item -> item).reduce((sum, n) -> sum + n).get();//total=6
           
#将list中所有POJO屬性set新值
pojoList.forEach(x -> x.setNeedStocktake(1));
           
// 擷取java.util.List<T> 範型類型
System.out.println(list.get(0).getClass());// com.ljheee.mail.MonitorPO

Object obj = list.get(0);//MonitorPO
Field[] declaredFields = obj.getClass().getDeclaredFields();

declaredFields[0].setAccessible(true);//orderId
System.out.println(declaredFields[0].get(obj));//擷取orderId字段值
           
//數組逆轉
    public void reverse(char[] arr, int begin, int end) {
        while(end > begin) {
            char temp = arr[begin];
            arr[begin] = arr[end];
            arr[end] = temp;

            begin++;
            end--;
        }
    }
           
int a[] = new int[]{1, 2, 3, 4, 5, 6, 7, 8};
    //經典洗牌算法
    public static void shuffle(int[] a) {
        Random random = new Random();
        int n = a.length;
        for (int i = n - 1; i >= 1; i--) {
            int temp = a[i];
            int j = random.nextInt(n);
            a[i] = a[j];
            a[j] = temp;
            // swap(a[i],a[j])
        }
    }
           

指令使用篇

#git結合Linux下awk指令,統計[指定author]git送出代碼行數
#該指令在.git目錄下運作;更換author即可。去掉--author參數,則統計整個項目代碼行
MacBook-Pro% git log --author="lijianhua" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
added lines: 39, removed lines: 39, total lines: 0
           

配置檔案篇

<!-- mybatis-config.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!-- 分布式應用必須禁用查詢緩存 -->
        <setting name="cacheEnabled" value="false"/>
        <!-- 禁用懶加載-->
        <setting name="lazyLoadingEnabled" value="false"/>
        <!-- 預設執行逾時時間(秒) -->
        <setting name="defaultStatementTimeout" value="180"/>
        <!-- 下劃線命名轉駝峰命名-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!-- 預設開啟“擷取插入自增主鍵的值”(僅insert語句) -->
        <setting name="useGeneratedKeys" value="true"/>
        <setting name="logImpl" value="LOG4J2"/>
    </settings>
</configuration>
           

繼續閱讀