From 3b90348adae1b71b7768b03cf8582656e8d3459f Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Sun, 1 Oct 2023 19:58:41 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=20Lambda=20=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=20SQL=20=E5=88=97=E6=9E=84=E5=BB=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/coretest/LambdaSqlTest.java | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LambdaSqlTest.java diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LambdaSqlTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LambdaSqlTest.java new file mode 100644 index 00000000..5cad6b71 --- /dev/null +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LambdaSqlTest.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.apache.org/licenses/LICENSE-2.0 + *
+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.coretest; + +import com.github.vertical_blank.sqlformatter.SqlFormatter; +import com.mybatisflex.core.query.QueryWrapper; +import org.junit.Test; + +import static com.mybatisflex.core.query.QueryMethods.allColumns; +import static com.mybatisflex.core.query.QueryMethods.defaultColumns; + +/** + * Lambda 构建 SQL 测试。 + * + * @author 王帅 + * @since 2023-10-01 + */ +public class LambdaSqlTest { + + public static void printSQL(QueryWrapper queryWrapper) { + System.out.println(SqlFormatter.format(queryWrapper.toSQL())); + } + + @Test + public void test01() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select() + .from(Account.class) + .join(Article.class).on(wrapper -> wrapper.where(Article::getAccountId).eq(Account::getId)) + .where(Account::getAge).ge(18); + + printSQL(queryWrapper); + } + + @Test + public void test02() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select(allColumns(Account.class, Article.class)) + .from(Account.class) + .join(Article.class).on(wrapper -> wrapper.where(Article::getAccountId).eq(Account::getId)) + .where(Account::getAge).ge(18); + + printSQL(queryWrapper); + } + + @Test + public void test03() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select(allColumns(Account.class, Article.class)) + .from(Account.class).as("ac") + .join(Article.class).as("ar").on(wrapper -> wrapper.where(Article::getAccountId).eq(Account::getId)) + .where(Account::getAge).ge(18); + + printSQL(queryWrapper); + } + + @Test + public void test04() { + @SuppressWarnings("unchecked") + QueryWrapper queryWrapper = QueryWrapper.create() + .select(defaultColumns(Account.class)) + .select(Article::getTitle, Article::getContent) + .from(Account.class).as("ac") + .join(Article.class).as("ar").on(wrapper -> wrapper.where(Article::getAccountId).eq(Account::getId)) + .where(Account::getAge).ge(18); + + printSQL(queryWrapper); + } + + @Test + public void test05() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select(defaultColumns(Account.class)) + .select(allColumns(Article.class)) + .from(Account.class).as("ac") + .join(Article.class).as("ar").on(wrapper -> wrapper.where(Article::getAccountId).eq(Account::getId)) + .where(Account::getAge).ge(18); + + printSQL(queryWrapper); + } + +}