Ver Fonte

первая сущность Address, прикрутил querydsl

kpmy há 8 anos atrás
pai
commit
3c1255e2d7

+ 50 - 2
pom.xml

@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns="http://maven.apache.org/POM/4.0.0"
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xmlns="http://maven.apache.org/POM/4.0.0"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
 
@@ -63,6 +63,30 @@
             <version>2.5</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.querydsl</groupId>
+            <artifactId>querydsl-jpa</artifactId>
+            <version>${querydsl.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.querydsl</groupId>
+            <artifactId>querydsl-collections</artifactId>
+            <version>${querydsl.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.querydsl</groupId>
+            <artifactId>querydsl-apt</artifactId>
+            <version>${querydsl.version}</version>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>javax.inject</groupId>
+            <artifactId>javax.inject</artifactId>
+            <version>1</version>
+        </dependency>
     </dependencies>
 
     <build>
@@ -81,6 +105,30 @@
                     <compilerVersion>1.8</compilerVersion>
                 </configuration>
             </plugin>
+            <plugin>
+                <groupId>com.mysema.maven</groupId>
+                <artifactId>apt-maven-plugin</artifactId>
+                <version>1.1.3</version>
+                <executions>
+                    <execution>
+                        <phase>generate-sources</phase>
+                        <goals>
+                            <goal>process</goal>
+                        </goals>
+                        <configuration>
+                            <outputDirectory>${project.basedir}/target/generated-sources/java</outputDirectory>
+                            <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
+                        </configuration>
+                    </execution>
+                </executions>
+                <dependencies>
+                    <dependency>
+                        <groupId>com.querydsl</groupId>
+                        <artifactId>querydsl-apt</artifactId>
+                        <version>${querydsl.version}</version>
+                    </dependency>
+                </dependencies>
+            </plugin>
         </plugins>
     </build>
 </project>

+ 0 - 23
src/main/java/in/ocsf/app/TestController.java

@@ -1,23 +0,0 @@
-package in.ocsf.app;/* kpmy 05.03.2017 */
-
-import in.ocsf.report.ReportPrototype;
-import in.ocsf.report.StagePrototype;
-import in.ocsf.report.TablePrototype;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
-
-@RestController
-public class TestController {
-
-    @RequestMapping("test/init")
-    public Object init(@RequestParam("name") String name){
-        ReportPrototype report = new ReportPrototype();
-
-        TablePrototype table0 = new TablePrototype();
-
-        StagePrototype stage0 = new StagePrototype();
-
-        return null;
-    }
-}

+ 0 - 4
src/main/java/in/ocsf/report/StagePrototype.java

@@ -1,4 +0,0 @@
-package in.ocsf.report;/* kpmy 05.03.2017 */
-
-public class StagePrototype {
-}

+ 0 - 4
src/main/java/in/ocsf/report/TablePrototype.java

@@ -1,4 +0,0 @@
-package in.ocsf.report;/* kpmy 05.03.2017 */
-
-public class TablePrototype {
-}

+ 1 - 1
src/main/java/in/ocsf/report/ReportPrototype.java → src/main/java/in/ocsf/report/api/ReportPrototype.java

@@ -1,4 +1,4 @@
-package in.ocsf.report;/* kpmy 05.03.2017 */
+package in.ocsf.report.api;/* kpmy 05.03.2017 */
 
 import java.util.ArrayList;
 import java.util.List;

+ 4 - 0
src/main/java/in/ocsf/report/api/StagePrototype.java

@@ -0,0 +1,4 @@
+package in.ocsf.report.api;/* kpmy 05.03.2017 */
+
+public class StagePrototype {
+}

+ 4 - 0
src/main/java/in/ocsf/report/api/TablePrototype.java

@@ -0,0 +1,4 @@
+package in.ocsf.report.api;/* kpmy 05.03.2017 */
+
+public class TablePrototype {
+}

+ 3 - 3
src/main/java/in/ocsf/app/App.java → src/main/java/in/ocsf/report/app/Application.java

@@ -1,4 +1,4 @@
-package in.ocsf.app;/* kpmy 05.03.2017 */
+package in.ocsf.report.app;/* kpmy 05.03.2017 */
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -8,9 +8,9 @@ import org.springframework.scheduling.annotation.EnableAsync;
 
 @SpringBootApplication
 @EnableAsync
-public class App extends SpringBootServletInitializer {
+public class Application extends SpringBootServletInitializer {
 
-    private static Class<App> applicationClass = App.class;
+    private static Class<Application> applicationClass = Application.class;
 
     public static void main(String[] args) {
         SpringApplication.run(applicationClass, args);

+ 19 - 0
src/main/java/in/ocsf/report/app/QueryDSL.java

@@ -0,0 +1,19 @@
+package in.ocsf.report.app;/* kpmy 12.03.2017 */
+
+import com.querydsl.collections.CollQueryFactory;
+import com.querydsl.jpa.impl.JPAQueryFactory;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import javax.persistence.EntityManager;
+
+@Configuration
+public class QueryDSL {
+
+    public static final CollQueryFactory collectionQueryFactory = null;
+
+    @Bean
+    public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) {
+        return new JPAQueryFactory(entityManager);
+    }
+}

+ 60 - 0
src/main/java/in/ocsf/report/app/controller/TestController.java

@@ -0,0 +1,60 @@
+package in.ocsf.report.app.controller;/* kpmy 05.03.2017 */
+
+import com.querydsl.collections.CollQueryFactory;
+import com.querydsl.jpa.impl.JPAQueryFactory;
+import in.ocsf.report.api.ReportPrototype;
+import in.ocsf.report.api.StagePrototype;
+import in.ocsf.report.api.TablePrototype;
+import in.ocsf.report.app.object.Address;
+import in.ocsf.report.app.repo.AddressRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+import static in.ocsf.report.app.object.QAddress.address;
+import static in.ocsf.report.app.spec.AddressSpecifications.hasParent;
+import static in.ocsf.report.app.spec.AddressSpecifications.isActual;
+import static org.springframework.data.jpa.domain.Specifications.where;
+
+@RestController
+public class TestController {
+
+    @Autowired
+    private JPAQueryFactory jpaQueryFactory;
+
+    @Autowired
+    private AddressRepository addrRepo;
+
+    @RequestMapping("test/init")
+    public Object init(@RequestParam("name") String name){
+        ReportPrototype report = new ReportPrototype();
+
+        TablePrototype table0 = new TablePrototype();
+
+        StagePrototype stage0 = new StagePrototype();
+
+        return null;
+    }
+
+    @RequestMapping("test/repo/address")
+    public List<Address> addr() {
+        List<Address> top = addrRepo.findActualTop();
+        for (Address address : top) {
+            List<Address> next = addrRepo.findAll(where(hasParent(address.getGuid())).and(isActual()));
+        }
+
+        Address yar = jpaQueryFactory.selectFrom(address)
+                .where(address.region.like("76").and(address.parentGuid.isNull()))
+                .fetchFirst();
+
+        String yarName0 = jpaQueryFactory.select(address.name).from(address).where(address.region.like("76").and(address.parentGuid.isNull())).fetchFirst();
+
+        String yarName1 = CollQueryFactory.from(address, top).select(address.name).where(address.region.like("76").and(address.parentGuid.isNull())).fetchFirst();
+
+        Iterable<Address> yars = addrRepo.findAll(address.region.like("76").and(address.parentGuid.isNull()));
+        return top;
+    }
+}

+ 88 - 0
src/main/java/in/ocsf/report/app/object/Address.java

@@ -0,0 +1,88 @@
+package in.ocsf.report.app.object;/* kpmy 12.03.2017 */
+
+import javax.persistence.*;
+import java.util.Date;
+
+@Entity
+@Table(schema = "FIAS", name = "ADDROBJ")
+public class Address {
+
+    @Id
+    @Column(name = "AOID")
+    private String id;
+
+    @Column(name = "AOGUID", columnDefinition = "CHAR(36)")
+    private String guid;
+
+    @Column(name = "PARENTGUID", columnDefinition = "CHAR(36)")
+    private String parentGuid;
+
+    @Column(name = "FORMALNAME")
+    private String name;
+
+    @Column(name = "SHORTNAME")
+    private String abbr;
+
+    @Column(name = "REGIONCODE")
+    private String region;
+
+    @Column(name = "ENDDATE")
+    @Temporal(TemporalType.DATE)
+    private Date dateKd;
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public String getGuid() {
+        return guid;
+    }
+
+    public void setGuid(String guid) {
+        this.guid = guid;
+    }
+
+    public String getParentGuid() {
+        return parentGuid;
+    }
+
+    public void setParentGuid(String parentGuid) {
+        this.parentGuid = parentGuid;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getAbbr() {
+        return abbr;
+    }
+
+    public void setAbbr(String abbr) {
+        this.abbr = abbr;
+    }
+
+    public String getRegion() {
+        return region;
+    }
+
+    public void setRegion(String region) {
+        this.region = region;
+    }
+
+    public Date getDateKd() {
+        return dateKd;
+    }
+
+    public void setDateKd(Date dateKd) {
+        this.dateKd = dateKd;
+    }
+}

+ 15 - 0
src/main/java/in/ocsf/report/app/repo/AddressRepository.java

@@ -0,0 +1,15 @@
+package in.ocsf.report.app.repo;/* kpmy 12.03.2017 */
+
+import in.ocsf.report.app.object.Address;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.querydsl.QueryDslPredicateExecutor;
+import org.springframework.data.repository.PagingAndSortingRepository;
+
+import java.util.Date;
+import java.util.List;
+
+public interface AddressRepository extends PagingAndSortingRepository<Address, String>, AddressRepositoryCustom, JpaSpecificationExecutor<Address>, QueryDslPredicateExecutor<Address> {
+
+    List<Address> findByDateKdGreaterThanAndParentGuidIsNull(Date dateKd);
+
+}

+ 10 - 0
src/main/java/in/ocsf/report/app/repo/AddressRepositoryCustom.java

@@ -0,0 +1,10 @@
+package in.ocsf.report.app.repo;/* kpmy 12.03.2017 */
+
+import in.ocsf.report.app.object.Address;
+
+import java.util.List;
+
+public interface AddressRepositoryCustom {
+
+    List<Address> findActualTop();
+}

+ 20 - 0
src/main/java/in/ocsf/report/app/repo/AddressRepositoryImpl.java

@@ -0,0 +1,20 @@
+package in.ocsf.report.app.repo;/* kpmy 12.03.2017 */
+
+import in.ocsf.report.app.object.Address;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Lazy;
+
+import java.util.Date;
+import java.util.List;
+
+public class AddressRepositoryImpl implements AddressRepositoryCustom {
+
+    @Autowired
+    @Lazy
+    private AddressRepository addrRepo;
+
+    @Override
+    public List<Address> findActualTop() {
+        return addrRepo.findByDateKdGreaterThanAndParentGuidIsNull(new Date());
+    }
+}

+ 17 - 0
src/main/java/in/ocsf/report/app/spec/AddressSpecifications.java

@@ -0,0 +1,17 @@
+package in.ocsf.report.app.spec;/* kpmy 12.03.2017 */
+
+import in.ocsf.report.app.object.Address;
+import org.springframework.data.jpa.domain.Specification;
+
+import java.util.Date;
+
+public class AddressSpecifications {
+
+    public static Specification<Address> isActual() {
+        return (root, q, cb) -> cb.greaterThan(root.get("dateKd"), new Date());
+    }
+
+    public static Specification<Address> hasParent(String parent) {
+        return (root, criteriaQuery, criteriaBuilder) -> criteriaBuilder.equal(root.get("parentGuid"), parent);
+    }
+}

+ 3 - 0
src/main/resources/application-dev.properties

@@ -1,3 +1,6 @@
 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/FIAS?verifyServerCertificate=false&useSSL=false&requireSSL=false&useUnicode=yes&characterEncoding=UTF-8
 spring.datasource.username=user
 spring.datasource.password=password
+spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
+spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
+spring.jpa.show-sql=true