兄弟们: ObjectiveSQL 是我花了很大的经历为开源社区捐献的项目,有人会关注,当然也会有人视而不见,我也很直白的讲出这个项目的目的,我做程序员 15 年左右吧,之前一直在熟人的圈子里,后面创业 8 年,最近遇到一些事情,想重新换一个环境,工作这么多年,总要一个能够客观描述我自身价值的东西,光凭嘴说是不行的,所以就想起搞一个开源项目,ObjectiveSQL 是我为公众贡献的第一个项目,但不会是最后一个,由于我想让老外认可,所在以 github 上的是洋文。
你可能喜欢它,也可能讨厌它,请留下你的意见,更多的希望你看一下项目的源代码,提出更多意见,你们的意见是我成长的动力,感谢...
项目地址: https://github.com/braisdom/ObjectiveSql
现在我详细介绍一下特性:
@DomainModel
public class Member {
private String no;
@Queryable
private String name;
private Integer gender;
private String mobile;
private String otherInfo;
@Relation(relationType = RelationType.HAS_MANY)
private List<Order> orders;
}
数据存储
Member.create(newMember);
Member.create(newMember, true); // Create a member without validating
Member.create(Member.newInstanceFrom(memberHash));
Member.create(new Member[]{newMember1, newMember2, newMember3}, false);
Member.update(1L, newMember, true); // Update a member with primary key
Member.update("name = 'Smith => Jackson'", "name = 'Alice'");
Member.destroy(1L); // Delete a member with primary key
Member.destroy("name = 'Mary'");
// Execute SQL
Member.execute(String.format("DELETE FROM %s WHERE name = 'Mary'", Member.TABLE_NAME));
事务处理
@Transactional
public static void makeOrder(Order order, OrderLine... orderLines) throws SQLException {
Order.create(order, false);
OrderLine.create(orderLines, false);
}
查询与统计
Member.countAll();
Member.count("id > ?", 1);
Member.queryByPrimaryKey(1);
Member.queryFirst("id = ?", 1);
Member.query("id > ?", 1);
Member.queryAll();
分页查询
Page page = Page.create(0, 10);// Create a Page instance with current page and page size
PagedList<Member> members = Member.pagedQueryAll(page, Member.HAS_MANY_ORDERS);
关联对象查询
// Querying objects with convenient methods, and it will carry the related objects
Member.queryAll(Member.HAS_MANY_ORDERS);
Member.queryByPrimary(1, Member.HAS_MANY_ORDERS);
Member.queryByName("demo", Member.HAS_MANY_ORDERS);
...
// SQL programming with Java syntax without losing the features of SQL syntax
Order.Table orderTable = Order.asTable();
Select select = new Select();
select.project(sum(orderTable.amount) / sum(orderTable.quantity) * 100)
.from(orderTable)
.where(orderTable.quantity > 30 &&
orderTable.salesAt.between($("2020-10-10 00:00:00"), $("2020-10-30 23:59:59")))
.groupBy(orderTable.productId);
生成的 SQL
-- SQL syntax is the same as Java syntax
SELECT ((((SUM(`T0`.`amount` ) / SUM(`T0`.`quantity` ) )) * 100))
FROM `orders` AS `T0`
WHERE ((`T0`.`quantity` > 30) AND
`T0`.`sales_at` BETWEEN '2020-10-10 00:00:00' AND '2020-10-30 23:59:59')
GROUP BY `T0`.`product_id`