[英]how to use non-static method generics?
I want to get sql by following code :我想通过以下代码获取 sql:
public class Mappers {
public static class SqlBuilder {
//SqlImpl class implements Sql, and select,from,method,alias,where return this.
public static <T> SqlImpl<T> builder() {
return new SqlImpl<>();
}
}
}
Define the relationship定义关系
public interface Join<T> {
//select * from tab_a a left join tab_b b on a.col1 = b.col2
Join<T> leftJoin(Sql<T> Sql);
//select * from tab_a a left join tab_b b `on a.col1 = b.col2`
// 1.Function implements Serializable
// 2.class.getDeclaredMethod("writeReplace")
// 3.get field name
<T1,T2> Sql<T> on(Function<T1,Object> bf1,Function<T2,Object> bf2)
}
Sql interface sql接口
public interface Sql<T> extends Join<T> {
// select a,b,c ...
Sql<T> select(BuilderFunction<T, Object>... t);
// from tab_a,tab_b
Sql<T> from(Class<T> t);
// select * from tab_a a
Sql<T> alias(String name);
//select * from tab_a where col = ?
Sql<T> where(SqlCondition condition);
// get sql
String build();
}
I want to use function like:我想使用如下功能:
Mappers.SqlBuilder.<A>builder()
.select(A::getAId,A::getName)
.from(A.class)
.where(new SqlCondition().andEquals(A::getName,"x"))
.leftJoin(Mappers.SqlBuilder.<B>builder()
.select(B::getAId,B::getBId)
.from(B.class)
.alias("b_tab")
)
.on(A::getAId,B::getAId); // error: Non-static method cannot be referenced from a static context`.
A and B甲乙
@Table(name="a_tab")
class A {
private Long aId;
private String name;
//getter setter
}
@Table(name="b_tab")
class B {
private Long bId;
private Long aId;
//getter setter
}
on
, I can't use on( A::getAId,B::getAId
), I get error: Non-static method cannot be referenced from a static context
.on
方法,我不能使用 on( A::getAId,B::getAId
),我收到error: Non-static method cannot be referenced from a static context
。Function<A,Long> af = A::getAId;
Function<A,Long> af = A::getAId;
different A::getAId
A::getAId
Modify Join<T> leftJoin(Sql<T> Sql);
修改
Join<T> leftJoin(Sql<T> Sql);
to Join<T> leftJoin(Sql Sql);
Join<T> leftJoin(Sql Sql);
, full source code: ,完整源代码:
import javax.persistence.Table;
import java.util.function.Function;
interface Join<T> {
//select * from tab_a a left join tab_b b on a.col1 = b.col2
Join<T> leftJoin(Sql Sql);
//select * from tab_a a left join tab_b b `on a.col1 = b.col2`
// 1.Function implements Serializable
// 2.class.getDeclaredMethod("writeReplace")
// 3.get field name
<T1,T2> Sql<T> on(Function<T1,Object> bf1, Function<T2,Object> bf2);
}
interface BuilderFunction<T,R> {
R build(T t);
}
class SqlCondition {
public <T> SqlCondition andEquals(BuilderFunction<T, String> f, String x) {
return this;
}
}
interface Sql<T> extends Join<T> {
// select a,b,c ...
Sql<T> select(BuilderFunction<T, Object>... t);
// from tab_a,tab_b
Sql<T> from(Class<T> t);
// select * from tab_a a
Sql<T> alias(String name);
//select * from tab_a where col = ?
Sql<T> where(SqlCondition condition);
// get sql
String build();
}
class SqlImpl<T> implements Sql<T> {
@Override
public Join<T> leftJoin(Sql Sql) {
return null;
}
@Override
public <T1, T2> Sql<T> on(Function<T1, Object> bf1, Function<T2, Object> bf2) {
return null;
}
@Override
public Sql<T> select(BuilderFunction<T, Object>... t) {
return null;
}
@Override
public Sql<T> from(Class<T> t) {
return null;
}
@Override
public Sql<T> alias(String name) {
return null;
}
@Override
public Sql<T> where(SqlCondition condition) {
return null;
}
@Override
public String build() {
return null;
}
}
@Table(name="a_tab")
class A {
private Long aId;
private String name;
public String getName() {
return name;
}
public Long getAId() {
return aId;
}
//getter setter
}
@Table(name="b_tab")
class B {
private Long bId;
private Long aId;
public Long getAId() {
return aId;
}
public Long getBId() {
return bId;
}
//getter setter
}
public class Mappers {
public static class SqlBuilder {
//SqlImpl class implements Sql, and select,from,method,alias,where return this.
public static <T> SqlImpl<T> builder() {
return new SqlImpl<>();
}
}
public static void main(String[] args) {
Mappers.SqlBuilder.<A>builder()
.select(A::getAId,A::getName)
.from(A.class)
.where(new SqlCondition().andEquals(A::getName,"x"))
.leftJoin(Mappers.SqlBuilder.<B>builder()
.select(B::getAId,B::getBId)
.from(B.class)
.alias("b_tab")
)
.on(A::getAId,B::getAId); // error: Non-static method cannot be referenced from a static context`.
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.