Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 28105d0

Browse files
Spring integración con QueryDSL
Integrar Spring y QueryDSL para facilitar la creación de consultas.
1 parent 0343b26 commit 28105d0

File tree

9 files changed

+347
-0
lines changed

9 files changed

+347
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<!--
4+
This file contains additional configuration written by modules in the NetBeans IDE.
5+
The configuration is intended to be shared among all the users of project and
6+
therefore it is assumed to be part of version control checkout.
7+
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
8+
-->
9+
<spring-data xmlns="http://www.netbeans.org/ns/spring-data/1">
10+
<config-files>
11+
<config-file>src/main/resources/springXMLConfig.xml</config-file>
12+
<config-file>src/main/resources/beans.xml</config-file>
13+
</config-files>
14+
<config-file-groups/>
15+
</spring-data>
16+
</project-shared-configuration>

‎tutorial_spring_querydsl/nbactions.xml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<actions>
3+
<action>
4+
<actionName>run</actionName>
5+
<packagings>
6+
<packaging>jar</packaging>
7+
</packagings>
8+
<goals>
9+
<goal>process-classes</goal>
10+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
11+
</goals>
12+
<properties>
13+
<exec.args>-classpath %classpath carmelo.spring.data.SpringDataJpa</exec.args>
14+
<exec.executable>java</exec.executable>
15+
</properties>
16+
</action>
17+
<action>
18+
<actionName>debug</actionName>
19+
<packagings>
20+
<packaging>jar</packaging>
21+
</packagings>
22+
<goals>
23+
<goal>process-classes</goal>
24+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
25+
</goals>
26+
<properties>
27+
<exec.args>-Xdebug -Xrunjdwp:transport=dt_socket,server=n,address=${jpda.address} -classpath %classpath carmelo.spring.data.SpringDataJpa</exec.args>
28+
<exec.executable>java</exec.executable>
29+
<jpda.listen>true</jpda.listen>
30+
</properties>
31+
</action>
32+
<action>
33+
<actionName>profile</actionName>
34+
<packagings>
35+
<packaging>jar</packaging>
36+
</packagings>
37+
<goals>
38+
<goal>process-classes</goal>
39+
<goal>org.codehaus.mojo:exec-maven-plugin:1.2.1:exec</goal>
40+
</goals>
41+
<properties>
42+
<exec.args>-classpath %classpath carmelo.spring.data.SpringDataJpa</exec.args>
43+
<exec.executable>java</exec.executable>
44+
</properties>
45+
</action>
46+
</actions>

‎tutorial_spring_querydsl/pom.xml

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>carmelo.spring</groupId>
5+
<artifactId>tutorial_spring_querydsl</artifactId>
6+
<version>1.0</version>
7+
<packaging>jar</packaging>
8+
<name>Tutorial Spring QueryDSL</name>
9+
<description>Acceso a datos con Sprind Data JPA e integrando QueryDSL para facilitar las consultas.</description>
10+
11+
<dependencyManagement>
12+
<dependencies>
13+
<dependency>
14+
<groupId>org.springframework</groupId>
15+
<artifactId>spring-framework-bom</artifactId>
16+
<version>4.3.7.RELEASE</version>
17+
<type>pom</type>
18+
<scope>import</scope>
19+
</dependency>
20+
</dependencies>
21+
</dependencyManagement>
22+
23+
<dependencies>
24+
25+
<dependency>
26+
<groupId>org.hibernate</groupId>
27+
<artifactId>hibernate-core</artifactId>
28+
<version>4.3.11.Final</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.hibernate</groupId>
32+
<artifactId>hibernate-entitymanager</artifactId>
33+
<version>4.3.11.Final</version>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.hsqldb</groupId>
38+
<artifactId>hsqldb</artifactId>
39+
<version>2.3.3</version>
40+
<scope>runtime</scope>
41+
</dependency>
42+
43+
44+
<dependency>
45+
<groupId>org.springframework.data</groupId>
46+
<artifactId>spring-data-jpa</artifactId>
47+
<!-- <version>1.11.1.RELEASE</version> -->
48+
<version>1.9.2.RELEASE</version>
49+
</dependency>
50+
51+
52+
<dependency>
53+
<groupId>org.projectlombok</groupId>
54+
<artifactId>lombok</artifactId>
55+
<version>1.16.12</version>
56+
</dependency>
57+
58+
59+
<!-- bucar ultima version -->
60+
<!--
61+
<dependency>
62+
<groupId>com.querydsl</groupId>
63+
<artifactId>querydsl-apt</artifactId>
64+
<version>${querydsl.version}</version>
65+
<scope>provided</scope>
66+
</dependency>
67+
68+
<dependency>
69+
<groupId>com.querydsl</groupId>
70+
<artifactId>querydsl-jpa</artifactId>
71+
<version>${querydsl.version}</version>
72+
</dependency>
73+
-->
74+
75+
<!-- Query DSL integration -->
76+
<dependency>
77+
<groupId>com.mysema.querydsl</groupId>
78+
<artifactId>querydsl-apt</artifactId>
79+
<version>3.7.1</version>
80+
</dependency>
81+
<dependency>
82+
<groupId>com.mysema.querydsl</groupId>
83+
<artifactId>querydsl-jpa</artifactId>
84+
<version>3.7.1</version>
85+
</dependency>
86+
87+
</dependencies>
88+
89+
<properties>
90+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
91+
<maven.compiler.source>1.8</maven.compiler.source>
92+
<maven.compiler.target>1.8</maven.compiler.target>
93+
<querydsl.version>3.7.1</querydsl.version>
94+
</properties>
95+
96+
<build>
97+
<plugins>
98+
<plugin>
99+
<groupId>com.mysema.maven</groupId>
100+
<artifactId>apt-maven-plugin</artifactId>
101+
<version>1.1.3</version>
102+
<executions>
103+
<execution>
104+
<goals>
105+
<goal>process</goal>
106+
</goals>
107+
<configuration>
108+
<outputDirectory>target/generated-sources/java</outputDirectory>
109+
<!-- <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor> -->
110+
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
111+
</configuration>
112+
</execution>
113+
</executions>
114+
</plugin>
115+
</plugins>
116+
</build>
117+
</project>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package carmelo.spring.data;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import javax.sql.DataSource;
7+
8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
11+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
12+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
13+
import org.springframework.orm.jpa.JpaTransactionManager;
14+
import org.springframework.orm.jpa.JpaVendorAdapter;
15+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
16+
import org.springframework.orm.jpa.vendor.Database;
17+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
18+
import org.springframework.transaction.PlatformTransactionManager;
19+
import org.springframework.transaction.annotation.EnableTransactionManagement;
20+
21+
@Configuration
22+
@EnableTransactionManagement
23+
@EnableJpaRepositories("carmelo.spring.data.repository")
24+
public class SpringConfiguration {
25+
26+
@Bean
27+
public DataSource dataSource() {
28+
return new EmbeddedDatabaseBuilder()
29+
.setType(EmbeddedDatabaseType.HSQL)
30+
.addScript("classpath:data/schema.sql")
31+
.addScript("classpath:data/data.sql")
32+
.generateUniqueName(true)
33+
.build();
34+
}
35+
36+
@Bean
37+
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
38+
LocalContainerEntityManagerFactoryBean factoryBean
39+
= new LocalContainerEntityManagerFactoryBean();
40+
factoryBean.setDataSource(dataSource());
41+
factoryBean.setJpaVendorAdapter(jpaVendorAdapter());
42+
factoryBean.setPackagesToScan("carmelo.spring.data.model");
43+
factoryBean.setPersistenceUnitName("spring-data-pun");
44+
45+
Map<String, String> props = new HashMap<>();
46+
props.put("hibernate.format_sql", "true");
47+
48+
factoryBean.setJpaPropertyMap(props);
49+
50+
return factoryBean;
51+
}
52+
53+
@Bean
54+
public JpaVendorAdapter jpaVendorAdapter() {
55+
HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
56+
jpaVendorAdapter.setGenerateDdl(false);
57+
jpaVendorAdapter.setDatabase(Database.HSQL);
58+
jpaVendorAdapter.setShowSql(true);
59+
return jpaVendorAdapter;
60+
}
61+
62+
@Bean
63+
public PlatformTransactionManager transactionManager() {
64+
JpaTransactionManager transactionManager = new JpaTransactionManager();
65+
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
66+
return transactionManager;
67+
}
68+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package carmelo.spring.data;
2+
3+
import carmelo.spring.data.model.QProduct;
4+
import carmelo.spring.data.repository.ProductRepository;
5+
import com.mysema.query.types.Predicate;
6+
import com.mysema.query.types.expr.BooleanExpression;
7+
import java.sql.SQLException;
8+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
9+
import org.springframework.context.support.AbstractApplicationContext;
10+
11+
public class SpringDataJpa {
12+
13+
public static void main(String[] args) throws SQLException {
14+
15+
AbstractApplicationContext ctx
16+
= new AnnotationConfigApplicationContext(SpringConfiguration.class);
17+
18+
ProductRepository pr = ctx.getBean(ProductRepository.class);
19+
20+
QProduct qp = QProduct.product;
21+
22+
BooleanExpression precio = qp.price.gt(100.0);
23+
BooleanExpression helado = qp.name.contains("Ice");
24+
25+
Predicate helados_caros = helado.and(precio);
26+
27+
pr.findAll(helados_caros).forEach(System.out::println);
28+
29+
// pr.findAll(qp.isNotNull(), qp.price.asc())
30+
// .forEach(System.out::println);
31+
//
32+
// long count = pr.count(qp.price.between(50.0, 220.0));
33+
// System.out.println("count: " + count);
34+
35+
ctx.close();
36+
}
37+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package carmelo.spring.data.model;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.Id;
5+
6+
import lombok.Data;
7+
8+
@Data
9+
@Entity
10+
public class Product {
11+
12+
@Id
13+
private Long id;
14+
15+
private String name;
16+
17+
private Double price;
18+
19+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package carmelo.spring.data.repository;
2+
3+
import carmelo.spring.data.model.Product;
4+
import org.springframework.data.querydsl.QueryDslPredicateExecutor;
5+
import org.springframework.data.repository.CrudRepository;
6+
7+
public interface ProductRepository extends
8+
CrudRepository<Product, Long>,
9+
QueryDslPredicateExecutor<Product> {
10+
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
INSERT INTO Customer VALUES(0,'Laura','Steel','429 Seventh Av.','Dallas');
2+
INSERT INTO Product VALUES(0,'Iron Iron',54);
3+
INSERT INTO Customer VALUES(1,'Susanne','King','366 - 20th Ave.','Olten');
4+
INSERT INTO Product VALUES(1,'Chair Shoe',248);
5+
INSERT INTO Customer VALUES(2,'Anne','Miller','20 Upland Pl.','Lyon');
6+
INSERT INTO Product VALUES(2,'Telephone Clock',248);
7+
INSERT INTO Customer VALUES(3,'Michael','Clancy','542 Upland Pl.','San Francisco');
8+
INSERT INTO Product VALUES(3,'Chair Chair',254);
9+
INSERT INTO Customer VALUES(4,'Sylvia','Ringer','365 College Av.','Dallas');
10+
INSERT INTO Product VALUES(4,'Ice Tea Shoe',128);
11+
INSERT INTO Customer VALUES(5,'Laura','Miller','294 Seventh Av.','Paris');
12+
INSERT INTO Product VALUES(5,'Clock Clock',236);
13+
INSERT INTO Customer VALUES(6,'Laura','White','506 Upland Pl.','Palo Alto');
14+
INSERT INTO Product VALUES(6,'Ice Tea Chair',98);
15+
INSERT INTO Customer VALUES(7,'James','Peterson','231 Upland Pl.','San Francisco');
16+
INSERT INTO Product VALUES(7,'Telephone Shoe',84);
17+
INSERT INTO Customer VALUES(8,'Andrew','Miller','288 - 20th Ave.','Seattle');
18+
INSERT INTO Product VALUES(8,'Ice Tea Clock',226);
19+
INSERT INTO Customer VALUES(9,'James','Schneider','277 Seventh Av.','Berne');
20+
INSERT INTO Product VALUES(9,'Clock Telephone',172);
21+
INSERT INTO Customer VALUES(10,'Anne','Fuller','135 Upland Pl.','Dallas');
22+
INSERT INTO Product VALUES(10,'Telephone Ice Tea',204);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CREATE TABLE Customer(
2+
ID INTEGER PRIMARY KEY,
3+
FirstName VARCHAR(20),
4+
LastName VARCHAR(30),
5+
Street VARCHAR(50),
6+
City VARCHAR(25));
7+
8+
CREATE TABLE Product(
9+
ID INTEGER PRIMARY KEY,
10+
Name VARCHAR(30),
11+
Price DECIMAL);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /