这么优雅的Java ORM没见过吧!

  Java的ORM框架有很多,但由于Java语言的限制大部分都不够优雅也不够简单,所以作者只能另辟蹊径造轮子了。照旧先看示例代码了解个大概,然后再解释实现原理。

一、ORM示例 1. Insert public CompletableFuture<Void> insert() { var obj = new sys.entities.Demo("MyName"); //构造参数为主键 obj.Age = 100; //设置实体属性的值 return obj.saveAsync(); } 2. Update

更新单个实体(必须具备主键)

public CompletableFuture<Void> update(sys.entities.Demo obj) { obj.Age = 200; return obj.saveAsync(); }

根据条件更新(必须指定条件以防误操作)

public CompletableFuture<?> update() { var cmd = new SqlUpdateCommand<sys.entities.Demo>(); cmd.update(e -> e.City = "Wuxi"); //更新字段 cmd.update(e -> e.Age = e.Age + 1); //更新累加字段 cmd.where(e -> e.Name == "Johne"); //更新的条件 var outs = cmd.output(e -> e.Age); //更新的同时返回指定字段 return cmd.execAsync().thenApply(rows -> { System.out.println("更新记录数: " + rows); System.out.println("返回的值: " + outs.get(0)); return "Done."; }); } 3. Delete

删除单个实体(必须具备主键)

public CompletableFuture<Void> update(sys.entities.Demo obj) { obj.markDeleted(); //先标记为删除状态 return obj.saveAsync(); //再调用保存方法 }

根据条件删除(必须指定条件以防误操作)

public CompletableFuture<?> delete() { var cmd = new SqlDeleteCommand<sys.entities.Demo>(); cmd.where(e -> e.Age < 0 || e.Age > 200); return cmd.execAsync(); } 4. Transaction

  由于作者讨厌隐式事务,所以事务命令必须显式指定。

public CompletableFuture<?> transaction() { var obj1 = new sys.entities.Demo("Demo1"); obj1.Age = 11; var obj2 = new sys.entities.Demo("Demo2"); obj2.Age = 22; return DataStore.DemoDB.beginTransaction().thenCompose(txn -> { //开始事务 return obj1.saveAsync(txn) //事务保存obj1 .thenCompose(r -> obj2.saveAsync(txn)) //事务保存obj2 .thenCompose(r -> txn.commitAsync()); //递交事务 }).thenApply(r -> "Done"); } 5. Sql查询

Where条件

public CompletableFuture<?> query(String key) { var q = new SqlQuery<sys.entities.Demo>(); q.where(e -> e.Age > 10 && e.Age < 80); if (key != null) q.andWhere(e -> e.Name.contains(key)); //拼接条件 return q.toListAsync(); //返回List<sys.entities.Demo> }

分页查询

public CompletableFuture<?> query(int pageSize, int pageIndex) { var q = new SqlQuery<sys.entities.Demo>(); return q.skip(pageSize * pageIndex) .take(pageSize) .toListAsync(); }

结果映射至匿名类

public CompletableFuture<?> query() { var q = new SqlQuery<sys.entities.Demo>(); return q.toListAsync(e -> new Object() { //返回List<匿名类> public final String Name = e.Name; //匿名类属性 = 实体属性表达式 public final int Age = e.Age + 10; public final String Father = e.Parent.Name; }).thenApply(appbox.data.JsonResult::new); }

结果映射至继承的匿名类

public CompletableFuture<?> query() { var q = new SqlQuery<sys.entities.Demo>(); q.where(e -> e.Parent.Name == "Rick"); return q.toListAsync(e -> new sys.entities.Demo() { //返回List<? extens Demo> public final String Father = e.Parent.Name; }); }

结果映射至树状结构列表

public CompletableFuture<?> tree() { var q = new SqlQuery<sys.entities.Demo>(); q.where(t -> t.Name == "Rick"); return q.toTreeAsync(t -> t.Childs); //参数指向EntitySet(一对多成员) }

EntityRef(一对一引用的实体成员)自动Join

public CompletableFuture<?> query() { var q = new SqlQuery<sys.entities.Customer>(); q.where(cus -> cus.City.Name == "Wuxi"); return q.toListAsync(); } 生成的Sql: Select t.* From "Customer" t Left Join "City" j1 On j1."Code"=t."CityCode"

手工指定Join

public CompletableFuture<?> join() { var q = new SqlQuery<sys.entities.Customer>(); var j = new SqlQueryJoin<sys.entities.City>(); q.leftJoin(j, (cus, city) -> cus.CityCode == city.Code); q.where(j, (cus, city) -> city.Name == "Wuxi"); return q.toListAsync(); }

子查询

public CompletableFuture<?> subQuery() { var sq = new SqlQuery<sys.entities.Demo>(); sq.where(s -> s.ParentName == "Rick"); var q = new SqlQuery<sys.entities.Demo>(); q.where(t -> DbFunc.in(t.Name, sq.toSubQuery(s -> s.Name))); return q.toListAsync(); }

GroupBy

public CompletableFuture<?> groupBy() { var q = new SqlQuery<sys.entities.Demo>(); q.groupBy(t -> t.ParentName) //多个可重复 .having(t -> DbFunc.sum(t.Age) > 10); return q.toListAsync(t -> new Object() { public final String group = t.ParentName == null ? "可怜的孩子" : t.ParentName; public final int totals = DbFunc.sum(t.Age); }).thenApply(appbox.data.JsonResult::new); } 二、实现原理

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wppwjd.html