diff --git a/luoo_tag/pom.xml b/luoo_tag/pom.xml index c707da6..6c39922 100644 --- a/luoo_tag/pom.xml +++ b/luoo_tag/pom.xml @@ -42,6 +42,10 @@ org.springframework.cloud spring-cloud-starter-openfeign + + com.h2database + h2 + app diff --git a/luoo_tag/src/main/java/com/luoo/tag/TagApplication.java b/luoo_tag/src/main/java/com/luoo/tag/TagApplication.java index 11fec68..efc639b 100644 --- a/luoo_tag/src/main/java/com/luoo/tag/TagApplication.java +++ b/luoo_tag/src/main/java/com/luoo/tag/TagApplication.java @@ -6,12 +6,15 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; + import util.IdWorker; @SpringBootApplication(scanBasePackages = "com.luoo") @EnableEurekaClient @EnableDiscoveryClient @EnableFeignClients @EnableCaching +@EnableJpaAuditing public class TagApplication { public static void main(String[] args) { diff --git a/luoo_tag/src/main/java/com/luoo/tag/pojo/Tag.java b/luoo_tag/src/main/java/com/luoo/tag/pojo/Tag.java index 5b0f88c..e4a8457 100644 --- a/luoo_tag/src/main/java/com/luoo/tag/pojo/Tag.java +++ b/luoo_tag/src/main/java/com/luoo/tag/pojo/Tag.java @@ -4,14 +4,21 @@ package com.luoo.tag.pojo; import lombok.Data; import javax.persistence.Entity; +import javax.persistence.EntityListeners; import javax.persistence.Id; import javax.persistence.Table; + +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + import java.io.Serializable; import java.time.LocalDateTime; @Data @Entity @Table(name = "tb_tag") +@EntityListeners(AuditingEntityListener.class) public class Tag implements Serializable { private static final long serialVersionUID = -5593148272164146667L; @@ -46,10 +53,12 @@ public class Tag implements Serializable { /** * 创建时间 */ + @CreatedDate private LocalDateTime createTime; /** * 更新时间 */ + @LastModifiedDate private LocalDateTime updateTime; } diff --git a/luoo_tag/src/test/java/com/luoo/tag/dao/TagDaoTest.java b/luoo_tag/src/test/java/com/luoo/tag/dao/TagDaoTest.java new file mode 100644 index 0000000..c54d772 --- /dev/null +++ b/luoo_tag/src/test/java/com/luoo/tag/dao/TagDaoTest.java @@ -0,0 +1,57 @@ +package com.luoo.tag.dao; + +import static org.junit.Assert.*; + +import org.junit.Test; +import java.time.LocalDateTime; +import java.util.stream.IntStream; + +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.test.context.junit4.SpringRunner; + +import com.luoo.tag.pojo.Tag; + +@RunWith(SpringRunner.class) +@DataJpaTest +public class TagDaoTest { + @Autowired + private TestEntityManager entityManager; + + @Autowired + private TagDao dao; + + @Test + public void testPagable() throws InterruptedException { + saveTags(); + assertEquals(dao.count(), 100L); + Pageable pageRequest = PageRequest.of(0, 2); + Page tags = dao.findAll(pageRequest); + assertEquals(tags.getSize(), 2); + Tag firstTag = tags.iterator().next(); + String firstTagId = "tag-0"; + assertEquals(firstTag.getId(), firstTagId); + + LocalDateTime updateTimeBefore = firstTag.getUpdateTime(); + firstTag.setState(1); + entityManager.persistAndFlush(firstTag); + assertEquals(dao.count(), 100L); + + LocalDateTime updateTimeAfter = dao.findById(firstTagId).get().getUpdateTime(); + assertTrue(updateTimeAfter.isAfter(updateTimeBefore)); + } + + private void saveTags() { + IntStream.range(0, 100).forEach(id -> { + Tag tag = new Tag(); + tag.setId("tag-" + id); + + entityManager.persist(tag); + }); + } +}