SpringDataJPA 整合QueryDSL

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

生成查询模板插件

  
                com.mysema.maven
                apt-maven-plugin
                1.1.3
                
                    
                        
                            process
                        
                        
                            target/generated-sources/java
                            com.querydsl.apt.jpa.JPAAnnotationProcessor
                        
                    
                
            

pom.xml配置依赖

 
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            org.projectlombok
            lombok
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        

        
            org.postgresql
            postgresql
            ${postgresql.version}
        
        
            org.springframework.boot
            spring-boot-starter-test
        
        
            jaxen
            jaxen
            1.2.0
        
        
        
            com.querydsl
            querydsl-jpa
        
        
            com.querydsl
            querydsl-apt
            provided
        

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命令
SpringDataJPA 整合QueryDSL_第1张图片
上步骤执行完毕后,会在你的target中自动生成了QUser.class类:
SpringDataJPA 整合QueryDSL_第2张图片

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

    private static final long serialVersionUID = -567285970L;

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

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

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

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

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

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

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

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

    public QUser(Path path) {
        super(path.getType(), path.getMetadata());
    }

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

}

然后就可以进行查询了
SpringDataJPA 整合QueryDSL_第3张图片
参考

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

你可能感兴趣的:(笔记,spring)