java, mysql trigger, redis serial number generated (yyyyMM000)

Recently encountered need to generate a date serial number of the business, and then recorded several generation method is through a java code, is a database trigger, as well as through redis. Here is the code:

Simple water generated by the java:

    /**
     * 通过日期和生成的流水号拼接
     * @param maxCount 已经生成的个数
     * @return
     */
    public static String recountNew(int maxCount) {
        if (maxCount < 0) {
            return null;
        }
        //201707999
        String str = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMM"));
        String countStr = str + num(maxCount, 3, 3);
        System.out.println("合同编号: " + Long.valueOf(countStr));
        return countStr;
    }

    /**
     * 生成流水号
     * @param current 当前生成个数
     * @param max 最大整数位
     * @param min 最小整数位
     * @return 生成的流水号
     */
    public static String num(int current, int max, int min) {
        current++;
        NumberFormat numberFormat = NumberFormat.getInstance();
        //设置是否使用分组
        numberFormat.setGroupingUsed(false);
        //设置最大整数位数
        numberFormat.setMaximumIntegerDigits(max);
        //设置最小整数位数
        numberFormat.setMinimumIntegerDigits(min);
        return numberFormat.format(current);
    }

Data Triggers achieved by:

The main logic: for example to 201 906 001, 201 606 to obtain a serial number based on the current maximum of date, save to n. Then add a serial number, the current date, and then stitched together 201906

        CREATE TABLE orders (
            orders_id INT (10) PRIMARY KEY,
            customer_name VARCHAR (100)
        );

        DROP TRIGGER tr_orders_id;

        CREATE TRIGGER tr_orders_id BEFORE INSERT ON orders FOR EACH ROW
        BEGIN
            DECLARE
                n INT;
        SELECT
            IFNULL(max(RIGHT(orders_id, 3)), 0) INTO n
        FROM
            orders
        WHERE
            mid(orders_id, 1, 6) = DATE_FORMAT(now(), '%Y%m');
        SET NEW.orders_id = concat(
            DATE_FORMAT(now(), '%Y%m'),
            RIGHT (001 + n, 3)
        );
        END;

        INSERT INTO orders VALUES (0, 'jack');
        INSERT INTO orders VALUES (0, 'jack');

redis implementation (use)

The main use StringRedisTemplate to operate redis, write in the business layer, requires the use of direct injection on the line. This is no detailed description of the configuration StringRedisTemplate, the following codes there will be problems, but know this method is useful when, to write their own just fine.


/**
 * @version V1.0
 * @Authoer CX
 * @Since:2019/5/20
 */
public interface NumberGenService {

    /**
     * 根据code生成编号
     * 例:NB000001
     * @param code 前缀
     * @return 编号
     */
    String generateNumber(String code);

    /**
     * 根据code及年月生成编号
     * 例子:NB201905000001
     * @param code 前缀
     * @return 编号
     */
    String generateNumberByMonth(String code);


    /**
     * 根据code及年月生成编号
     * 例子:NB20190508000001
     * @param code 前缀
     * @return 编号
     */
    String generateNumberByDay (String code);
}
import com.cloudkeeper.confinement.main.service.NumberGenService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @version V1.0
 * @Authoer CX
 * @Since:2019/5/20
 */
@Service
public class NumberGenServiceImpl implements NumberGenService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final int LENGTH = 6;

    private static final String MONTH_FORMAT = "yyyyMM";

    private static final String DAY_FORMAT = "yyyyMMdd";

    public String generateNumber (String code) {
        return getNumber(code, "");
    }

    public String generateNumberByMonth (String code) {
        return getNumber(code, new SimpleDateFormat(MONTH_FORMAT).format(new Date()));
    }

    public String generateNumberByDay (String code) {
        return getNumber(code, new SimpleDateFormat(DAY_FORMAT).format(new Date()));
    }


    private String getNumber(String code, String month) {
        code += month;
        Long number = stringRedisTemplate.opsForValue().increment("" + ":" + code);
        return code + StringUtils.leftPad(number.toString(), LENGTH, '0');
    }

}

to sum up

Above is a summary of several implementations, the company uses a self-energizing redis to achieve, generate the same number of problems can avoid concurrency. By generating java Problems occur when you save the same number.

Guess you like

Origin www.cnblogs.com/black-spike/p/10986260.html