diff --git a/src/main/java/io/spring/MyBatisConfig.java b/src/main/java/io/spring/MyBatisConfig.java new file mode 100644 index 0000000..bbf9231 --- /dev/null +++ b/src/main/java/io/spring/MyBatisConfig.java @@ -0,0 +1,9 @@ +package io.spring; + +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@Configuration +@EnableTransactionManagement +public class MyBatisConfig { +} diff --git a/src/main/java/io/spring/infrastructure/repository/MyBatisArticleRepository.java b/src/main/java/io/spring/infrastructure/repository/MyBatisArticleRepository.java index 114fd34..056dd01 100644 --- a/src/main/java/io/spring/infrastructure/repository/MyBatisArticleRepository.java +++ b/src/main/java/io/spring/infrastructure/repository/MyBatisArticleRepository.java @@ -5,6 +5,7 @@ import io.spring.core.article.ArticleRepository; import io.spring.core.article.Tag; import io.spring.infrastructure.mybatis.mapper.ArticleMapper; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; import java.util.Optional; @@ -17,6 +18,7 @@ public class MyBatisArticleRepository implements ArticleRepository { } @Override + @Transactional public void save(Article article) { if (articleMapper.findById(article.getId()) == null) { createNew(article); @@ -26,13 +28,13 @@ public class MyBatisArticleRepository implements ArticleRepository { } private void createNew(Article article) { - articleMapper.insert(article); for (Tag tag : article.getTags()) { if (!articleMapper.findTag(tag.getName())) { articleMapper.insertTag(tag); } articleMapper.insertArticleTagRelation(article.getId(), tag.getId()); } + articleMapper.insert(article); } @Override diff --git a/src/test/java/io/spring/infrastructure/article/ArticleRepositoryTransactionTest.java b/src/test/java/io/spring/infrastructure/article/ArticleRepositoryTransactionTest.java new file mode 100644 index 0000000..7a09ec3 --- /dev/null +++ b/src/test/java/io/spring/infrastructure/article/ArticleRepositoryTransactionTest.java @@ -0,0 +1,46 @@ +package io.spring.infrastructure.article; + +import io.spring.core.article.Article; +import io.spring.core.article.ArticleRepository; +import io.spring.core.user.User; +import io.spring.core.user.UserRepository; +import io.spring.infrastructure.mybatis.mapper.ArticleMapper; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureTestDatabase +public class ArticleRepositoryTransactionTest { + @Autowired + private ArticleRepository articleRepository; + + @Autowired + private UserRepository userRepository; + + @Autowired + private ArticleMapper articleMapper; + + + @Test + public void transactional_test() throws Exception { + User user = new User("aisensiy@gmail.com", "aisensiy", "123", "bio", "default"); + userRepository.save(user); + Article article = new Article("test", "desc", "body", new String[]{"java", "spring"}, user.getId()); + articleRepository.save(article); + Article anotherArticle = new Article("test", "desc", "body", new String[]{"java", "spring", "other"}, user.getId()); + try { + articleRepository.save(anotherArticle); + } catch (Exception e) { + assertThat(articleMapper.findTag("other"), is(false)); + } + } + +} diff --git a/src/test/java/io/spring/infrastructure/article/MyBatisArticleRepositoryTest.java b/src/test/java/io/spring/infrastructure/article/MyBatisArticleRepositoryTest.java index ba6defb..adf3a70 100644 --- a/src/test/java/io/spring/infrastructure/article/MyBatisArticleRepositoryTest.java +++ b/src/test/java/io/spring/infrastructure/article/MyBatisArticleRepositoryTest.java @@ -30,6 +30,7 @@ public class MyBatisArticleRepositoryTest { @Autowired private UserRepository userRepository; + private User user; private Article article;