前章已介绍过`APT`,在这里有所涉及,通过配置pom.xml build plugin,当实体类为SLog,那么会生成一个QSLog,理论上写也是可以的(未测试) > 本框架主要封装常用操作,更多见[官方文档](http://www.querydsl.com/) ### 依赖模块 ``` <!-- https://mvnrepository.com/artifact/com.querydsl/querydsl-jpa --> <dependency> <groupId>com.querydsl</groupId> <artifactId>querydsl-jpa</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/com.querydsl/querydsl-apt --> <dependency> <groupId>com.querydsl</groupId> <artifactId>querydsl-apt</artifactId> <scope>provided</scope> </dependency> ``` ### 依赖插件 ``` <!-- 编译 --> <build> <finalName>gotv-admin</finalName> <plugins> <!-- QDSL --> <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> <dependencies> <dependency> <groupId>com.querydsl</groupId> <artifactId>querydsl-apt</artifactId> <version>${querydsl.version}</version> </dependency> </dependencies> </plugin> </plugins> </build> ``` ### 生成示例 实体类 ``` @Data @Accessors(chain = true) @EqualsAndHashCode(callSuper = false) @FieldNameConstants(innerTypeName = "FIELDS") @Table(name = "sys_log") @Entity @DynamicInsert @DynamicUpdate public class SLog extends JpaPlusEntity<SLog> { private static final long serialVersionUID = 1L; /** * 主键 */ @Id @GeneratedValue(generator = "idGenerator") @GenericGenerator(name = "idGenerator", strategy = "com.xiesx.fastboot.core.jpa.identifier.IdWorkerGenerator") private Long id; /** * 创建时间 */ @Column private Date createDate; /** * 请求IP */ @Column private String ip; /** * 方法 */ @Column private String method; /** * 方式 */ @Column private String type; /** * 地址 */ @Column private String url; /** * 请求 */ @Column private String req; /** * 响应 */ @Column private String res; /** * 执行时间(毫秒) */ @Column private Long t; /** * 操作人 */ @Column private String opt; } ``` Q实体类 ``` /** * QSLog is a Querydsl query type for SLog */ @Generated("com.querydsl.codegen.EntitySerializer") public class QSLog extends EntityPathBase<SLog> { private static final long serialVersionUID = -894895227L; public static final QSLog sLog = new QSLog("sLog"); public final DateTimePath<java.util.Date> createDate = createDateTime("createDate", java.util.Date.class); public final NumberPath<Long> id = createNumber("id", Long.class); public final StringPath ip = createString("ip"); public final StringPath method = createString("method"); public final StringPath opt = createString("opt"); public final StringPath req = createString("req"); public final StringPath res = createString("res"); public final NumberPath<Long> t = createNumber("t", Long.class); public final StringPath type = createString("type"); public final StringPath url = createString("url"); public QSLog(String variable) { super(SLog.class, forVariable(variable)); } public QSLog(Path<? extends SLog> path) { super(path.getType(), path.getMetadata()); } public QSLog(PathMetadata metadata) { super(SLog.class, metadata); } } ``` ### 如何使用 方式1 ``` @Autowired JPAQueryFactory mJPAQueryFactory; ``` 方式2 ``` JPAQueryFactory mJPAQueryFactory = SpringHelper.getBean(JPAQueryFactory.class); ``` 条件查询 ``` List<SLog> logs = mJPAQueryFactory.selectFrom(qSLog) .where( qSLog.method.eq("POST"), qSLog.method.eq("GET")) .fetch(); ``` 排序查询 ``` List<SLog> logs = mJPAQueryFactory.selectFrom(qSLog) .orderBy(qSLog.createDate.desc(),qSLog.t.asc()) .fetch(); ``` 聚合查询 ``` List<Person> persons = mJPAQueryFactory.selectFrom(person) .where(person.children.size() .eq(JPAExpressions.select(parent.children.size().max()).from(parent))) .fetch(); ``` 投影查询 ``` List<Tuple> tuples = mJPAQueryFactory .select( qSLog.method, qSLog.type, qSLog.url) .from(qSLog) .fetch(); ``` 添加 ``` 待补充 ``` 修改 ``` int row = (int) mJPAQueryFactory.update(qUser) .set(qUser.status, 1) .where(qUser.id.in(base.getIds())) .execute(); ``` 删除 ``` int row = (int) mJPAQueryFactory.delete(qUser) .where(qUser.id.in(ids())) .execute(); ``` > 没有什么框架是万能,合适的才是最好的