springboot关于mysql数据库date时间的坑

默认存储是"yyyy-MM-dd’T’hh:mm:ss.SSSZ"类型的格式,如果想存储"yyyy-MM-dd HH:mm:ss"类型的时间,需要在实体类时间参数上面加上两行代码

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

在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用Spring Boot实现MySQL增量备份的示例代码: ```java @Component public class IncrementalBackupTask { @Value("${database.backup.path}") private String backupPath; @Autowired private DataSource dataSource; private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); @Scheduled(cron = "${database.backup.cron}") public void backupDatabase() { try { File backupFolder = new File(backupPath); if (!backupFolder.exists() || !backupFolder.isDirectory()) { backupFolder.mkdirs(); } String backupFileName = "incremental-backup-" + DATE_FORMAT.format(new Date()) + ".sql"; File backupFile = new File(backupFolder, backupFileName); String lastBackupFileName = getLastBackupFileName(); if (lastBackupFileName == null) { fullBackup(backupFile); } else { incrementalBackup(lastBackupFileName, backupFile); } jdbcTemplate.update("INSERT INTO backup_history (file_name, backup_time) VALUES (?, ?)", backupFileName, new Date()); } catch (Exception e) { e.printStackTrace(); } } private void fullBackup(File backupFile) throws Exception { ProcessBuilder processBuilder = new ProcessBuilder( "mysqldump", "--skip-comments", "--skip-triggers", "-u" + getUserName(), "-p" + getPassword(), "--databases", getDatabaseName(), "--result-file=" + backupFile.getAbsolutePath()); processBuilder.environment().put("MYSQL_PWD", getPassword()); Process process = processBuilder.start(); int exitCode = process.waitFor(); if (exitCode != 0) { throw new RuntimeException("Failed to backup database"); } } private void incrementalBackup(String lastBackupFileName, File backupFile) throws Exception { String binLogFileName = getBinLogFileName(lastBackupFileName); ProcessBuilder processBuilder = new ProcessBuilder( "mysqlbinlog", "--start-position=4", "--base64-output=DECODE-ROWS", "-u" + getUserName(), "-p" + getPassword(), getBinLogFile(lastBackupFileName), "|", "mysql", "-u" + getUserName(), "-p" + getPassword(), "--database=" + getDatabaseName()); processBuilder.environment().put("MYSQL_PWD", getPassword()); Process process = processBuilder.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { Files.write(backupFile.toPath(), (line + "\n").getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND); } int exitCode = process.waitFor(); if (exitCode != 0) { throw new RuntimeException("Failed to backup database incrementally"); } } private String getLastBackupFileName() { String sql = "SELECT file_name FROM backup_history ORDER BY backup_time DESC LIMIT 1"; List<String> fileNames = jdbcTemplate.queryForList(sql, String.class); return fileNames.isEmpty() ? null : fileNames.get(0); } private String getBinLogFileName(String lastBackupFileName) throws Exception { String sql = "SHOW MASTER STATUS"; List<Map<String, Object>> rows = jdbcTemplate.queryForList(sql); if (!rows.isEmpty()) { Map<String, Object> row = rows.get(0); String binLogFileName = (String) row.get("File"); if (binLogFileName != null) { String position = (String) row.get("Position"); if (position != null) { return binLogFileName; } } } throw new RuntimeException("Failed to get bin log file name"); } private String getBinLogFile(String lastBackupFileName) { return backupPath + "/mysql-bin." + getBinLogNumber(lastBackupFileName); } private int getBinLogNumber(String lastBackupFileName) { String[] parts = lastBackupFileName.split("\\."); return Integer.parseInt(parts[2]); } private String getUserName() { return dataSource.getUsername(); } private String getPassword() { return dataSource.getPassword(); } private String getDatabaseName() { return dataSource.getConnection().getCatalog(); } } ``` 在上面的代码中,我们使用了Spring Boot的定时任务注解`@Scheduled`来定期执行备份任务。在备份任务中,我们首先检查上一个备份文件是否存在,如果不存在,则执行完整备份。否则,我们通过解析上一个备份文件的名称来获取上一个备份文件的binlog文件名称和位置。然后,我们使用`mysqlbinlog`命令来获取从上一个备份文件后发生的所有更改,并将它们追加到新的备份文件中。最后,我们使用JdbcTemplate将备份历史记录保存在数据库中。请注意,这里我们使用了MySQL,如果您使用其他数据库,需要相应地更改备份命令。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值