본문 바로가기
SpringBoot

spring boot jpa(querydsl)

by ByteBridge 2015. 10. 20.
반응형


spring boot 에서 jpa 사용


기본적으로 제공되는 findByXXX, 와 같은 기능들은 문제없이 사용 된다. 

repository를 실제고 구현 해야 할때는 추가적으로 해야 할 설정이있다. ( 개인 적인 생각?)


cusmtom repository 를 구현 하려면 데이터 소스에 대한 설정을 해주어한다.



member jpa model : 



datasourceconfig:



repository class 다이어그램 :




repository 인터페이스를 구현 할 클래스의 이름은 무조건(?) 해당 repository + impl 을 붙여야 한다.


데모프로젝트 패키지 구조 : 



해당 데모 프로젝트 소스 


application.properties:

spring.profiles.active=production

#server port

server.port=80

#postgresql info

#spring.jpa.hibernate.ddl-auto=create

spring.jpa.show-sql=true

spring.jpa.database=POSTGRESQL

spring.datasource.platform=postgres

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres

spring.datasource.username=postgres

spring.datasource.password=password

spring.datasource.driver-class-name=org.postgresql.Driver


data.jpa.model:


package com.example.data.jpa.model;


@Entity

@Table(name="MEMBER")

public class Member 

{

@Id

@GeneratedValue(strategy = GenerationType.SEQUENCE)

private Long index;

private String id;

private String name;

private int age;

@Temporal(TemporalType.TIMESTAMP)

private Date registerTime;


}


data.jpa.repository:

public interface MemberRepository extends JpaRepository<Member, Long>,JpaSpecificationExecutor<Member>,MemberRepositoryCustom

{


}


data.jpa.repository.custom:

public interface MemberRepositoryCustom 

{

public Member getMemberList(String strName);

}

data.jpa.repository.impl:

public class MemberRepositoryImpl extends QueryDslRepositorySupport implements MemberRepositoryCustom

{


@PersistenceContext

private EntityManager entityManager;

  public MemberRepositoryImpl() 

  {

super(Member.class);

}

@Override

public Member getMemberList(String strName) 

{

QMember qMember = QMember.member;

JPAQuery query = new JPAQuery(entityManager);

return query.from(qMember).where(qMember.name.like(strName)).singleResult(qMember);

}


}


datasourceconfig.java:

@Configuration

public class DatasourceConfig 

{

@Bean(name="memberDatasource")

@ConfigurationProperties(prefix="spring.datasource")

public DataSource getMemberDataSource()

{

return DataSourceBuilder.create().build();

}

}


  DemoApp.java:

@SpringBootApplication

public class DemoApplication {


    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);

    }

}


Test

@RunWith(SpringJUnit4ClassRunner.class)

@SpringApplicationConfiguration(classes = DemoApplication.class)

@WebAppConfiguration

public class DemoApplicationTests {


@Autowired

private MemberRepository memberRepository;

@Test

public void contextLoads() {

}

// @Test

public void addmemberTest()

{

Member member = new Member();

member.setAge(100);

member.setId("gavin");

member.setName("gavinKim");

member.setRegisterTime(new Date());

memberRepository.save(member);

}

@Test

public void repositoryTest()

{

Member member = memberRepository.getMemberList("gavinKim");

System.out.println(member.toString()); 

}

}




Demo.zip


반응형