Encountered various problems in the project

1. The difference between @Autowired and @Resource:

@Autowired is provided by spring, and the injection is matched by type by default

@Resource is provided by java. You can use the name attribute to specify the ben to be injected. If the name attribute is not used, then the injection will be matched according to the type. If there are multiple instances of ben to be injected at this time, an error will be reported.

@autowired can be used in constructors, properties and methods, resource can only be used in properties and methods

@autowired plus @Qualifier annotation can specify the ben to be injected

2. Use @JsonFormat(shape = JsonFormat.Shape.STRING)
annotation

        In the database, the decimal type will be used for issues related to the amount of money. Usually, two decimal places are reserved at the end, but when sending to the front end, when the Json format is converted, the 0 after the number will be removed. So you need to add @JsonFormat(shape = JsonFormat.Shape.STRING) to the amount field, and when formatting json, don't remove 0

3. The datetime problem in the database

        In my current project, when creating a table, the time type is datetime. If the length is not set, the millisecond value is not saved by default. If it is set, it is generally set to 3, which means that the three-digit milliseconds are reserved, and then correspond to the java field The LocalDateTime type, this time usually use annotations:

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone="GMT+8")

        It will put the time back to the front end in the form of json, and format the date. When the front end passes it over, it can be formatted as LocalDateTime according to the format in the pattern.

4. The case where the table field in the database involves a decimal point

        If you set the database field by yourself, you must use the decimal type if the decimal point is involved. Don’t think about using the flaot type to save some byte length, because if you use the double type or float type, it is the double type when it corresponds to java. Attributes, if you use the double type, various precision loss situations will occur. For example, if you define a value of 1.30, you will lose precision when using System.out.println output, and when using double, the last one will be automatically 0 remove

5. When writing the xml file, if you do not want to use the java object to receive the return value, you can directly use the Map to receive the query value

        When writing a query statement, if there is a piece of data that you want to return in the form of a Map, then the return value of the mapper interface can be written as Map<String, Object>, if multiple pieces of data are returned in the form of a Map, the return value in the mapper interface Write the value as List<Map<String,Object>>, as long as you write the resultMap value in the xml file: java.util.Map, you can correspond to the above two situations

        If you use this method, there are many things to pay attention to, that is, resultType="java.util.Map" cannot specify generics here, so no matter what the return value of the interface is received, check it in the xml file The results are finally returned as Map<Object, Object>, although the return value of the interface has a specified generic type, for example, the return value of the interface is: Map<String, String> but the actual generic type of this Map is: Map <Object,Object>
        So when the returned result contains long type or large number type, using Map.get("field name") directly may result in type conversion exception, for example:

         And when the front and back ends are separated, assuming that the return value type in the interface is Map<String, Object> and then there is a long integer type in the result of querying through the xml file, then after using the @responseBody annotation to pass to the front end, precision will be lost In this case, this problem is not a problem with the json toolkit, but caused by the loss of precision when js stores large numbers.

        When js expresses an integer, it can express up to 15 digits, and if it exceeds 15 digits, precision will be lost

        So you need to change the number type to a string, convert it to json, and pass it to the front end without losing precision


        

        Note: Because resultType="java.util.Map" in the xml file cannot specify generics, it is best to use Map<String,Object> to receive the return value of the interface

6. Fuzzy query and paging and sort writing in xml files

7. Common operations of js

8. Simple algorithm for lottery draw of mini program

public static int lottery(double[] prob) {
    double rand = Math.random(); // 生成一个0到1之间的随机数
    double cumulativeProb = 0.0; // 初始化累积概率为0
    for (int i = 0; i < prob.length; i++) { // 遍历每个奖项的概率
        cumulativeProb += prob[i]; // 累加奖项概率
        if (rand < cumulativeProb) { // 如果随机数小于累积概率,表示抽中该奖项
            return i; // 返回该奖项的索引
        }
    }
    return -1; // 如果所有奖项概率之和小于1,则返回-1表示抽奖失败
}

        The basic idea of ​​the algorithm is to first generate a random number rand between 0 and 1, then iterate through the probabilities of each award, and add these probabilities to the variable cumulativeProb one by one. If the random number is less than cumulativeProb, it means that the corresponding award has been drawn, and the index of the award is returned; otherwise, continue to accumulate the probability of the next award until the drawn award is found or the sum of the probabilities of all awards is greater than or equal to 1.


        If the gifts for the first prize have been sent out, then the first prize cannot be drawn again during the lottery draw. In this case, the probability of the first prize can be set to 0, and then the sum of the probabilities of all prizes can be recalculated, and then the lottery can be drawn.

public static int lottery(double[] prob, boolean[] isPrizeAvailable) {
    double rand = Math.random();
    double cumulativeProb = 0.0;
    for (int i = 0; i < prob.length; i++) {
        if (!isPrizeAvailable[i]) { // 如果该奖项的礼品已经送完,则概率设置为0
            prob[i] = 0.0;
        }
        cumulativeProb += prob[i];
    }

    if (cumulativeProb == 0.0) { // 如果所有奖项的概率都为0,则返回-1表示抽奖失败
        return -1;
    }

    for (int i = 0; i < prob.length; i++) {
        prob[i] /= cumulativeProb; // 重新计算每个奖项的概率
    }

    cumulativeProb = 0.0;
    for (int i = 0; i < prob.length; i++) {
        cumulativeProb += prob[i];
        if (rand < cumulativeProb) {
            isPrizeAvailable[i] = false; // 将抽中的奖项标记为已送完
            return i;
        }
    }
    return -1;
}

        If the sum of all prize probabilities in the `prob` array is equal to 1, the algorithm will not return -1 because there will always be a prize drawn during the lottery. If the sum of the probabilities of all prizes is less than 1, the algorithm returns -1, indicating that the draw has failed.

In the code, if the sum of the probabilities of all prizes is equal to 0, -1 will be returned directly, indicating that the lottery has failed. If the sum of the probabilities is greater than 0, the sum of the probabilities of each award will be calculated one by one in the loop, compared with the random number, and the corresponding award will be returned.

Guess you like

Origin blog.csdn.net/qq_26112725/article/details/129932317