我试图使用querydsl为动态模式构建动态查询.我试图获得只是查询而不必实际执行它.
到目前为止,我遇到了两个问题: - schema.table符号不存在.相反,我只得到表名. - 我已经能够得到查询,但它将变量分开并放入'?' 相反,这是可以理解的.但我想知道是否有某种方法可以获得完全具体化的查询,包括参数.
这是我当前的尝试和结果(我使用MySQLTemplates来创建配置):
private SQLTemplates templates = new MySQLTemplates();
private Configuration configuration = new Configuration(templates);
String table = "sometable"
Path<Object> userPath = new PathImpl<Object>(Object.class, table);
StringPath usernamePath = Expressions.stringPath(userPath, "username");
NumberPath<Long> idPath = Expressions.numberPath(Long.class, userPath, "id");
SQLQuery sqlQuery = new SQLQuery(connection, configuration)
.from(userPath).where(idPath.eq(1l)).limit(10);
String query = sqlQuery.getSQL(usernamePath).getSQL();
return query;
Run Code Online (Sandbox Code Playgroud)
而我得到的是:
select sometable.username
from sometable
where sometable.id = ?
limit ?
Run Code Online (Sandbox Code Playgroud)
我想得到的是:
select sometable.username
from someschema.sometable
where sometable.id = ?
limit ?
Run Code Online (Sandbox Code Playgroud)
更新:我想出了这种黑客来获取参数实现(不理想,并希望更好的解决方案)但仍然 …
在寻找 Java 库以与数据库无关的方式构建查询时,我遇到了许多库,包括 iciql、querydsl、jooq、joist、hibernate 等。
我想要一些不需要配置文件并且可以使用动态模式的东西。对于我的应用程序,我在运行时了解数据库和模式,因此我不会有任何配置文件或模式的域类。
这似乎是 querydsl 的核心目标之一,但是通过 querydsl 的文档,我看到了很多使用域类构建动态查询的示例,但我没有遇到任何解释如何仅使用我有关于架构的动态信息。
Jooq 提供了这样的功能(参见:http : //www.jooq.org/doc/3.2/manual/getting-started/use-cases/jooq-as-a-standalone-sql-builder/)但有一个限制性许可证,如果我想将我的注意力扩展到 Oracle 或 MS SQL(我可能不喜欢但需要支持)。
有 querydsl 经验的人可以让我知道 querydsl 是否可以实现这样的事情,如果可以,如何实现。
如果有人知道任何其他可以满足我的要求的人,我将不胜感激。
Java允许接口中的字段.这在java 5之前有一些用处.今天它们有什么好的用例吗?
有人可以给我一些很好的用例,其中一个人会在接口中使用字段而不是其他许多方法来满足相同的设计要求吗?
实际上,界面允许在某些情况下出现歧义和混淆.以此为例.以下代码:
请注意,我承认之前已经讨论过歧义问题并得到了回答,但这不是我的问题.我的问题在上面以粗体显示.这只是界面中字段的一个潜在副作用的说明.
public class Sample implements Foo,Bar{
public void print() {
System.out.println("Hello"+name);//field name is ambiguous
}
public static void main(String[] args) {
Sample mySample = new Sample();
mySample.print();
}
}
public interface Foo {
String name = "foo";
}
public interface Bar{
String name = "bar";
}
Run Code Online (Sandbox Code Playgroud) 我有一个包含字符串,int和boolean字段的类.我为他们宣布了吸气剂和制定者.
public class SomeClass {
private int id;
private String description;
private boolean active;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
}
Run Code Online (Sandbox Code Playgroud)
我是BeanPropertyRowMapper来从Oracle DB获取所有对象.
@Override
public List<Destination> getAll() {
List<SomeClass> objs = jdbcTemplate.query(
myQuery, new BeanPropertyRowMapper<SomeClass>(SomeClass.class));
return objs;
} …Run Code Online (Sandbox Code Playgroud)