https://beanvalidation.org/
4.9.1 手工调用 `validate` 方法Member newMember = new Member().setNo("100").setName("Pamela").setGender(1).setMobile("15011112222");// Violations occurred in field 'no'Validator.Violation[] violations = newMember.validate();
4.9.2 创建对象时 `validate`Member newMember = new Member().setNo("100000").setName("Pamela").setGender(1).setMobile("15011112222");Member.create(newMember); Member.create(newMember, true); // Skip validation
4.10 自定义SQLMember.execute("DELETE FROM members WHERE name = ?", "Pamela");
5 复杂SQL 编程指南ObjectiveSQL 提供的复杂SQL 编程,其实是对SQL 语法的一种抽象和建模,以Java API 形式进行互相作用,使得复杂SQL 不再以字符串的形式出现在Java 中,从而实现动态化SQL 变得清晰易理解,不同的业务系统也可以基于ObjectiveSQL 对自身业务的再抽象和建模,实现SQL 逻辑的复用 。
5.1 JOIN 查询5.1.1 隐式 JoinMember.Table member = Member.asTable();Order.Table order = Order.asTable();Select select = new Select();select.project(member.no, member.name, count().as("order_count")).from(member, order).where(member.id.eq(order.memberId)).groupBy(member.no, member.name);List<Member> members = select.execute(Member.class);
SELECT `T0`.`NO` , `T0`.`name` , COUNT(*) AS `order_count`FROM `members` AS `T0`, `orders` AS `T1`WHERE (`T0`.`id` = `T1`.`member_id` )GROUP BY `T0`.`NO` , `T0`.`name`
5.1.2 显式JoinMember.Table member = Member.asTable();Order.Table order = Order.asTable();Select select = new Select();select.project(member.no, member.name, count().as("order_count")).from(member).leftOuterJoin(order, order.memberId.eq(member.id)).groupBy(member.no, member.name);List<Member> members = select.execute(Member.class);
SELECT `T0`.`NO` , `T0`.`name` , COUNT(*) AS `order_count`FROM `members` AS `T0`LEFT OUTER JOIN `orders` AS `T1` ON (`T1`.`member_id` = `T0`.`id` )GROUP BY `T0`.`NO` , `T0`.`name`
5.2 分页查询Member.Table member = Member.asTable();Order.Table order = Order.asTable();Paginator<Member> paginator = Databases.getPaginator();Page page = Page.create(0, 20);Select select = new Select();select.project(member.no, member.name, count().as("order_count")).from(member, order).where(member.id.eq(order.memberId)).groupBy(member.no, member.name);PagedList<Member> members = paginator.paginate(page, select, Member.class);
-- Counting SQLSELECT COUNT(*) AS count_FROM (SELECT`T0`.`NO`,`T0`.`name`,COUNT(*) AS `order_count`FROM `members` AS `T0`, `orders` AS `T1`WHERE (`T0`.`id` = `T1`.`member_id`)GROUP BY `T0`.`NO`, `T0`.`name`) T
-- Querying SQLSELECT `T0`.`NO`, `T0`.`name`, COUNT(*) AS `order_count`FROM `members` AS `T0`, `orders` AS `T1`WHERE (`T0`.`id` = `T1`.`member_id`)GROUP BY `T0`.`NO`, `T0`.`name`LIMIT 0, 20
5.3 复杂表达式查询ObjectiveSQL 通过运算符重域技术使得Expression 也可以参与各类运算符计算,从而使得Java 代码变得简单易懂,而不是通过各类运算符方法进行计算 。ObjectiveSQL 表达式计算时并不能够与SQL 表达完匹配,默认情况下所有表达式均可以进行算术运算,在IntelliJ IDEA 中并不能给出完整的提醒,例如:JoinExpression 也可以进行算术运算,此时在IntelliJ IDEA 中并不会出现语法错误的提醒,但在执行运算过程中会抛出
UnsupportedArithmeticalException,该异常为RuntimeException 的子类 。
Order.Table orderTable = Order.asTable();Select select = new Select();select.project((sum(orderTable.amount) / sum(orderTable.quantity) * 100).as("unit_amount")).from(orderTable).where(orderTable.quantity > 30 &&orderTable.salesAt.between("2020-05-01 00:00:00", "2020-05-02 23:59:59")).groupBy(orderTable.memberId);List<Order> orders = select.execute(Order.class);
SELECT ((((SUM(`T0`.`amount` ) / SUM(`T0`.`quantity` ) )) * 100)) AS unit_amountFROM `orders` AS `T0`WHERE ((`T0`.`quantity` > 30)AND `T0`.`sales_at` BETWEEN '2020-05-01 00:00:00' AND '2020-05-02 23:59:59' )GROUP BY `T0`.`member_id`
5.4 动态查询所谓动态查询,实际上就是表达式的构建过程跟随着参数的有无而变化,基于这种使用场景,ObjectiveSQL 设计了一个永真的逻辑表达式EternalExpression,永真表达式是程序上的一种巧妙设计,使得代码逻辑变得更清晰,即使所有参数均未赋值,整个表达式也会存在一个永的表达,确保最终SQL 语句的正常 。
String[] filteredNo = {"202000001", "202000002", "202000003"};int filteredQuantity = 0;Order.Table orderTable = Order.asTable();Select select = new Select();LogicalExpression eternalExpression = new EternalExpression();if(filteredNo.length > 0) {eternalExpression = eternalExpression.and(orderTable.no.in(filteredNo));}if(filteredQuantity != 0) {eternalExpression = eternalExpression.and(orderTable > filteredQuantity);}select.project((sum(orderTable.amount) / sum(orderTable.quantity) * 100).as("unit_amount")).from(orderTable).where(eternalExpression).groupBy(orderTable.memberId);List<Order> orders = select.execute(Order.class);
推荐阅读
- Android开发:使用Kotlin+协程+自定义注解+Retrofit的网络框架
- Java高级用法,写个代理侵入你 ?
- 七种能开运、旺财的花草
- Android开发:当前项目以Module形式引用别的项目的步骤
- Linux内核:虚拟地址到物理地址,是什么时候开始映射
- 阿里架构师整理的 Netty 学习笔记之:Java NIO 网络编程
- Javascript的New、Apply、Bind、Call知多少
- 专门针对chrome谷歌浏览器而开发的一款访问插件
- 纯前端生成设置头像 - 基于Vue3、Ts、Vite
- 冬藏是立冬还是冬至?