jdbc
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
package com.example.oneul.domain.post.dao;
|
||||
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.example.oneul.domain.post.domain.Post;
|
||||
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
|
||||
|
||||
public class PostCommandJdbcRespositoryImpl implements PostCommandRepository {
|
||||
private final JdbcTemplate jdbcTemplate;
|
||||
|
||||
public PostCommandJdbcRespositoryImpl(DataSource dataSource){
|
||||
this.jdbcTemplate = new JdbcTemplate(dataSource);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post save(Post post){
|
||||
SimpleJdbcInsert jdbcInsert = new SimpleJdbcInsert(jdbcTemplate);
|
||||
jdbcInsert.withTableName("post").usingGeneratedKeyColumns("id");
|
||||
|
||||
Map<String, Object> parameters = new HashMap<>();
|
||||
parameters.put("content", post.getContent());
|
||||
parameters.put("writer", post.getWriter());
|
||||
Number key = jdbcInsert.executeAndReturnKey(new MapSqlParameterSource(parameters));
|
||||
post.setId(key.longValue());
|
||||
|
||||
return new Post();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post update(Post post){
|
||||
return new Post();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteById(Long id){
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Post post){
|
||||
|
||||
}
|
||||
}
|
||||
@@ -12,10 +12,10 @@ import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
@Transactional
|
||||
public class PostQueryRepositoryImpl implements PostCommandRepository {
|
||||
public class PostCommandJpaRepositoryImpl implements PostCommandRepository {
|
||||
private final EntityManager em;
|
||||
|
||||
public PostQueryRepositoryImpl(EntityManagerFactory entityManagerFactory){
|
||||
public PostCommandJpaRepositoryImpl(EntityManagerFactory entityManagerFactory){
|
||||
this.em = entityManagerFactory.createEntityManager();
|
||||
}
|
||||
|
||||
@@ -32,9 +32,15 @@ public class PostQueryRepositoryImpl implements PostCommandRepository {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Long id){
|
||||
em.createQuery("delete from Post where id = :id")
|
||||
.setParameter("id", id)
|
||||
.executeUpdate();
|
||||
public void deleteById(Long id){
|
||||
Post post = em.createQuery("select p from Post p where p.id = :id", Post.class)
|
||||
.setParameter("id", id)
|
||||
.getSingleResult();
|
||||
delete(post);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Post post){
|
||||
em.remove(post);
|
||||
}
|
||||
}
|
||||
@@ -5,5 +5,6 @@ import com.example.oneul.domain.post.domain.Post;
|
||||
public interface PostCommandRepository{
|
||||
Post save(Post post);
|
||||
Post update(Post post);
|
||||
void delete(Long id);
|
||||
void deleteById(Long id);
|
||||
void delete(Post post);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.example.oneul.global.config;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import com.example.oneul.domain.post.dao.PostCommandJdbcRespositoryImpl;
|
||||
import com.example.oneul.domain.post.dao.PostCommandRepository;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class JdbcConfig {
|
||||
private final DataSource dataSource;
|
||||
|
||||
public JdbcConfig(DataSource dataSource){
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PostCommandRepository postCommandRepository(){
|
||||
return new PostCommandJdbcRespositoryImpl(dataSource);
|
||||
}
|
||||
}
|
||||
@@ -3,13 +3,18 @@ package com.example.oneul.repository;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import com.example.oneul.domain.post.dao.PostCommandRepository;
|
||||
import com.example.oneul.domain.post.dao.PostQueryRepository;
|
||||
import com.example.oneul.domain.post.domain.Post;
|
||||
import com.example.oneul.domain.user.dao.UserRepository;
|
||||
import com.example.oneul.domain.user.domain.UserEntity;
|
||||
import com.example.oneul.global.config.JdbcConfig;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
@@ -17,8 +22,9 @@ import org.testcontainers.junit.jupiter.Testcontainers;
|
||||
@Testcontainers
|
||||
@ActiveProfiles("test")
|
||||
@SpringBootTest
|
||||
public class PostCommandServiceTest {
|
||||
@Autowired private PostCommandRepository postCommandRepository;
|
||||
public class PostCommandRepositoryTest {
|
||||
private PostCommandRepository postCommandRepository;
|
||||
@Autowired private PostQueryRepository postQueryRepository;
|
||||
@Autowired private UserRepository userRepository;
|
||||
|
||||
static {
|
||||
@@ -30,6 +36,12 @@ public class PostCommandServiceTest {
|
||||
System.setProperty("spring.redis.port", redis.getFirstMappedPort() + "");
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
public void init(){
|
||||
ApplicationContext ac = new AnnotationConfigApplicationContext(JdbcConfig.class);
|
||||
postCommandRepository = ac.getBean("postCommandRepository", PostCommandRepository.class);
|
||||
}
|
||||
|
||||
private UserEntity createTestUser(){
|
||||
return userRepository.save(UserEntity.builder()
|
||||
.username("test user")
|
||||
@@ -46,8 +58,8 @@ public class PostCommandServiceTest {
|
||||
.writer(testUser)
|
||||
.build();
|
||||
|
||||
Post createdPost = postCommandRepository.save(post);
|
||||
|
||||
post = postCommandRepository.save(post);
|
||||
Post createdPost = postQueryRepository.findById(post.getId()).orElse(new Post());
|
||||
assertEquals(false, createdPost.getId() == null);
|
||||
assertEquals(post, createdPost);
|
||||
}
|
||||
@@ -80,7 +92,20 @@ public class PostCommandServiceTest {
|
||||
.build();
|
||||
|
||||
Post createdPost = postCommandRepository.save(post);
|
||||
Long id = createdPost.getId();
|
||||
postCommandRepository.delete(id);
|
||||
postCommandRepository.delete(createdPost);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deleteByIdTest(){
|
||||
UserEntity testUser = createTestUser();
|
||||
|
||||
Post post = Post.builder()
|
||||
.content("test content")
|
||||
.writer(testUser)
|
||||
.build();
|
||||
|
||||
Post createdPost = postCommandRepository.save(post);
|
||||
System.out.println("post id: " + post.getId());
|
||||
postCommandRepository.deleteById(createdPost.getId());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user