将lambda解析为sql where语句,后期升级为orm
简单的查询条件
String sqlSimple = new SelectSimple<Person>()
.where(p -> p.getName().startsWith("张") && p.getAge() < 10)
.where(p -> list.contains(p.getId()))
.toSql();
结果为
name LIKE '%张' AND age < '10' AND id IN ('1','2')
复杂的查询条件
List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
String sql = new Select<Person>()
.where(w -> w.where(p -> p.getAge() > 15)
.and(p -> p.getName().startsWith("张"))
.and(p -> p.getId() != "666")
.or(p -> list.contains(p.getId()))
.and(p -> p.getAge() < 15))
.where(w -> w.where(p -> p.getName() != "tom")
.and(p -> p.getName().contains("daily")))
.orderBy(p -> p.getAge())
.toSql();
结果为
(age > '15'AND name LIKE '%张' AND id <> '666' OR id IN ('1','2') AND age < '15')
AND
(name <> 'tom'AND name LIKE '%daily%')
因为Java的ExpressionTree是第三方实现 or 的解析存在问题,只能解析简单的表达式,复杂查询无法解析,只能是以上这种不优雅的写法。