From 3108d8075fb38a3f22d18669380560dbbe6cb33f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Mon, 17 Apr 2023 15:55:20 +0800 Subject: [PATCH] =?UTF-8?q?fixed=EF=BC=9A=E5=9C=A8=E6=9F=90=E4=BA=9B?= =?UTF-8?q?=E5=9C=BA=E6=99=AF=E4=B8=8B=EF=BC=8C=E5=8A=A8=E6=80=81=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E6=97=B6=EF=BC=8C=E7=94=9F=E6=88=90=E7=9A=84SQL?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E3=80=82=20close=20#I6W89G?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/mybatisflex/core/query/Brackets.java | 15 ++++++-- .../java/com/mybatisflex/test/Account.java | 27 +++++++++++++ .../mybatisflex/test/EntityTestStarter.java | 38 +++++++++++++++---- .../java/com/mybatisflex/test/TypeEnum.java | 16 ++++++++ 4 files changed, 85 insertions(+), 11 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Brackets.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Brackets.java index 843e5759..a287ff71 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Brackets.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Brackets.java @@ -16,6 +16,7 @@ package com.mybatisflex.core.query; import com.mybatisflex.core.dialect.IDialect; +import com.mybatisflex.core.util.StringUtil; import java.util.List; @@ -58,11 +59,17 @@ public class Brackets extends QueryCondition { StringBuilder sql = new StringBuilder(); if (checkEffective()) { String childSql = childCondition.toSql(queryTables, dialect); - QueryCondition effectiveBefore = getEffectiveBefore(); - if (effectiveBefore != null) { - childSql = effectiveBefore.connector + "(" + childSql + ")"; + if (StringUtil.isNotBlank(childSql)) { + QueryCondition effectiveBefore = getEffectiveBefore(); + if (effectiveBefore != null) { + childSql = effectiveBefore.connector + "(" + childSql + ")"; + } + sql.append(childSql); + } else { + //all child conditions is not effective + //fixed gitee #I6W89G + this.effective = false; } - sql.append(childSql); } if (this.next != null) { diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java index f659c69c..6b7cb7d8 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/Account.java @@ -33,6 +33,9 @@ public class Account extends BaseAccount implements Serializable { private TypeEnum typeEnum; +// @Column(isLogicDelete = true) +// private boolean deleteFlag; + public Long getId() { return id; @@ -81,6 +84,30 @@ public class Account extends BaseAccount implements Serializable { options.put(key, value); } + public String getDeptId() { + return deptId; + } + + public void setDeptId(String deptId) { + this.deptId = deptId; + } + + public TypeEnum getTypeEnum() { + return typeEnum; + } + + public void setTypeEnum(TypeEnum typeEnum) { + this.typeEnum = typeEnum; + } + +// public boolean isDeleteFlag() { +// return deleteFlag; +// } +// +// public void setDeleteFlag(boolean deleteFlag) { +// this.deleteFlag = deleteFlag; +// } + @Override public String toString() { return "Account{" + diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java index 6b8932bd..f8154410 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java @@ -16,14 +16,18 @@ package com.mybatisflex.test; import com.mybatisflex.core.MybatisFlexBootstrap; -import com.mybatisflex.core.query.QueryWrapper; -import org.apache.ibatis.logging.stdout.StdOutImpl; +import com.mybatisflex.core.audit.AuditManager; +import com.mybatisflex.core.audit.ConsoleMessageCollector; +import com.mybatisflex.core.audit.MessageCollector; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import javax.sql.DataSource; import java.util.List; +import static com.mybatisflex.core.query.QueryMethods.select; +import static com.mybatisflex.test.table.Tables.ACCOUNT; + public class EntityTestStarter { public static void main(String[] args) { @@ -35,10 +39,15 @@ public class EntityTestStarter { MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance() .setDataSource(dataSource) - .setLogImpl(StdOutImpl.class) .addMapper(AccountMapper.class) .start(); + //开启审计功能 + AuditManager.setAuditEnable(true); + +//设置 SQL 审计收集器 + MessageCollector collector = new ConsoleMessageCollector(); + AuditManager.setMessageCollector(collector); // //查询 ID 为 1 的数据 // Account account = bootstrap.execute(AccountMapper.class, accountMapper -> @@ -46,14 +55,29 @@ public class EntityTestStarter { // System.out.println(account); AccountMapper accountMapper = bootstrap.getMapper(AccountMapper.class); - Account account = accountMapper.selectOneById(1); +// Account account = accountMapper.selectOneById(1); - List accounts = accountMapper.selectAll(); + +// QueryWrapper query = QueryWrapper.create().where(SYS_CONFIG.TYPE.eq(type).when(StrChecker.isNotBlank(type))) +// .and(SYS_CONFIG.NAME.like(word).when(StrChecker.isNotBlank(word)) +// .or(SYS_CONFIG.CODE.like(word).when(StrChecker.isNotBlank(word))) +// .or(SYS_CONFIG.VALUE.like(word).when(StrChecker.isNotBlank(word))) +// .or(SYS_CONFIG.TYPE.like(word).when(StrChecker.isNotBlank(word))) +// ); + + List accounts = accountMapper.selectListByQuery( + select().where(ACCOUNT.AGE.ge(18).when(false)) + .and(ACCOUNT.USER_NAME.like("aaaa").when(false) + .or(ACCOUNT.USER_NAME.like("aaaa").when(false)) + .or(ACCOUNT.USER_NAME.like("aaaa").when(false)) + .or(ACCOUNT.USER_NAME.like("aaaa").when(false)) + ) + ); System.out.println(accounts); // - long l = accountMapper.selectCountByQuery(QueryWrapper.create()); - System.out.println("count: "+ l); +// long l = accountMapper.selectCountByQuery(QueryWrapper.create()); +// System.out.println("count: "+ l); // System.out.println(account); // diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TypeEnum.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TypeEnum.java index d7df7166..d75b36c1 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TypeEnum.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/TypeEnum.java @@ -18,4 +18,20 @@ public enum TypeEnum { this.code = code; this.desc = desc; } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } }