**废话不多说 直接代码
**
1.导入依赖
导入SpringDataJpa、QueryDSL依赖。自己根据需要导入数据库需要的依赖。
<!-- QueryDSL-->
<dependency>
<groupId>com.njbx</groupId>
<artifactId>njbx-convention</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
</dependency>
<!-- SpringDataJPA-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 忽略数据库依赖-->
2.添加queryDSL插件
根据官网的提示,添加query生成查询类的插件。
<project>
<build>
<plugins>
<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>
</plugins>
</build>
</project>
3.编写实体类
@Entity
@Getter
@Setter
public class CompanyInfo {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private BusinessLicenseInfo businessLicenseInfo;
@Schema(description = "该公司下app集合")
@OneToMany(targetEntity = ClientApp.class,mappedBy = "companyInfo")
private List<ClientApp> clientApps= new ArrayList<>();
}
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
public class BusinessLicenseInfo implements Serializable {
private String licenseCopy;
private String licenseNumber;
private String merchantName;
private String legalPerson;
}
4.生成查询类
执行maven的complete。
第2步导入的插件会自动生成所有的实体类(包括内嵌类如上面的**BusinessLicenseInfo**类)的查询类,生成的目录第2布指定的在**target/generated-sources/java**中
生成的查询类如下 如何使用后面再说
CompanyInfo伴生查询QCompanyInfo类
/**
* QCompanyInfo is a Querydsl query type for CompanyInfo
*/
@Generated("com.querydsl.codegen.EntitySerializer")
public class QCompanyInfo extends EntityPathBase<CompanyInfo> {
private static final long serialVersionUID = -1343250178L;
private static final PathInits INITS = PathInits.DIRECT2;
public final QBusinessLicenseInfo businessLicenseInfo;
public final ListPath<com.njbx.app.entities.ClientApp, com.njbx.app.entities.QClientApp> clientApps = this.<com.njbx.app.entities.ClientApp, com.njbx.app.entities.QClientApp>createList("clientApps", com.njbx.app.entities.ClientApp.class, com.njbx.app.entities.QClientApp.class, PathInits.DIRECT2);
public final NumberPath<Long> id = createNumber("id", Long.class);
public QCompanyInfo(String variable) {
this(CompanyInfo.class, forVariable(variable), INITS);
}
public QCompanyInfo(Path<? extends CompanyInfo> path) {
this(path.getType(), path.getMetadata(), PathInits.getFor(path.getMetadata(), INITS));
}
public QCompanyInfo(PathMetadata metadata) {
this(metadata, PathInits.getFor(metadata, INITS));
}
public QCompanyInfo(PathMetadata metadata, PathInits inits) {
this(CompanyInfo.class, metadata, inits);
}
public QCompanyInfo(Class<? extends CompanyInfo> type, PathMetadata metadata, PathInits inits) {
super(type, metadata, inits);
this.businessLicenseInfo = inits.isInitialized("businessLicenseInfo") ? new QBusinessLicenseInfo(forProperty("businessLicenseInfo")) : null;
}
}
BusinessLicenseInfo伴生查询QBusinessLicenseInfo类
/**
* QBusinessLicenseInfo is a Querydsl query type for BusinessLicenseInfo
*/
@Generated("com.querydsl.codegen.EmbeddableSerializer")
public class QBusinessLicenseInfo extends BeanPath<BusinessLicenseInfo> {
private static final long serialVersionUID = -1470426142L;
public static final QBusinessLicenseInfo businessLicenseInfo = new QBusinessLicenseInfo("businessLicenseInfo");
public final StringPath legalPerson = createString("legalPerson");
public final StringPath licenseCopy = createString("licenseCopy");
public final StringPath licenseNumber = createString("licenseNumber");
public final StringPath merchantName = createString("merchantName");
public QBusinessLicenseInfo(String variable) {
super(BusinessLicenseInfo.class, forVariable(variable));
}
public QBusinessLicenseInfo(Path<? extends BusinessLicenseInfo> path) {
super(path.getType(), path.getMetadata());
}
public QBusinessLicenseInfo(PathMetadata metadata) {
super(BusinessLicenseInfo.class, metadata);
}
}
5.创建Dao层操作类
就像SpringDataJPA使用的一样,只不过需要格外继承一个接口QuerydslPredicateExecutor。
@Repository
public interface CompanyInfoRepository extends JpaRepository<CompanyInfo,Long>,
QuerydslPredicateExecutor<CompanyInfo> {
}
6.书写业务Service类和业务逻辑
//查出所有的实体
companyInfoRepository.findAll();
//查询数据库中和probe不为null的属性一样的实体--不会关联查询
//下面相当于jpa命名的findAllCompanyId("123456")
final CompanyInfo probe = new CompanyInfo();
probe.setCompanyId("123456");
final ClientApp clientApp = new ClientApp();clientApp.serName("蒙古上单");//这里是没用的
probe.setClientApps(List.of(clientApp));
companyInfoRepository.findAll(Example.of(probe))
利用QueryDSL生成的查询类来完成动态sql拼接
下面调用的是QuerydslPredicateExecutor<T>#Iterable<T> findAll(Predicate predicate)
-----查出CompanyInfo.id等于参数id,并且clientApps中有任意一个的名字包含参数name
注意like方法并不会给你添加% 你得自己添加%
public List<CompanyInfo> getList(Long id,String name){
final QCompanyInfo qCompanyInfo= QCompanyInfo .companyInfo;
return (List<CompanyInfo> companyInfoRepository.findAll(qCompanyInfo.id.eq(id)
.and(qCompanyInfo.clientApps.any.name.like("%"+name+"%")));