新增,时间问题解决了
This commit is contained in:
parent
c6ce8eb246
commit
9dab03231f
@ -0,0 +1,4 @@
|
|||||||
|
package com.example.demo.common.base;
|
||||||
|
|
||||||
|
public interface BaseListener {
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.demo.common.base.defaults;
|
package com.example.demo.common.base.startup.defaults;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.demo.common.executor;
|
package com.example.demo.common.base.startup.executor;
|
||||||
|
|
||||||
import com.example.demo.common.base.BaseStartupOutput;
|
import com.example.demo.common.base.BaseStartupOutput;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.demo.common.output;
|
package com.example.demo.common.base.startup.output;
|
||||||
|
|
||||||
import com.example.demo.common.base.BaseStartupOutput;
|
import com.example.demo.common.base.BaseStartupOutput;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.demo.common.output;
|
package com.example.demo.common.base.startup.output;
|
||||||
|
|
||||||
import com.example.demo.common.base.BaseStartupOutput;
|
import com.example.demo.common.base.BaseStartupOutput;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.demo.common.config;
|
package com.example.demo.common.config.database;
|
||||||
|
|
||||||
import com.example.demo.common.constant.GlobalConstants;
|
import com.example.demo.common.constant.GlobalConstants;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
@ -1,10 +1,9 @@
|
|||||||
package com.example.demo.common.config;
|
package com.example.demo.common.config.database.initializer;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.boot.CommandLineRunner;
|
import org.springframework.boot.CommandLineRunner;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.jdbc.datasource.init.ScriptUtils;
|
import org.springframework.jdbc.datasource.init.ScriptUtils;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.demo.common.config;
|
package com.example.demo.common.config.database.real;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.demo.common.config;
|
package com.example.demo.common.config.database.real;
|
||||||
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
|
@ -0,0 +1,69 @@
|
|||||||
|
package com.example.demo.common.config.orm.handler;
|
||||||
|
|
||||||
|
import org.apache.ibatis.type.JdbcType;
|
||||||
|
import org.apache.ibatis.type.TypeHandler;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SQLite 日期类型处理器
|
||||||
|
* 将 Java Date 对象转换为 SQLite 支持的日期字符串格式
|
||||||
|
*
|
||||||
|
* @author 岳佳君 (2025年09月24日 17:40:34)
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class SqliteDateTypeHandler implements TypeHandler<Date> {
|
||||||
|
|
||||||
|
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException {
|
||||||
|
if (parameter == null) {
|
||||||
|
ps.setNull(i, Types.VARCHAR);
|
||||||
|
} else {
|
||||||
|
// 将 Date 转换为 SQLite 支持的日期字符串格式
|
||||||
|
String dateString = DATE_FORMAT.format(parameter);
|
||||||
|
ps.setString(i, dateString);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Date getResult(ResultSet rs, String columnName) throws SQLException {
|
||||||
|
String dateString = rs.getString(columnName);
|
||||||
|
return parseDate(dateString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Date getResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||||
|
String dateString = rs.getString(columnIndex);
|
||||||
|
return parseDate(dateString);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Date getResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||||
|
String dateString = cs.getString(columnIndex);
|
||||||
|
return parseDate(dateString);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析日期字符串为 Date 对象
|
||||||
|
*
|
||||||
|
* @param dateString 日期字符串
|
||||||
|
* @return Date 对象
|
||||||
|
*/
|
||||||
|
private Date parseDate(String dateString) {
|
||||||
|
if (dateString == null || dateString.trim().isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 尝试解析 yyyy-MM-dd HH:mm:ss 格式
|
||||||
|
return DATE_FORMAT.parse(dateString);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 如果解析失败,尝试其他格式或返回当前时间
|
||||||
|
return new Date();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
package com.example.demo.common.config.orm.listener;
|
||||||
|
|
||||||
|
import com.mybatisflex.annotation.InsertListener;
|
||||||
|
import com.mybatisflex.annotation.UpdateListener;
|
||||||
|
import com.example.demo.common.domain.BaseEntity;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 时间戳监听器
|
||||||
|
* 用于在插入和更新操作时自动设置创建时间和更新时间
|
||||||
|
*
|
||||||
|
* @author 岳佳君 (2025年09月24日 17:40:34)
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class FlexListener implements InsertListener, UpdateListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 插入前监听
|
||||||
|
* 设置创建时间和更新时间
|
||||||
|
*
|
||||||
|
* @param entity 实体对象
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onInsert(Object entity) {
|
||||||
|
log.info("--------------------------------- onInsert");
|
||||||
|
if (entity instanceof BaseEntity baseEntity) {
|
||||||
|
Date now = new Date();
|
||||||
|
|
||||||
|
// 设置创建时间(如果为空)
|
||||||
|
if (baseEntity.getCreatedTime() == null) {
|
||||||
|
baseEntity.setCreatedTime(now);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置更新时间
|
||||||
|
baseEntity.setUpdatedTime(now);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新前监听
|
||||||
|
* 设置更新时间
|
||||||
|
*
|
||||||
|
* @param entity 实体对象
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void onUpdate(Object entity) {
|
||||||
|
log.info("--------------------------------- onUpdate");
|
||||||
|
if (entity instanceof BaseEntity baseEntity) {
|
||||||
|
// 设置更新时间
|
||||||
|
baseEntity.setUpdatedTime(new Date());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,11 @@
|
|||||||
package com.example.demo.common.domain;
|
package com.example.demo.common.domain;
|
||||||
|
|
||||||
|
import com.example.demo.common.config.orm.handler.SqliteDateTypeHandler;
|
||||||
|
import com.example.demo.common.config.orm.listener.FlexListener;
|
||||||
import com.mybatisflex.annotation.Column;
|
import com.mybatisflex.annotation.Column;
|
||||||
import com.mybatisflex.annotation.Id;
|
import com.mybatisflex.annotation.Id;
|
||||||
import com.mybatisflex.annotation.KeyType;
|
import com.mybatisflex.annotation.KeyType;
|
||||||
|
import com.mybatisflex.annotation.Table;
|
||||||
import com.mybatisflex.core.keygen.KeyGenerators;
|
import com.mybatisflex.core.keygen.KeyGenerators;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
@ -30,6 +33,7 @@ import java.util.Date;
|
|||||||
@Accessors(chain=true)
|
@Accessors(chain=true)
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@Table(value = "demo", onInsert = FlexListener.class, onUpdate = FlexListener.class)
|
||||||
public class BaseEntity implements Serializable {
|
public class BaseEntity implements Serializable {
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
@ -74,8 +78,13 @@ public class BaseEntity implements Serializable {
|
|||||||
* <hr/>
|
* <hr/>
|
||||||
* Column(onInsertValue = "now()")
|
* Column(onInsertValue = "now()")
|
||||||
* Column(onInsertValue = "CURRENT_TIMESTAMP")
|
* Column(onInsertValue = "CURRENT_TIMESTAMP")
|
||||||
|
* Column(onInsertValue = "datetime('now', 'localtime')")
|
||||||
*/
|
*/
|
||||||
@Column(onInsertValue = "CURRENT_TIMESTAMP")
|
// @Column(onInsertValue = "datetime('now', 'localtime')")
|
||||||
|
/**
|
||||||
|
* 创建时间
|
||||||
|
*/
|
||||||
|
@com.mybatisflex.annotation.Column(typeHandler = SqliteDateTypeHandler.class)
|
||||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||||
@Schema(description = "创建时间")
|
@Schema(description = "创建时间")
|
||||||
@ -93,8 +102,13 @@ public class BaseEntity implements Serializable {
|
|||||||
* <hr/>
|
* <hr/>
|
||||||
* Column(onUpdateValue = "now()", onInsertValue = "now()")
|
* Column(onUpdateValue = "now()", onInsertValue = "now()")
|
||||||
* Column(onUpdateValue = "CURRENT_TIMESTAMP", onInsertValue = "CURRENT_TIMESTAMP")
|
* Column(onUpdateValue = "CURRENT_TIMESTAMP", onInsertValue = "CURRENT_TIMESTAMP")
|
||||||
|
* Column(onUpdateValue = "datetime('now', 'localtime')", onInsertValue = "datetime('now', 'localtime')")
|
||||||
*/
|
*/
|
||||||
@Column(onUpdateValue = "CURRENT_TIMESTAMP", onInsertValue = "CURRENT_TIMESTAMP")
|
// @Column(onUpdateValue = "datetime('now', 'localtime')", onInsertValue = "datetime('now', 'localtime')")
|
||||||
|
/**
|
||||||
|
* 更新时间
|
||||||
|
*/
|
||||||
|
@com.mybatisflex.annotation.Column(typeHandler = SqliteDateTypeHandler.class)
|
||||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||||
@Schema(description = "更新时间")
|
@Schema(description = "更新时间")
|
||||||
|
@ -72,7 +72,7 @@ public abstract class BaseServiceImpl<
|
|||||||
BeanUtil.copyProperties(dto, entity);
|
BeanUtil.copyProperties(dto, entity);
|
||||||
// 强制新增,将ID置为空
|
// 强制新增,将ID置为空
|
||||||
entity.setId(null);
|
entity.setId(null);
|
||||||
// 新增数据
|
// 新增数据(时间戳由监听器自动设置)
|
||||||
save(entity);
|
save(entity);
|
||||||
// 仅返回数据ID
|
// 仅返回数据ID
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
@ -93,7 +93,7 @@ public abstract class BaseServiceImpl<
|
|||||||
public Boolean update(DTO dto) {
|
public Boolean update(DTO dto) {
|
||||||
Entity entity = createEntity();
|
Entity entity = createEntity();
|
||||||
BeanUtil.copyProperties(dto, entity);
|
BeanUtil.copyProperties(dto, entity);
|
||||||
// 更新数据
|
// 更新数据(时间戳由监听器自动设置)
|
||||||
return updateById(entity);
|
return updateById(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.example.demo.parser.entity;
|
package com.example.demo.parser.entity;
|
||||||
|
|
||||||
import com.example.demo.common.domain.BaseEntity;
|
import com.example.demo.common.domain.BaseEntity;
|
||||||
|
import com.example.demo.common.config.orm.listener.FlexListener;
|
||||||
import com.mybatisflex.annotation.Table;
|
import com.mybatisflex.annotation.Table;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
@ -25,7 +26,7 @@ import java.util.Date;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@Table("file_record")
|
@Table(value = "file_record", onInsert = FlexListener.class, onUpdate = FlexListener.class)
|
||||||
public class FileRecordEntity extends BaseEntity {
|
public class FileRecordEntity extends BaseEntity {
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.example.demo.parser.entity;
|
package com.example.demo.parser.entity;
|
||||||
|
|
||||||
import com.example.demo.common.domain.BaseEntity;
|
import com.example.demo.common.domain.BaseEntity;
|
||||||
|
import com.example.demo.common.config.orm.listener.FlexListener;
|
||||||
import com.mybatisflex.annotation.Table;
|
import com.mybatisflex.annotation.Table;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
@ -24,7 +25,7 @@ import java.io.Serial;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@Table("mapper_rule")
|
@Table(value = "mapper_rule", onInsert = FlexListener.class, onUpdate = FlexListener.class)
|
||||||
public class MapperRuleEntity extends BaseEntity {
|
public class MapperRuleEntity extends BaseEntity {
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.example.demo.parser.entity;
|
package com.example.demo.parser.entity;
|
||||||
|
|
||||||
import com.example.demo.common.domain.BaseEntity;
|
import com.example.demo.common.domain.BaseEntity;
|
||||||
|
import com.example.demo.common.config.orm.listener.FlexListener;
|
||||||
import com.mybatisflex.annotation.Table;
|
import com.mybatisflex.annotation.Table;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
@ -24,7 +25,7 @@ import java.io.Serial;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@EqualsAndHashCode(callSuper = true)
|
@EqualsAndHashCode(callSuper = true)
|
||||||
@Table("parse_rule")
|
@Table(value = "parse_rule", onInsert = FlexListener.class, onUpdate = FlexListener.class)
|
||||||
public class ParseRuleEntity extends BaseEntity {
|
public class ParseRuleEntity extends BaseEntity {
|
||||||
|
|
||||||
@Serial
|
@Serial
|
||||||
|
Loading…
x
Reference in New Issue
Block a user