前章已介绍过`APT`,在这里有所涉及,通过配置pom.xml build plugin,当实体类为SLog,那么会生成一个QSLog,理论上写也是可以的(已测试)
> 本框架主要封装常用操作,更多见[官方文档](http://www.querydsl.com/static/querydsl/latest/reference/html/index.html)
### 依赖模块
```
<!-- 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 = "mis_log")
@Entity
@EntityListeners(AuditingEntityListener.class)
@DynamicInsert
@DynamicUpdate
public class LogRecord extends JpaPlusEntity<LogRecord> {
private static final long serialVersionUID = 1L;
/**
* 主键,例(L1408447004119666688)
*/
@Id
@GeneratedValue(generator = "idGenerator")
@GenericGenerator(name = "idGenerator", // 名称
strategy = "com.xiesx.FastBoot.db.jpa.identifier.IdWorkerGenerator", // 生成策略
parameters = {// 生成参数
@Parameter(name = "prefix", value = "L"), // 前缀,L
@Parameter(name = "workerId", value = "1"), // 终端ID,默认0
@Parameter(name = "centerId", value = "1") // 数据中心ID,默认0
})
@JSONField(ordinal = 1)
private String id;
/**
* 创建时间
*/
@CreatedDate
@Column(updatable = false, nullable = false)
@JSONField(ordinal = 2)
private Date createTime;
/**
* 修改时间
*/
@LastModifiedDate
@Column(nullable = false)
@JSONField(ordinal = 3)
private Date updateTime;
/**
* 请求IP
*/
@Column
@JSONField(ordinal = 4)
private String ip;
/**
* 方法
*/
@Column
@JSONField(ordinal = 5)
private String method;
/**
* 方式
*/
@Column
@JSONField(ordinal = 6)
private String type;
/**
* 地址
*/
@Column
@JSONField(ordinal = 7)
private String url;
/**
* 请求参数
*/
@JSONField(serialize = false)
private String req;
/**
* 响应结果
*/
@JSONField(serialize = false)
private String res;
/**
* 执行时间(毫秒)
*/
@Column
@JSONField(ordinal = 10)
private Long t;
/**
* 操作人
*/
@LastModifiedBy
@Column
@JSONField(ordinal = 11, serialize = false)
private String opt;
// ======================
@Transient
@JSONField(serialize = false, ordinal = 8)
public Object getParams() {
return JSON.parse(req);
}
@Transient
@JSONField(serialize = false, ordinal = 9)
public Object getResult() {
return JSON.parse(res);
}
}
```
Q实体类
```
@Generated("com.querydsl.codegen.EntitySerializer")
public class QLogRecord extends EntityPathBase<LogRecord> {
private static final long serialVersionUID = 261544295L;
public static final QLogRecord logRecord = new QLogRecord("logRecord");
public final DateTimePath<java.util.Date> createTime = createDateTime("createTime", java.util.Date.class);
public final StringPath id = createString("id");
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 DateTimePath<java.util.Date> updateTime = createDateTime("updateTime", java.util.Date.class);
public final StringPath url = createString("url");
public QLogRecord(String variable) {
super(LogRecord.class, forVariable(variable));
}
public QLogRecord(Path<? extends LogRecord> path) {
super(path.getType(), path.getMetadata());
}
public QLogRecord(PathMetadata metadata) {
super(LogRecord.class, metadata);
}
}
```
### 如何使用
方式1
```
@Autowired
JPAQueryFactory mJPAQueryFactory;
```
方式2
```
JPAQueryFactory mJPAQueryFactory = SpringHelper.getBean(JPAQueryFactory.class);
```
查询对象
```
QLogRecord ql = QLogRecord.logRecord;
```
条件查询
```
List<SLog> logs = mJPAQueryFactory.selectFrom(ql)
.where(
ql.method.eq("POST"),
ql.method.eq("GET"))
.fetch();
```
排序查询
```
List<SLog> logs = mJPAQueryFactory.selectFrom(ql)
.orderBy(ql.createDate.desc(),ql.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(
ql.method, ql.type, ql.url)
.from(qSLog)
.fetch();
```
添加
```
待补充
```
修改
```
int row = (int) mJPAQueryFactory.update(qu)
.set(qu.status, 1)
.where(qu.id.in(base.getIds()))
.execute();
```
删除
```
int row = (int) mJPAQueryFactory.delete(qu)
.where(qu.id.in(ids()))
.execute();
```
> 没有什么框架是万能,合适的才是最好的