Item
This commit is contained in:
28
pom.xml
28
pom.xml
@@ -54,6 +54,17 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<!-- QueryDSL APT Config -->
|
||||
<dependency>
|
||||
<groupId>com.querydsl</groupId>
|
||||
<artifactId>querydsl-apt</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- QueryDSL JPA Config -->
|
||||
<dependency>
|
||||
<groupId>com.querydsl</groupId>
|
||||
<artifactId>querydsl-jpa</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -70,6 +81,23 @@
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>com.mysema.maven</groupId>
|
||||
<artifactId>apt-maven-plugin</artifactId>
|
||||
<version>1.1.3</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>process</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>target/generated-sources/java</outputDirectory>
|
||||
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.security.basic.configure;
|
||||
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
|
||||
@Configuration
|
||||
@EnableJpaAuditing
|
||||
public class JpaConfiguration {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@Bean
|
||||
public JPAQueryFactory jpaQueryFactory () {
|
||||
return new JPAQueryFactory(em);
|
||||
}
|
||||
}
|
||||
15
src/main/java/com/security/basic/mapped/BaseItem.java
Normal file
15
src/main/java/com/security/basic/mapped/BaseItem.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package com.security.basic.mapped;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@MappedSuperclass
|
||||
public abstract class BaseItem {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id", nullable = false)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "name", nullable = false)
|
||||
private String name;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.security.basic.mapped;
|
||||
|
||||
import com.querydsl.core.types.Projections;
|
||||
import com.querydsl.jpa.impl.JPAQueryFactory;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Repository
|
||||
public class BaseItemRepository {
|
||||
|
||||
private final JPAQueryFactory queryFactory;
|
||||
|
||||
com.security.basic.mapped.QBaseItem qItemA = com.security.basic.mapped.QItemA.itemA._super;
|
||||
com.security.basic.mapped.QBaseItem qItemB = com.security.basic.mapped.QItemB.itemB._super;
|
||||
|
||||
public List<ItemDto> itemAList() {
|
||||
return queryFactory.select(Projections.constructor(ItemDto.class,
|
||||
qItemA.id,
|
||||
qItemA.name
|
||||
))
|
||||
.from(qItemA)
|
||||
.fetch();
|
||||
}
|
||||
|
||||
public List<ItemDto> itemBList() {
|
||||
return queryFactory.select(Projections.constructor(ItemDto.class,
|
||||
qItemB.id,
|
||||
qItemB.name
|
||||
))
|
||||
.from(qItemB)
|
||||
.fetch();
|
||||
}
|
||||
}
|
||||
25
src/main/java/com/security/basic/mapped/Controller.java
Normal file
25
src/main/java/com/security/basic/mapped/Controller.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.security.basic.mapped;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/mapped")
|
||||
public class Controller {
|
||||
|
||||
private final BaseItemRepository baseItemRepository;
|
||||
|
||||
@GetMapping("/a")
|
||||
public ResponseEntity<?> a() {
|
||||
return ResponseEntity.ok(baseItemRepository.itemAList());
|
||||
}
|
||||
|
||||
@GetMapping("/b")
|
||||
public ResponseEntity<?> b() {
|
||||
return ResponseEntity.ok(baseItemRepository.itemBList());
|
||||
}
|
||||
}
|
||||
8
src/main/java/com/security/basic/mapped/ItemA.java
Normal file
8
src/main/java/com/security/basic/mapped/ItemA.java
Normal file
@@ -0,0 +1,8 @@
|
||||
package com.security.basic.mapped;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class ItemA extends BaseItem{
|
||||
|
||||
}
|
||||
10
src/main/java/com/security/basic/mapped/ItemB.java
Normal file
10
src/main/java/com/security/basic/mapped/ItemB.java
Normal file
@@ -0,0 +1,10 @@
|
||||
package com.security.basic.mapped;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Table(name = "item_a")
|
||||
@Entity
|
||||
public class ItemB extends BaseItem{
|
||||
|
||||
}
|
||||
14
src/main/java/com/security/basic/mapped/ItemDto.java
Normal file
14
src/main/java/com/security/basic/mapped/ItemDto.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.security.basic.mapped;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class ItemDto {
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
}
|
||||
Reference in New Issue
Block a user