간단하게 조회만 해보자. 샘플 데이터를 넣자.
main/resources/application.yml
spring:
profiles:
active: local
test/resources/application.yml
spring:
profiles:
active: test
datasource:
url: jdbc:h2:tcp://localhost/~/querydsl
username: sa
password:
driver-class-name: org.h2.Driver
jpa:
hibernate:
ddl-auto: create
properties:
hibernate:
# show_sql: true
format_sql: true
# use_sql_comments: true
logging.level:
org.hibernate.SQL: debug
org.hibernate.type: trace
InitMember
@Profile("local")
@Component
@RequiredArgsConstructor
public class InitMember {
private final InitMemberService initMemberService;
@PostConstruct
public void init() {
initMemberService.init();
}
@Component
static class InitMemberService {
@PersistenceContext
private EntityManager em;
@Transactional
public void init() {
Team teamA = new Team("teamA");
Team teamB = new Team("teamB");
em.persist(teamA);
em.persist(teamB);
for (int i = 0; i < 100; i++) {
Team selectedTeam = i % 2 == 0 ? teamA : teamB;
em.persist(new Member("member" + i, i, selectedTeam));
}
}
}
}
@PostConstruct
와 @Transactional
이 동시에 써지지가 않는다.실행해 보면 스프링부트가 처음 뜰 때 profiles active가 표시된다.