我的建议是使用 substring 函数。这是我在本地使用 querydsl v3.2.3 的示例:
QMyTable g = new QMyTable("g");
query = new SQLQuery(connection , dialect);
List<Tuple> l = query.from(g).list(g.mystringfield,
g.mystringfield.substring(g.mystringfield.length().subtract(10),
g.mystringfield.length() ).substring(0, 5).as("mystringfield5"));
for (Tuple t : l) {
System.out.println(t.get(g.mystringfield) + " =====> " + t.get(1, String.class));
}
生成的查询将类似于以下内容:
select g.mystringfield,
(substr(substr(g.mystringfield,(length(g.mystringfield) - ?)+1,length(g.mystringfield)-(length(g.mystringfield) - ?)),1,5)) as mystringfield5
from MyTable g