其他分享
首页 > 其他分享> > SpringDataJPA 整合QueryDSL

SpringDataJPA 整合QueryDSL

作者:互联网

由于springdataJpa在使用单表查询还十分方便,但是到了后面复杂查询条件,多表查询,这就显得差强人意。于是网上找到DSL

生成查询模板插件

  <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>

pom.xml配置依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>jaxen</groupId>
            <artifactId>jaxen</artifactId>
            <version>1.2.0</version>
        </dependency>
        <!--query dsl -->
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <scope>provided</scope>
        </dependency>

application.properties配置

# server
server.port=8021
server.session.timeout=3600
server.use-forwardheaders=true
server.compression.enabled=true

# log
logging.level.com.yccj.lzlj=TRACE
logging.level.ROOT=INFO
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl=ERROR
logging.file=logs/lzlj.log

spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=1234
spring.datasource.driverClassName=org.postgresql.Driver

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.physical-strategy= org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

spring.resources.static-locations=classpath:static/,file:/

spring.jpa.properties.hibernate.show_sql=true          
spring.jpa.properties.hibernate.format_sql=true        
spring.jpa.properties.hibernate.use_sql_comments=true  

依赖使用到JPAQueryFactory,而JPAQueryFactory在这里依赖使用EntityManager,所以在主类中做如下配置,使得Spring自动帮我们注入EntityManager与自动管理JPAQueryFactory:

import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.EntityManager;

@Configuration
public class QueryDslConfig {

    @Autowired
    private EntityManager entityManager;

    @Bean
    public JPAQueryFactory jpaQueryFactory(){
        return new JPAQueryFactory(entityManager);
    }
}

实体类

@Data
@Entity
@Table(name = "t_user")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer userId;
    @Column(name = "user_name",columnDefinition = "varchar(32) comment '用户姓名'")
    private String userName;
    @Column(name = "password",columnDefinition = "varchar(32) comment '用户密码'")
    private String password;
    @Column(name = "nick_name",columnDefinition = "varchar(32) comment '昵称'")
    private String nickName;
    @Column(name = "birthday",columnDefinition = "DATETIME comment '生日'")
    private Date birthday;
    @Column(name = "uIndex",columnDefinition = " decimal comment '排序号'")
    private BigDecimal uIndex;  //排序号
}

执行maven命令
在这里插入图片描述
上步骤执行完毕后,会在你的target中自动生成了QUser.class类:
在这里插入图片描述

/**
 * QUser is a Querydsl query type for User
 */
@Generated("com.querydsl.codegen.EntitySerializer")
public class QUser extends EntityPathBase<User> {

    private static final long serialVersionUID = -567285970L;

    public static final QUser user = new QUser("user");

    public final DateTimePath<java.util.Date> birthday = createDateTime("birthday", java.util.Date.class);

    public final StringPath nickName = createString("nickName");

    public final StringPath password = createString("password");

    public final NumberPath<java.math.BigDecimal> uIndex = createNumber("uIndex", java.math.BigDecimal.class);

    public final NumberPath<Integer> userId = createNumber("userId", Integer.class);

    public final StringPath userName = createString("userName");

    public QUser(String variable) {
        super(User.class, forVariable(variable));
    }

    public QUser(Path<? extends User> path) {
        super(path.getType(), path.getMetadata());
    }

    public QUser(PathMetadata metadata) {
        super(User.class, metadata);
    }

}

然后就可以进行查询了
在这里插入图片描述
参考

https://blog.csdn.net/qq_43681755/article/details/107511827

标签:SpringDataJPA,jpa,spring,boot,QueryDSL,class,整合,org,public
来源: https://blog.csdn.net/qq_44014971/article/details/119172922