保存到 mysql 数据库时,值从双精度更改为整数

Values changes from double to integer when saving to mysql database

实体: 模拟:

package Simulations.Entity;

import lombok.*;

import javax.persistence.*;
import java.util.List;

@Builder
@Table
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Simulation {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    private Long id;

    @Column(name = "N")
    private String simulation_Name;

    @Column(name = "P")
    private Double population_Size;

    @Column(name = "I")
    private Double initial_Infected_Number;

    @Column(name = "R")
    private Double how_Many_One_Infects;

    @Column(name = "M")
    private Double mortality_Rate;

    @Column(name = "Ti")
    private Double number_Of_Days_To_Recovery;

    @Column(name = "Tm")
    private Double number_Of_Days_To_Death;

    @Column(name = "Ts")
    private Double simulation_Time;

    @OneToMany(mappedBy = "simulation", cascade = CascadeType.REMOVE)
    private List<SimulationsValues> simulationsValues;
}

模拟值:

package Simulations.Entity;

import lombok.*;

import javax.persistence.*;

@Builder
@Table
@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class SimulationsValues {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column
    private Long id;

    @Column(name = "Day")
    private Double day;

    @Column(name = "Pi")
    private Double number_Of_Infected;

    @Column(name = "Pv")
    private Double healthy_Prone_To_Infection;

    @Column(name = "Pm")
    private Double dead;

    @Column(name = "Pr")
    private Double regained_Health_And_Immunity;

    @ManyToOne
    @JoinColumn(name = "simulation_id", referencedColumnName = "id")
    private Simulation simulation;
}

逻辑类:

package Simulations.Services;

import Simulations.Entity.Simulation;
import Simulations.Entity.SimulationsValues;
import Simulations.Repositories.SimulationRepository;
import Simulations.Repositories.SimulationsValuesRepository;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Component
@Aspect
public class EachRemainingDayService {


    private final SimulationRepository simulationRepository;
    private final SimulationsValuesRepository simulationsValuesRepository;


    @Pointcut("@annotation(Simulations.Annotations.MyAnnotation)")
    public void MyAnnotationMethod() {
    }


    public List<Simulation> getSimulations() {
        List<Simulation> listOdIds = new ArrayList<>(simulationRepository.findAll());
        listOdIds.sort(Comparator.comparing(Simulation::getId));
        return listOdIds;
    }

    public List<SimulationsValues> getSimulationsValues() {
        List<SimulationsValues> listOdIdsOfValues = new ArrayList<>(simulationsValuesRepository.findAll());
        listOdIdsOfValues.sort(Comparator.comparing(SimulationsValues::getId));
        return listOdIdsOfValues;
    }


    //3 zapis wynikow do bazy
    @After("MyAnnotationMethod()")// zmienilem z Before
    public void SimulationParametersForRemainingDays() {


        var sim = getSimulations();
        var newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());

        var simVal = getSimulationsValues();
        var newestSimulationsVal = simulationsValuesRepository
                .getById(simVal.get(simVal.size() - 1).getId());


        //zapisanie do bazy TO LOGIKA!!!
        for (double i = 2; i <= newestSimulation.getSimulation_Time(); i++) {
            if (newestSimulationsVal.getHealthy_Prone_To_Infection() <= 0) {
                break;
            }

            if (i >= newestSimulation.getNumber_Of_Days_To_Death()
                    && i < newestSimulation.getNumber_Of_Days_To_Recovery()) {

                sim = getSimulations();
                newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());

                simVal = getSimulationsValues();
                newestSimulationsVal = simulationsValuesRepository
                        .getById(simVal.get(simVal.size() - 1).getId());

                SimulationsValues simulation_values;
                Simulation finalNewestSimulation1 = newestSimulation;
                double finalI = i;



                simulation_values = SimulationsValues.builder()
                        .day(i)
                        .healthy_Prone_To_Infection(newestSimulation.getPopulation_Size() -
                                (((newestSimulation.getHow_Many_One_Infects())
                                        * newestSimulationsVal.getNumber_Of_Infected()
                                        + newestSimulationsVal.getNumber_Of_Infected())))


                        .number_Of_Infected((((newestSimulation.getHow_Many_One_Infects())
                                * newestSimulationsVal.getNumber_Of_Infected()
                                + newestSimulationsVal.getNumber_Of_Infected())//L24
                                - (newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository
                                .findAll().stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                        .getNumber_Of_Days_To_Death()))
                                .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
                                        .filter(e -> e.getDay() == (finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Death()))).count() - 1).getNumber_Of_Infected()))))//V24


                        .regained_Health_And_Immunity(0d)// Z24


                        .dead((newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository
                                .findAll().stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                        .getNumber_Of_Days_To_Death()))
                                .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
                                        .filter(e -> e.getDay() == (finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Death()))).count() - 1).getNumber_Of_Infected()))
                                - newestSimulationsVal.getDead())//V24

                        .build();


                var simId = simulationsValuesRepository.save(simulation_values);
                simId.setSimulation(newestSimulation);//dodane
                var simVal2 = newestSimulation.getSimulationsValues();
                simVal2.add(simId);
                newestSimulation.setSimulationsValues(simVal2);
                simulationRepository.save(newestSimulation);

            } else if (i >= newestSimulation.getNumber_Of_Days_To_Recovery()) {

                sim = getSimulations();
                newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());

                simVal = getSimulationsValues();
                newestSimulationsVal = simulationsValuesRepository
                        .getById(simVal.get(simVal.size() - 1).getId());


                Simulation finalNewestSimulation1 = newestSimulation;
                double finalI = i;
                SimulationsValues simulation_values;


                simulation_values = SimulationsValues.builder()
                        .day(i)
                        .healthy_Prone_To_Infection(newestSimulation.getPopulation_Size() -
                                ((newestSimulation.getHow_Many_One_Infects())
                                        * newestSimulationsVal.getNumber_Of_Infected()))


                        .number_Of_Infected(((newestSimulation.getHow_Many_One_Infects())
                                * newestSimulationsVal.getNumber_Of_Infected()
                                + newestSimulationsVal.getNumber_Of_Infected())//L24

                                - (newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository
                                .findAll().stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                        .getNumber_Of_Days_To_Death()))
                                .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
                                        .filter(e -> e.getDay() == (finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Death()))).count() - 1).getNumber_Of_Infected())//V24

                                - (((1 - newestSimulation.getMortality_Rate()) * (simulationsValuesRepository
                                .findAll().stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                        .getNumber_Of_Days_To_Recovery()))
                                .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
                                        .filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Recovery())).count() - 1)
                                .getNumber_Of_Infected()))//Z24
                        ))


                        .regained_Health_And_Immunity((1 - newestSimulation.getMortality_Rate()) *
                                (simulationsValuesRepository.findAll()
                                        .stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Recovery()))
                                        .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll()
                                        .stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                                .getNumber_Of_Days_To_Recovery())).count() - 1)
                                        .getNumber_Of_Infected())// Z24


                        .dead((newestSimulation.getMortality_Rate() * (double) ((simulationsValuesRepository.findAll()
                                .stream().filter(e -> e.getDay() == finalI + 1 - finalNewestSimulation1
                                        .getNumber_Of_Days_To_Death()))
                                .collect(Collectors.toList()).get((int) (simulationsValuesRepository.findAll().stream()
                                        .filter(e -> e.getDay() == (finalI + 1 - finalNewestSimulation1
                                             .getNumber_Of_Days_To_Death()))).count()1).getNumber_Of_Infected()))
                                - newestSimulationsVal.getDead())//V24

                        .build();


                var simId = simulationsValuesRepository.save(simulation_values);
                simId.setSimulation(newestSimulation);//dodane
                var simVal2 = newestSimulation.getSimulationsValues();
                simVal2.add(simId);
                newestSimulation.setSimulationsValues(simVal2);
                simulationRepository.save(newestSimulation);

            } else if (i == 2) {
                sim = getSimulations();
                newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());

                simVal = getSimulationsValues();
                newestSimulationsVal = simulationsValuesRepository
                        .getById(simVal.get(simVal.size() - 1).getId());


                SimulationsValues simulation_values;
                simulation_values = SimulationsValues.builder()
                        .day(i)
                        .healthy_Prone_To_Infection(newestSimulationsVal.getHealthy_Prone_To_Infection() -
                                ((newestSimulation.getHow_Many_One_Infects())
                                        * newestSimulationsVal.getNumber_Of_Infected()))
                        .number_Of_Infected((newestSimulation.getHow_Many_One_Infects())
                                * newestSimulationsVal.getNumber_Of_Infected()
                                + newestSimulationsVal.getNumber_Of_Infected())
                        .regained_Health_And_Immunity(0d)
                        .dead(0d)
                        .build();

                var simId2 = simulationsValuesRepository.save(simulation_values);
                simId2.setSimulation(newestSimulation);//dodane
                var simVal2 = newestSimulation.getSimulationsValues();
                simVal2.add(simId2);
                newestSimulation.setSimulationsValues(simVal2);
                simulationRepository.save(newestSimulation);

            } else {
                sim = getSimulations();
                newestSimulation = simulationRepository.getById(sim.get(sim.size() - 1).getId());

                simVal = getSimulationsValues();
                newestSimulationsVal = simulationsValuesRepository
                        .getById(simVal.get(simVal.size() - 1).getId());


                SimulationsValues simulation_values;
                simulation_values = SimulationsValues.builder()
                        .day(i)
                        .healthy_Prone_To_Infection(newestSimulationsVal.getHealthy_Prone_To_Infection() -
                                ((newestSimulation.getHow_Many_One_Infects())
                                        * newestSimulationsVal.getNumber_Of_Infected()))
                        .number_Of_Infected((newestSimulation.getHow_Many_One_Infects())
                                * newestSimulationsVal.getNumber_Of_Infected()
                                + newestSimulationsVal.getNumber_Of_Infected())
                        .regained_Health_And_Immunity(0d)
                        .dead(0d)
                        .build();

                var simId2 = simulationsValuesRepository.save(simulation_values);
                simId2.setSimulation(newestSimulation);//dodane
                var simVal2 = newestSimulation.getSimulationsValues();
                simVal2.add(simId2);
                newestSimulation.setSimulationsValues(simVal2);
                simulationRepository.save(newestSimulation);

            }
        }

    }
}


视图模型:


package Simulations.ViewModels;

import Simulations.Annotations.GreaterThan;
import Simulations.Entity.SimulationsValues;
import lombok.*;

import javax.validation.constraints.DecimalMax;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

@Builder
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@GreaterThan(message = "field number of days to death can not be equal or bigger than field days to recovery")
public class SimulationViewModel {

    private Long id;

    @NotEmpty(message = "field can not be empty")
    private String simulation_Name;

    @NotNull(message = "field can not be empty")
    @DecimalMax("10000000000.0")
    @DecimalMin("0.0")
    private Double population_Size;

    private Double initial_Infected_Number;

    @NotNull(message = "field can not be empty")
    private Double how_Many_One_Infects;

    @NotNull(message = "field can not be empty")
    @DecimalMax("1.0")
    @DecimalMin("0.001")
    private Double mortality_Rate;

    private @NotNull(message = "field can not be empty") @DecimalMin("1.0")
    Double number_Of_Days_To_Recovery;

    private @NotNull(message = "field can not be empty") @DecimalMin("1.0")
    Double number_Of_Days_To_Death;

    private @NotNull(message = "field can not be empty")
    Double simulation_Time;

    List<SimulationsValues> simulationsValues = new ArrayList<>();
}

嗨,我有一个问题。当我创建一个 simulationValues 对象(使用构建器)时,它的字段具有预期的双精度值,但是当我将该对象保存到数据库并查看数据库中的这些值时(MySql),这些值显示为整数,即逗号后没有值,例如,当我调试我的代码时,我看到“死”字段在 12 次迭代中的值为 0.9,但它作为 1 保存到数据库中。这是为什么?我不做任何转换,我用于此计算的所有字段都是双精度,所有字段 n SimulationValues class 和 SimulationViewModel 都是双精度。为什么在保存字段时更改类型?提前致谢

MySql 中的所有列都是 bigint 类型而不是 double 类型,因为我在相应的实体中创建它们,将它们更改为 BigDecimal 并且一切正常。