maven mongodb审核在spring Boot 中保存createdDate,lastModifiedDate,createdBy,lastModifiedBy

jljoyd4f  于 2023-08-03  发布在  Maven
关注(0)|答案(3)|浏览(125)

我使用的是 Boot ,因此我没有使用任何xml文件进行配置。我必须做的是EnableMongoAuditing保存createdDate,lastModifiedDate等,同时使用MongoRepository保存数据。
我的模特班

@Component
@Document(collection = "CAPPING")
public class TemporaryCapping extends BaseEntity {

    @Field("contract_id")
    private BigInteger contractId;

    @Field("period_id")
    private BigInteger periodId;

    @Field("user_id")
    private BigInteger userId;

    @Field("amount")
    private Double amount;

    @Field("type_of_capping")
    private TypeOfCapping typeOfCapping;

    public BigInteger getContractId() {
        return contractId;
    }

    public void setContractId(BigInteger contractId) {
        this.contractId = contractId;
    }

    public BigInteger getPeriodId() {
        return periodId;
    }

    public void setPeriodId(BigInteger periodId) {
        this.periodId = periodId;
    }

    public BigInteger getUserId() {
        return userId;
    }

    public void setUserId(BigInteger userId) {
        this.userId = userId;
    }

    public Double getAmount() {
        return amount;
    }

    public void setAmount(Double amount) {
        this.amount = amount;
    }

    public TypeOfCapping getTypeOfCapping() {
        return typeOfCapping;
    }

    public void setTypeOfCapping(TypeOfCapping typeOfCapping) {
        this.typeOfCapping = typeOfCapping;
    }

}


public class BaseEntity implements Serializable{

@Id
@Indexed(unique = true)
private BigInteger id;

@CreatedDate
private DateTime createdDate;

@Field("modified_date")
private BigInteger modifiedDate;

public BigInteger getId() {
    return id;
}

public void setId(BigInteger id) {
    this.id = id;
}

public DateTime getCreatedDate() {
    return createdDate;
}

public void setCreatedDate(DateTime createdDate) {
    this.createdDate = createdDate;
}

public BigInteger getModifiedDate() {
    return modifiedDate;
}

public void setModifiedDate(BigInteger modifiedDate) {
    this.modifiedDate = modifiedDate;
}

字符串
我使用@CreateDate注解保存createDate。我使用了jodatime依赖于DateTime

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.7</version>
</dependency>


spring-data-mongodb也被添加到依赖项中。
这是我的主要应用类

@SpringBootApplication
    @EnableMongoAuditing
   public class Application {

   public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
   }

}


我在这个推动错误的地方,因为日期是没有得到保存在数据库?我还知道,为了保存@createdBy,你需要编写AuditorAware bean,但现在我只是试图保存createdBy。
@EnableMongoAuditing应该在哪里使用?

u2nhd7ah

u2nhd7ah1#

在我的应用程序中,我通过Java代码进行配置。我以这种方式使用@EnableMongAuditing,并为ZonedDateTime创建转换。

@Configuration
@EnableMongoAuditing
@EnableMongoRepositories(basePackages = { BASE_PACKAGE })
public class MongoConfiguration extends AbstractMongoConfiguration {

    public static final String BASE_PACKAGE = "package.with.aggregates";

    @Value("${spring.data.mongodb.uri}")
    private String mongoUri;

    @Value("${spring.data.mongodb.database}")
    private String databaseName;

    @Override
    protected String getDatabaseName() {
        return databaseName;
    }

    @Override
    public Mongo mongo() throws Exception {
        return new MongoClient(new MongoClientURI(mongoUri));
    }

    // Here you must add converters to Joda datetypes. In my solution is ZonedDateTime
    @Override
    public CustomConversions customConversions() {
        List<Converter<?, ?>> converterList = new ArrayList<>();
        converterList.add(new DateToZonedDateTimeConverter());
        converterList.add(new ZonedDateTimeToDateConverter());
        return new CustomConversions(converterList);
    }

    @Override
    protected String getMappingBasePackage() {
        return BASE_PACKAGE;
    }
}

字符串

shyt4zoc

shyt4zoc2#

@EnableMongoAuditing实际上可以放置在配置中的任何位置(在@Configuration annotation旁边)

oo7oh9g9

oo7oh9g93#

简单的解决方案,如果@CreatedDate or @LastModifiedDate不像预期的那样工作(它对我来说根本不起作用)。
1.为审计添加统一类,然后您将扩展到任何实体(文档):

@Data
public abstract class AuditableDates {

  @CreatedDate
  Instant createdDate;

  @LastModifiedDate
  Instant updatedDate;
}

字符串
1.添加配置,通知Spring auditable已启用

@Configuration
@EnableMongoAuditing
public class DbConfig {
}


1.扩展我们的Audiable界面-成功,在创建/编辑时,您将在DB中获得预期的数据。

public class User extends AuditableDates {


如果您使用Joda时间(或任何其他特定类型),则应添加额外的CustomConversions。

相关问题