Java 如何根据服务器时间操作mySQL中的参数

Java 如何根据服务器时间操作mySQL中的参数,java,mysql,hibernate,spring-mvc,Java,Mysql,Hibernate,Spring Mvc,我已经用Spring/hibernate/mySQL构建了一个RESTful服务器和一个数据库 我有一个银行储蓄账户表,有3个储蓄余额栏 例如: account_id | a_savings | b_savings | c_savings 1 | 100 | 200 | 300 我希望每天(或每月),每个储蓄账户将根据服务器/当前时间自动向其价值增加0.01%(或其他金额) 我该怎么做呢?编写一个暂停一段时间的方法,而不是调用一个暂停一段时间的

我已经用Spring/hibernate/mySQL构建了一个RESTful服务器和一个数据库

我有一个银行储蓄账户表,有3个储蓄余额栏

例如:

account_id | a_savings | b_savings | c_savings  
1          | 100       | 200       | 300  
我希望每天(或每月),每个储蓄账户将根据服务器/当前时间自动向其价值增加0.01%(或其他金额)


我该怎么做呢?

编写一个暂停一段时间的方法,而不是调用一个暂停一段时间的方法

  • 通过SQL加载字段的当前值
  • 通过计算字段的值,
    value=value+(value*0.01)
    增加字段的值,然后使用SQL更新值

  • 对于java中的暂停,使用
    TimeUnit.MINUTES.sleep(2)
    of
    java.util.concurrent.TimeUnit
    这里的
    2
    代表
    2分钟
    。您还可以使用天/小时
    TimeUnit.DAYS.sleep(1)

    通过调度任务解决了此问题:

    @Component
    public class ScheduledTasks {
    
    private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
    
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    
    @Autowired
    private SavingsAccountDao savingsAccountDao;
    
    /* each day at 8 am */
    @Scheduled(cron = "0 0 8 * * *")
    public void increaseSavingsAccount() {
        log.info("Savings Accounts Updated", dateFormat.format(new Date()));
    
        /* get all savings accounts and increase balance according to the interest */
        List<SavingsAccount> savingsAccountList = savingsAccountDao.findAll();
        savingsAccountList.forEach((sa) -> {
    
            /* 39% interest in 12 months */
            sa.setASavingsBalance(sa.getASavingsBalance().multiply(new BigDecimal(1.0009)));
        });
    }
    
    @组件
    公共类ScheduledTasks{
    私有静态最终记录器log=LoggerFactory.getLogger(ScheduledTasks.class);
    私有静态最终SimpleDataFormat dateFormat=新SimpleDataFormat(“HH:mm:ss”);
    @自动连线
    私人储蓄;私人储蓄;
    /*每天早上8点*/
    @已计划(cron=“0 8***”)
    public void递增savingsaccount(){
    log.info(“储蓄账户更新”,dateFormat.format(new Date());
    /*获取所有储蓄账户,并根据利息增加余额*/
    List savingsAccountList=savingsAccountDao.findAll();
    savingsAccountList.forEach((sa)->{
    /*12个月内支付39%的利息*/
    sa.setASavingsBalance(sa.getASavingsBalance().multiply(新的BigDecimal(1.0009)));
    });
    }
    
    }