All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.schoolwow.quickdao.util.ParametersUtil Maven / Gradle / Ivy

There is a newer version: 5.3.1
Show newest version
package cn.schoolwow.quickdao.util;

import cn.schoolwow.quickdao.domain.entity.Property;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Collection;

public class ParametersUtil {
    private static Logger logger = LoggerFactory.getLogger(ParametersUtil.class);
    /**
     * SQL参数占位符
     */
    private static final String PLACEHOLDER = "** NOT SPECIFIED **";
    /**
     * 格式化旧版本的java.sql.Date类型
     */
    private final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 格式化旧版本的java.sql.Time类型
     */
    private final static SimpleDateFormat simpleTimeFormat = new SimpleDateFormat("HH:mm:ss");
    /**
     * 格式化旧版本的Timestampt类型
     */
    private final static SimpleDateFormat simpleDateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
    /**
     * 格式化日期参数
     */
    private final static DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    /**
     * 格式化日期参数
     */
    private final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * 设置字段值为当前日期
     *
     * @param property 字段属性
     * @param instance 实例
     */
    public static void setCurrentDateTime(Property property, Object instance) {
        Field field = getFieldFromInstance(instance, property.name);
        try {
            Object value = field.get(instance);
            //当用户手动设置该字段时,则程序不再注入时间
            if (null != value) {
                return;
            }
            switch (property.className) {
                case "java.util.Date": {
                    field.set(instance, new java.util.Date(System.currentTimeMillis()));
                }
                break;
                case "java.sql.Date": {
                    field.set(instance, new Date(System.currentTimeMillis()));
                }
                break;
                case "java.sql.Timestamp": {
                    field.set(instance, new Timestamp(System.currentTimeMillis()));
                }
                break;
                case "java.util.Calendar": {
                    field.set(instance, Calendar.getInstance());
                }
                break;
                case "java.time.LocalDate": {
                    field.set(instance, LocalDate.now());
                }
                break;
                case "java.time.LocalDateTime": {
                    field.set(instance, LocalDateTime.now());
                }
                break;
                default: {
                    throw new IllegalArgumentException("不支持该日期类型,目前支持的类型为Date,Calendar,LocalDate,LocalDateTime!当前类型:" + property.className);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("设置当前时间异常", e);
        }
    }

    /**
     * 替换SQL语句
     *
     * @param sql        语句
     * @param parameters 参数
     */
    public static String getFormatSQL(String sql, Collection parameters) {
        if(null==parameters||parameters.isEmpty()){
            return sql;
        }
        //判断参数个数是否匹配
        int parameterCount = 0;
        for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy