querydsl/querydsl-sql
Kara Hatherly 3b876d14a7 Adds methods to SQLListenerContext to retrieve SQLBindings.
This was added as an alternative to just getting an SQL string. Internally,
SQLListenerContextImpl now only takes SQLBindings and the getSQL and
getSQLStatements methods delegate to SQLBindings#getSQL. This required
shuffling around AbstractSQLQuery and SQL*Clause to create bindings sooner to
pass through to addSQL even though we derive the same data later on inside
AbstractSQLClause#setParameters - I figured this was preferable to larger-scale
refactoring.

Fixes #1839
2017-02-09 10:05:05 +11:00
..
src Adds methods to SQLListenerContext to retrieve SQLBindings. 2017-02-09 10:05:05 +11:00
pom.xml Bump version 2016-09-05 22:35:47 +03:00
README.md Update examples in readme to Querydsl 4 syntax 2016-05-30 20:54:23 +02:00

Querydsl SQL

The SQL module provides integration with the JDBC API.

Maven integration

Add the following dependencies to your Maven project :

<dependency>
  <groupId>com.querydsl</groupId>
  <artifactId>querydsl-sql</artifactId>
  <version>${querydsl.version}</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.6.1</version>
</dependency>

Code generation via Maven

This functionality is also available as a Maven plugin. The presented example can be declared like this in the POM :

<project>
  <build>
    <plugins>
      ...
      <plugin>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-maven-plugin</artifactId>
        <version>${querydsl.version}</version>
        <executions>
          <execution>
            <goals>
              <goal>export</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <jdbcDriver>org.apache.derby.jdbc.EmbeddedDriver</jdbcDriver>
          <jdbcUrl>jdbc:derby:target/demoDB;create=true</jdbcUrl>
          <packageName>com.myproject.domain</packageName>
          <targetFolder>${project.basedir}/target/generated-sources/java</targetFolder>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <version>${derby.version}</version>
          </dependency>
        </dependencies>
      </plugin>
      ...
    </plugins>
  </build>
</project>

Use the goal test-export to add the targetFolder as a test compile source root instead of a compile source root.

Querying

Querying with Querydsl SQL is as simple as this :

QCustomer customer = new QCustomer("c");

SQLTemplates dialect = new HSQLDBTemplates(); // SQL-dialect
SQLQuery<?> query = new SQLQuery<Void>(connection, dialect);
List<String> lastNames = query.select(customer.lastName)
    .from(customer)
    .where(customer.firstName.eq("Bob"))
    .fetch();

For more information on the Querydsl SQL module visit the reference documentation http://www.querydsl.com/static/querydsl/latest/reference/html/ch02s03.html