Skip to content

Kretech/Loulan

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Loulan Build Status

Laravel-Eloquent for java

[TOC]

Why ?

https://www.jianshu.com/p/f3bf0af7a1ea

Overview

//  作者叫 庆,标签为 java 的所有已发布文章
List<Post> posts = Post.query
  .with("author", "tags")				// 预加载,自动把数据组装到 post.author 对象 
  .has("author.name", "庆")				// 一对多嵌套查询
  .has("tags", query -> query.whereIn("name", "java"))	// 多对多嵌套查询(*暂未实现)
  .published()						// 封装好的业务查询(*暂未实现)
  .get();

相当于以下三条 SQL 的结果合并起来。当然,真的想合并到对应的属性里,你还得一堆java代码。现在有了 Loulan,只需要上面几行代码。

--	post
SELECT post.*
FROM post
WHERE status = 'published' AND post.author_id IN (
  SELECT id
  FROM user
  WHERE name = ''
) AND post.id IN (
  SELECT post_id
  FROM post_tag
  WHERE tag_id IN (
    SELECT id
    FROM tag
    WHERE `name` IN ("java")
  )
)

-- author
SELECT *
FROM author
WHERE id IN (post1.id, post2.id, ...);

-- tag
SELECT *
FROM tag
WHERE id IN (
  SELECT tag_id
  FROM tag
  WHERE post_id IN (post1.id, ...)
)

Scope

Plugins

Soft Delete

@SoftDelete()
class Post extends Model { 
}

Custom Query

interface PostQuery extends Query<Post> {
  
  default PostQuery published(Query query) {
    return query.where("status", "published");
  }
  
}

Hooks

Eager Loading

Post post = Post.query.with("author", "tags").first();
System.out.println(post.author.name);

Scalable

@Connection(name = "db0")
@Table(name = "post")
class Post extends Model {
  
  public String getTable() {
    return super.getTable() + getId() % 10;
  }

}

properties:

loulan.connection.default = "db0"

loulan.connections.db0.dsn = "jdbc:mysql://127.0.0.1:3306/test"
loulan.connections.db0.user = "user"

loulan.connections.db1.dsn = "jdbc:mysql://127.0.0.1:3306/test"
loulan.connections.db1.read.user = "reader"
loulan.connections.db1.write.user = "writer"

About

Rocking with your database

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages