easybest / spring-data-mybatis

Simplifies the development of creating a MyBatis-based data access layer.
https://sdm.easybest.io
Apache License 2.0
237 stars 73 forks source link

Querydsl support #222

Open easybest opened 4 years ago

easybest commented 4 years ago

Querydsl is a framework that enables the construction of statically typed SQL-like queries through its fluent API.

Spring Data Mybatis offer integration with Querydsl through QuerydslPredicateExecutor, as shown in the following example:

public interface QuerydslPredicateExecutor<T> {

  Optional<T> findById(Predicate predicate);  

  Iterable<T> findAll(Predicate predicate);   

  long count(Predicate predicate);            

  boolean exists(Predicate predicate);        

  // … more functionality omitted.
}

To make use of Querydsl support, extend QuerydslPredicateExecutor on your repository interface, as shown in the following example

interface UserRepository extends CrudRepository<User, Long>, QuerydslPredicateExecutor<User> {
}

The preceding example lets you write typesafe queries using Querydsl Predicate instances, as shown in the following example:

Predicate predicate = user.firstname.equalsIgnoreCase("dave")
    .and(user.lastname.startsWithIgnoreCase("mathews"));

userRepository.findAll(predicate);

How to use?

Add dependencies to you pom.xml

                <dependency>
            <groupId>io.easybest</groupId>
            <artifactId>spring-data-mybatis-querydsl</artifactId>
            <version>${revision}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-sql</artifactId>
            <version>${querydsl}</version>
            <optional>true</optional>
        </dependency>

spring-data-mybatis-querydsl use APT to generate query code

kopax commented 4 years ago

Good afternoon @easybest,

I wanted to take your attention on https://github.com/easybest/spring-data-mybatis/issues/189#issuecomment-683533742 , from what I have learn in v1, it is not possible to migrate to v2 without any further notices.

We use a lot of @Conditions coupled with XML before mappers in our spring-data-mybatis 1 application and so far, the example does not use before mappers (nor can execute all entities without errors).

I also take your attention on https://github.com/easybest/spring-data-mybatis/issues/215#issuecomment-683161764 , with a v2 documentation or v2 migration guide, this will solve #189, is there an estimated time of arrival for v2 documentation ?

Or before mappers completely removed ?

easybest commented 4 years ago

before mapper just marked the mapper files been executed before spring data mybatis automatic generate mappers. But in v2, it used mybatis starter from mybatis official, so you can define any mapper files if you want, and it will be added to the mybatis context before spring data mybatis, so theoretically, before mappers still can be used in a more reasonable way. If you want, you can show me some part of your project, and let me see that how to migrate it from V1.

@conditions was marked as @Deprecated, it is not a good(normative) way to condition query. Instead of it, in v2, there's new ways to resolve the probleam.

  1. use spring data example query;
  2. use mybatis example query;
  3. use querydsl detail document is writing and it will be published in https://sdm.easybest.io/
easybest commented 4 years ago

and in my roadmap, I will maintain the v1 version after v2 features completed, and v1 will migrate all new features from v2.