基础配置Java
大约 5 分钟
安装软件
获取最新
https://central.sonatype.com/ 搜索com.easy-query
获取最新安装包
api接口选型
目前easy-query
支持四种api接口方式:字符串属性,lambda属性,代理属性,对象查询四种api接口调用,且可以互相调用
<properties>
<easy-query.version>latest-version</easy-query.version>
</properties>
<!-- 提供了代理模式支持apt模式以非lambda形式的强类型sql语法 -->
<dependency>
<groupId>com.easy-query</groupId>
<artifactId>sql-api-proxy</artifactId>
<version>${easy-query.version}</version>
</dependency>
<!-- 我们使用EntityFileProxy所以不需要引入sql-processor包 -->
<!-- 这边以mysql为例 其实不需要添加下面的包也可以运行,指示默认的个别数据库行为语句没办法生成 -->
<dependency>
<groupId>com.easy-query</groupId>
<artifactId>sql-mysql</artifactId>
<version>${easy-query.version}</version>
</dependency>
<properties>
<easy-query.version>latest-version</easy-query.version>
</properties>
<!-- 提供了代理模式支持apt模式以非lambda形式的强类型sql语法 -->
<dependency>
<groupId>com.easy-query</groupId>
<artifactId>sql-api-proxy</artifactId>
<version>${easy-query.version}</version>
</dependency>
<!-- 提供了apt自动生成代理对象 -->
<dependency>
<groupId>com.easy-query</groupId>
<artifactId>sql-processor</artifactId>
<version>${easy-query.version}</version>
</dependency>
<!-- 这边以mysql为例 其实不需要添加下面的包也可以运行,指示默认的个别数据库行为语句没办法生成 -->
<dependency>
<groupId>com.easy-query</groupId>
<artifactId>sql-mysql</artifactId>
<version>${easy-query.version}</version>
</dependency>
<properties>
<easy-query.version>latest-version</easy-query.version>
</properties>
<!-- 提供了以java语法强类型,如果不引用也可以使用只是无法使用lambda表达式来表示属性只能用字符串 -->
<dependency>
<groupId>com.easy-query</groupId>
<artifactId>sql-api4j</artifactId>
<version>${easy-query.version}</version>
</dependency>
<!-- 这边以mysql为例 其实不需要添加下面的包也可以运行,指示默认的个别数据库行为语句没办法生成 -->
<dependency>
<groupId>com.easy-query</groupId>
<artifactId>sql-mysql</artifactId>
<version>${easy-query.version}</version>
</dependency>
<properties>
<easy-query.version>latest-version</easy-query.version>
</properties>
<!-- 这边以mysql为例 其实不需要添加下面的包也可以运行,指示默认的个别数据库行为语句没办法生成 -->
<dependency>
<groupId>com.easy-query</groupId>
<artifactId>sql-mysql</artifactId>
<version>${easy-query.version}</version>
</dependency>
使用示例
@Data
@Table("t_topic")
@EntityFileProxy //添加这个属性那么Topic对象会代理生成TopicProxy (需要idea build一下当前项目)
//@EntityProxy @EntityFileProxy两个都可以 File是插件处理,不带file的需要额外引入sql-processor是apt生成
public class Topic implements ProxyEntityAvailable<Topic , TopicProxy>{//implements ProxyEntityAvailable<Topic , TopicProxy>接口有插件生成也可以自行添加
@Column(primaryKey = true)
private String id;
private Integer stars;
private String title;
private LocalDateTime createTime;
}
Topic topic = entityQuery.queryable(Topic.class)
.where(o -> {
o.or(()->{
o.id().eq("3");
o.title().like("你好");
});
})
.firstOrNull();
==> Preparing: SELECT `id`,`stars`,`title`,`create_time` FROM `t_topic` WHERE (`id` = ? OR `title` LIKE ?) LIMIT 1
==> Parameters: 3(String),%你好%(String)
<== Time Elapsed: 3(ms)
<== Total: 1
@Data
@Table("t_topic")
@EntityProxy //添加这个属性那么Topic对象会代理生成TopicProxy (需要idea build一下当前项目)
public class Topic {
@Column(primaryKey = true)
private String id;
private Integer stars;
private String title;
private LocalDateTime createTime;
}
TopicProxy table = TopicProxy.createTable();
Topic topic = easyProxyQuery.queryable(table)
.where(filter -> filter.eq(table.id(), "3").or().like(table.title(), "你好"))
.firstOrNull();
==> Preparing: SELECT `id`,`stars`,`title`,`create_time` FROM `t_topic` WHERE (`id` = ? OR `title` LIKE ?) LIMIT 1
==> Parameters: 3(String),%你好%(String)
<== Time Elapsed: 3(ms)
<== Total: 1
@Data
@Table("t_topic")
public class Topic {
@Column(primaryKey = true)
private String id;
private Integer stars;
private String title;
private LocalDateTime createTime;
}
Topic topic = easyQuery.queryable(Topic.class)
.where(t -> t.eq(Topic::getId,"3").or().like(Topic::getTitle,"你好"))
.firstOrNull();
==> Preparing: SELECT `id`,`stars`,`title`,`create_time` FROM `t_topic` WHERE (`id` = ? OR `title` LIKE ?) LIMIT 1
==> Parameters: 3(String),%你好%(String)
<== Time Elapsed: 2(ms)
<== Total: 1
@Data
@Table("t_topic")
public class Topic {
@Column(primaryKey = true)
private String id;
private Integer stars;
private String title;
private LocalDateTime createTime;
}
Topic topic = easyQueryClient.queryable(Topic.class)
.where(t->t.eq("id","3").or().like("title","你好"))
.firstOrNull();
==> Preparing: SELECT `id`,`stars`,`title`,`create_time` FROM `t_topic` WHERE (`id` = ? OR `title` LIKE ?) LIMIT 1
==> Parameters: 3(String),%你好%(String)
<== Time Elapsed: 2(ms)
<== Total: 1
语义上面来讲代理模式最好,更符合sql语法
非spring-boot初始化
说明!!!
使用代理模式的话非springboot环境必须要安装
sql-api-proxy
和sql-processor
,springboot环境starter
已经包含了
<properties>
<easy-query.version>latest-version</easy-query.version>
</properties>
<!-- 提供了代理模式支持apt模式以非lambda形式的强类型sql语法 -->
<dependency>
<groupId>com.easy-query</groupId>
<artifactId>sql-api-proxy</artifactId>
<version>${easy-query.version}</version>
</dependency>
<!-- 提供了apt自动生成代理对象 如果使用EntityFileProxy使用插件那么可以不引入这个包-->
<dependency>
<groupId>com.easy-query</groupId>
<artifactId>sql-processor</artifactId>
<version>${easy-query.version}</version>
</dependency>
<!-- 提供了以java语法强类型,如果不引用也可以使用只是无法使用lambda表达式来表示属性只能用字符串 -->
<dependency>
<groupId>com.easy-query</groupId>
<artifactId>sql-api4j</artifactId>
<version>${easy-query.version}</version>
</dependency>
<!-- 纯lambda表达式模式 -->
<dependency>
<groupId>com.easy-query</groupId>
<artifactId>sql-api-lambda</artifactId>
<version>${easy-query.version}</version>
</dependency>
<!-- 这边以mysql为例 其实不需要添加下面的包也可以运行,指示默认的个别数据库行为语句没办法生成 -->
<dependency>
<groupId>com.easy-query</groupId>
<artifactId>sql-mysql</artifactId>
<version>${easy-query.version}</version>
</dependency>
//非强类型api
EasyQueryClient easyQueryClient = EasyQueryBootstrapper.defaultBuilderConfiguration()
.setDataSource(dataSource)
.useDatabaseConfigure(new MySQLDatabaseConfiguration())
.build();
//强类型api
EasyQuery easyQuery = new DefaultEasyQuery(easyQueryClient);
//强类型api
EasyProxyuery easyProxyQuery = new DefaultEasyProxyQuery(easyQueryClient);
//使用新版本api对象查询
EasyEntityQuery easyEntityQuery = new DefaultEasyEntityQuery(easyQueryClient);
//最新最热
EasyLambdaQueryClient easyLambdaQueryClient = new EasyLambdaQueryClient(easyQueryClient, MethodHandles.lookup());