是否有任何库将 SQL 查询表示为 Java 代码中的对象? [英] Is there any library to represent SQL queries as objects in Java code?
问题描述
我想知道是否有任何库可用于将 SQL 查询表示为 Java 中的对象.
I was wondering if there is any library that can be used to represent SQL queries as objects in Java.
在代码中,我有大量 java.lang.String 类型的静态变量,它们是手写的 SQL 查询.我会寻找具有良好 fluent API 的库,它允许我将查询表示为对象而不是字符串.
In the code I have plenty of static variables of type java.lang.String that are hand written SQL queries. I would be looking for library having a nice fluent API that allows me to represent the queries as objects rather than strings.
示例:
Query q = select("DATE", "QUOTE")
.from("STOCKMARKET")
.where(eq("CORP", "?"))
.orderBy("DATE", DESC);
推荐答案
Jequel 看起来很漂亮:http://www.jequel.de/
Jequel looks pretty nifty: http://www.jequel.de/
它使用流畅的界面,因此很容易阅读,几乎就像自然的 SQL(来自文档):
It uses a fluent interface, so it's easy to read, almost like natural SQL (from the docs):
SqlString sql = select(ARTICLE.OID)
.from(ARTICLE, ARTICLE_COLOR)
.where(ARTICLE.OID.eq(ARTICLE_COLOR.ARTICLE_OID)
.and(ARTICLE.ARTICLE_NO.is_not(NULL)));
它还支持对带参数的数据源执行查询,因此它也处理参数化查询的创建.
It also supports executing queries against a DataSource with parameters, so it handles creation of parameterized queries as well.
这篇关于是否有任何库将 SQL 查询表示为 Java 代码中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!