简体   繁体   English

开源Java库的SQL实用程序功能?

[英]open source Java library of SQL utility functions?

我正在寻找一个SQL实用程序库,该库允许我执行以下操作:使用LIKE转义字符串以进行前缀搜索,或者通过添加术语(带有占位符支持)以编程方式编写查询的WHERE部分。

Projects like Quaere , LIQUidFORM , jaQu , JEQUEL (Java Embedded QUEry Language) all offer a fluent interface to write SQL statements and might be what you're looking for. QuaereLIQUidFORMjaQuJEQUEL (Java嵌入式查询语言)之类的项目都提供了流畅的接口来编写SQL语句,并且可能正是您所需要的。 For example, with JEQUEL: 例如,使用JEQUEL:

public void testParameterExample() {
    final Sql sql = select(ARTICLE.NAME, ARTICLE.ARTICLE_NO)
            .from(ARTICLE)
            .where(ARTICLE.OID.in(named("article_oid"))).toSql();
    assertEquals("select ARTICLE.NAME, ARTICLE.ARTICLE_NO from ARTICLE where ARTICLE.OID in (:article_oid)", sql.toString());

    final Collection<String> articleDesc = sql.executeOn(dataSource)
            .withParams("article_oid", Arrays.asList(10, 11, 12))
            .mapBeans(new BeanRowMapper<ArticleBean, String>() {
                public String mapBean(final ArticleBean bean) {
                    return bean.getArticleNo() + "/" + bean.getName();
                }
            });
    assertEquals(1, articleDesc.size());
    assertEquals("12345/Foobar", articleDesc.iterator().next());
}

More of them at the bottom of the jaQu webpage. jaQu页面底部的更多内容。

A typical ORM framework like hibernate or JPA provides this out of the box. 像hibernate或JPA这样的典型ORM框架提供了开箱即用的功能。

eg in hibernate . 例如在休眠状态。

from Document doc fetch all properties where lower(doc.name) like 'cats%'

will return Document object where the nqme start with cats. 将返回nqme以cats开头的Document对象。

For parameter queries : 对于参数查询:

Query q = s.createQuery("from foo Foo as foo where foo.name=:name and foo.size=:size");
q.setProperties(fooBean); // fooBean has getName() and getSize()
List foos = q.list();

It will also save you from a lot of boilerplate to create all the JDBC objects needed and all the error handling. 它还可以帮助您避免创建所有所需的JDBC对象和所有错误处理。

If you need to stay close to SQL, give iBatis a careful look. 如果您需要靠近SQL,请仔细阅读iBatis。

If you're still thinking about SQL in these low-level terms, I'd say you aren't being object-oriented enough. 如果您仍在以这些底层术语考虑SQL,那么您可能还没有足够的面向对象的知识。

Think about your problems in terms of objects. 考虑对象方面的问题。 You're still too mired in the primitive level. 您仍然沉迷于原始级别。

Consider better abstractions for persistence on top of your model objects: the DAO pattern, Spring JDBC, iBatis, ORM tools like Hibernate, etc. 在模型对象之上考虑更好的持久性抽象:DAO模式,Spring JDBC,iBatis,ORM工具(例如Hibernate等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM