简体   繁体   English

Java MySQL查询命名参数?

[英]Java MySQL query named parameters?

Is there a way to have named parameters in Java MySQL query? 有没有一种方法可以在Java MySQL查询中命名参数?

Like this: 像这样:

SELECT * FROM table WHERE col1 = :val1 AND col2 = :val2

instead of this: 代替这个:

SELECT * FROM table WHERE col1 = ? AND col2 = ?

UPDATE: Im' using java.sql.* , however would be interested in alternatives capable of this. 更新:我正在使用java.sql.* ,但是会对能够实现此目的的替代方案感兴趣。

Maybe Hibernate is good choice for you. 也许休眠是您的好选择。 It provided the query style as your description, and it's so powerful and convenient to do persistence work that you'll feel cool. 它提供了查询样式作为您的描述,并且进行持久性工作是如此强大和便捷,您会觉得很酷。 eg 例如

Query query = sesion.createQuery("from Student s where s.age = :age");
query.setProperties(student);


see the doc: http://docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Query.html#setParameter(java.lang.String, java.lang.Object) 请参阅文档: http : //docs.jboss.org/hibernate/orm/3.2/api/org/hibernate/Query.html#setParameter(java.lang.String,java.lang.Object)

出色的JDBI库使您可以做到这一点以及更多。

Yes, there are alternatives. 是的,还有其他选择。 By example, with javax.persistence.* you can achieve that quite easily. 例如,使用javax.persistence.*可以很容易地实现这一目标。

http://docs.oracle.com/javaee/6/tutorial/doc/bnbrg.html http://docs.oracle.com/javaee/6/tutorial/doc/bnbrg.html

An entity manager will allow you to create dynamic (parametrized) queries with the methods EntityManager.createQuery , and EntityManager.createNamedQuery . 实体管理器将允许您使用EntityManager.createQueryEntityManager.createNamedQuery方法创建动态(参数化)查询。

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

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