AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當(dāng)前位置:首頁 > 科技  > 軟件

基于JPA如何處理數(shù)據(jù)表公共字段?

來源: 責(zé)編: 時(shí)間:2024-06-20 08:55:26 159觀看
導(dǎo)讀在軟件開發(fā)中,我們經(jīng)常需要跟蹤實(shí)體的歷史記錄,例如創(chuàng)建時(shí)間、修改時(shí)間等。Spring Data JPA 提供了一種名為 Auditing 的功能,可以自動(dòng)處理這些操作。本文將介紹如何在 Spring Boot 項(xiàng)目中使用 Spring JPA Auditing。實(shí)

在軟件開發(fā)中,我們經(jīng)常需要跟蹤實(shí)體的歷史記錄,例如創(chuàng)建時(shí)間、修改時(shí)間等。Spring Data JPA 提供了一種名為 Auditing 的功能,可以自動(dòng)處理這些操作。本文將介紹如何在 Spring Boot 項(xiàng)目中使用 Spring JPA Auditing。I0K28資訊網(wǎng)——每日最新資訊28at.com

實(shí)現(xiàn)方式

在實(shí)體類上添加注解;I0K28資訊網(wǎng)——每日最新資訊28at.com

  1. 定義通用實(shí)體,比如Domain類,我們會(huì)將通用字段在這里定義
@MappedSuperclass@Datapublic class Domain implements Serializable {    @CreatedBy    @Column(name = "creator", length = 56)    private String creator;    @CreatedDate    @Column(name = "create_time", length = 12)    private Date createTime;    @LastModifiedBy    @Column(name = "modifier", length = 56)    private String modifier;    @LastModifiedDate    @Column(name = "modified_time", length = 12)    private Date modifiedTime;}
  1. 添加Auditing相關(guān)注解;
// ...@EntityListeners(AuditingEntityListener.class)public class Domain implements Serializable {    // ...}
  1. 添加自動(dòng)填充屬性實(shí)現(xiàn),主要通過實(shí)現(xiàn)AuditorAware接口,并將實(shí)現(xiàn)注入到spring容器;
public class DomainAuditorAware implements AuditorAware<String> {    @Override    public Optional<String> getCurrentAuditor() {        return Optional.of("我是操作人");    }}
  1. 定義spring自動(dòng)配置;
@Configurationpublic class JpaAuditingConfiguration {    @Bean    public DomainAuditorAware domainAuditorAware(){        return new DomainAuditorAware();    }}

在resource目錄添加自動(dòng)注入配置META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports,這樣通過引入jar就可以自動(dòng)使用該配置。I0K28資訊網(wǎng)——每日最新資訊28at.com

cn.cycad.jpa.auditing.config.JpaAuditingConfiguration

應(yīng)用示例

  1. 比如現(xiàn)在有一個(gè)User實(shí)體,我們通過繼承基類;
@Entity@Table(name = "t_user")@Datapublic class User extends Domain {    @Id    private String id;    private String caption;}
  1. 定義用戶對應(yīng)的Repository;
public interface UserRepository extends JpaRepository<User,String> {    }
  1. 用戶的創(chuàng)建與修改基于UserRepository來實(shí)現(xiàn);
@RestController@RequestMapping("/user")public class UserController {    @Resource    private UserRepository userRepository;        @PostMapping    public User saveUser(@RequestBody User user){        return userRepository.save(user);    }}
  1. 服務(wù);
@EnableJpaAuditing@EntityScan({"cn.cycad.jpa.auditing.sample.entity"})@EnableJpaRepositories(basePackages="cn.cycad.jpa.auditing.sample.repository")@SpringBootApplicationpublic class SampleApplication{    public static void main(String[] args) {        SpringApplication.run(SampleApplication.class, args);    }}

通過注解@EnableJpaAuditing啟用。I0K28資訊網(wǎng)——每日最新資訊28at.com

  1. 編寫測試用例;
### 新增用戶POST http://localhost:8080/userContent-Type: application/json{  "id": "1",  "caption": "tom"}

這樣每次調(diào)用用戶新增請求時(shí),默認(rèn)會(huì)將DomainAuditorAware的返回值填充到@CreatedBy與@LastModifiedBy修飾的字段上去。I0K28資訊網(wǎng)——每日最新資訊28at.com

實(shí)現(xiàn)原理

可以看到,實(shí)現(xiàn)該效果狐妖有以下幾個(gè)關(guān)鍵點(diǎn):I0K28資訊網(wǎng)——每日最新資訊28at.com

  1. 實(shí)體需要添加@EntityListeners(AuditingEntityListener.class),并且需要再對應(yīng)字段上標(biāo)識出需要注入的操作人、操作時(shí)間等。
  2. 需要編寫自己的實(shí)現(xiàn)AuditorAware<String>,這里只用關(guān)注創(chuàng)建人,時(shí)間沒必要處理,當(dāng)然也可以通過實(shí)現(xiàn)接口DateTimeProvider來擴(kuò)展。
  3. 需要基于JpaRepository接口實(shí)現(xiàn)用戶的新增或修改。
  4. 需要@EnableJpaAuditing開啟。

本文鏈接:http://www.tebozhan.com/showinfo-26-95001-0.html基于JPA如何處理數(shù)據(jù)表公共字段?

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: Runway 王者歸來發(fā)布 Gen-3 Google 快手萬興科技等紛紛聚焦 AI 視頻生成賽道

下一篇: IDC報(bào)告:AR/VR頭顯出貨量驟降67.4%,但MR/ER頭顯呈現(xiàn)新機(jī)遇

標(biāo)簽:
  • 熱門焦點(diǎn)
Top