From be445c59076347a343c808799cb611bcdcd27ba0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Fri, 30 Jun 2023 09:35:41 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=94=99=E5=88=AB?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/zh/base/field-query.md | 2 +- docs/zh/core/column.md | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/zh/base/field-query.md b/docs/zh/base/field-query.md index dce45ee3..1e69ec42 100644 --- a/docs/zh/base/field-query.md +++ b/docs/zh/base/field-query.md @@ -23,7 +23,7 @@ public class Article { ```java {9-13} QueryWrapper queryWrapper = QueryWrapper.create() - .select().form(ARTICLE) + .select().from(ARTICLE) .where(ARTICLE.id.ge(100)); List
articles = mapper.selectListByQuery(queryWrapper diff --git a/docs/zh/core/column.md b/docs/zh/core/column.md index 5982f2e0..52aeea59 100644 --- a/docs/zh/core/column.md +++ b/docs/zh/core/column.md @@ -187,10 +187,10 @@ public class Tables { 一般的场景中,我们查询内容应该如下: ```java QueryWrapper.create() - //使用的是 DEFAULT_COLUMNS - .select(ARTICLE.DEFAULT_COLUMNS) - .form(DEFAULT_COLUMNS) - .where(...) + //使用的是 DEFAULT_COLUMNS + .select(ARTICLE.DEFAULT_COLUMNS) + .from(DEFAULT_COLUMNS) + .where(...) ``` ## isLogicDelete From 206e6874ded3ba68b4791a2b9be89878282daa19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Fri, 30 Jun 2023 20:19:46 +0800 Subject: [PATCH 2/2] optimize FlexConfiguration.addMapper --- .../core/mybatis/FlexConfiguration.java | 24 +++++++ .../test/MapperProxyCacheTestStarter.java | 69 +++++++++++++++++++ .../mybatis-flex.config | 2 +- .../mybatis-flex-spring-boot-test/pom.xml | 10 +-- .../mybatisflex/test/mapper/MyBaseMapper.java | 7 ++ .../com/mybatisflex/test/model/Account.java | 18 ++--- .../com/mybatisflex/test/model/Article.java | 54 +++++++++++++++ .../src/main/resources/application.yml | 20 +++--- .../src/main/resources/data.sql | 10 ++- .../src/main/resources/schema.sql | 15 +++- .../test/mapper/AccountMapperTest.java | 3 +- .../test/mapper/CombinedMapperTest.java | 34 +++++++++ 12 files changed, 238 insertions(+), 28 deletions(-) create mode 100644 mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MapperProxyCacheTestStarter.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyBaseMapper.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Article.java create mode 100644 mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/CombinedMapperTest.java diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java index 1d04fa57..a38800cf 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/mybatis/FlexConfiguration.java @@ -44,7 +44,10 @@ import org.apache.ibatis.session.*; import org.apache.ibatis.transaction.Transaction; import org.apache.ibatis.util.MapUtil; +import java.lang.reflect.ParameterizedType; import java.lang.reflect.Proxy; +import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; import java.util.Collections; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -309,6 +312,27 @@ public class FlexConfiguration extends Configuration { } + @Override + public void addMapper(Class type) { + Type[] genericInterfaces = type.getGenericInterfaces(); + boolean isGenericInterface = false; + for (Type genericInterface : genericInterfaces) { + if (genericInterface instanceof ParameterizedType) { + Type actualTypeArgument = ((ParameterizedType) genericInterface).getActualTypeArguments()[0]; + if (actualTypeArgument instanceof TypeVariable) { + isGenericInterface = true; + break; + } + } + } + + //不支持泛型类添加 + if (!isGenericInterface) { + super.addMapper(type); + } + } + + @Override public T getMapper(Class type, SqlSession sqlSession) { T mapper = super.getMapper(type, sqlSession); diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MapperProxyCacheTestStarter.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MapperProxyCacheTestStarter.java new file mode 100644 index 00000000..0401c4ec --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/MapperProxyCacheTestStarter.java @@ -0,0 +1,69 @@ +/** + * 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.test; + +import com.mybatisflex.core.MybatisFlexBootstrap; +import com.mybatisflex.core.audit.AuditManager; +import com.mybatisflex.core.audit.ConsoleMessageCollector; +import com.mybatisflex.core.audit.MessageCollector; +import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.mapper.ArticleMapper; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; + +import javax.sql.DataSource; +import java.util.List; + +public class MapperProxyCacheTestStarter { + + public static void main(String[] args) { + DataSource dataSource = new EmbeddedDatabaseBuilder() + .setType(EmbeddedDatabaseType.H2) + .addScript("schema.sql") + .addScript("data.sql") + .build(); + + MybatisFlexBootstrap bootstrap = MybatisFlexBootstrap.getInstance() + .setDataSource(dataSource) + .addMapper(MyBaseMapper.class) + .addMapper(ArticleMapper.class) + .addMapper(AccountMapper.class) + .addMapper(AccountMapper.class) + .start(); + + //开启审计功能 + AuditManager.setAuditEnable(true); + + //设置 SQL 审计收集器 + MessageCollector collector = new ConsoleMessageCollector(); + AuditManager.setMessageCollector(collector); + + + AccountMapper accountMapper = bootstrap.getMapper(AccountMapper.class); + ArticleMapper articleMapper = bootstrap.getMapper(ArticleMapper.class); + + for (int i = 0; i < 10; i++) { + List accounts = accountMapper.selectListByQuery(QueryWrapper.create()); + List

articles = articleMapper.selectListByQuery(QueryWrapper.create()); + } + + + + System.out.println(">>>>>>finished!!!"); + + + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/mybatis-flex.config b/mybatis-flex-test/mybatis-flex-spring-boot-test/mybatis-flex.config index 7263da34..7caf83f7 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/mybatis-flex.config +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/mybatis-flex.config @@ -1,5 +1,5 @@ processor.mappersGenerateEnable=true processor.entity.ignoreSuffixes=Entity #processor.allInTables=true -#processor.baseMapperClass=com.test.mapper.ExBaseMapper +processor.baseMapperClass=com.mybatisflex.test.mapper.MyBaseMapper diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml b/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml index cd800308..4bea3292 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/pom.xml @@ -64,11 +64,11 @@ - - - - - + + com.h2database + h2 + 2.1.214 + diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyBaseMapper.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyBaseMapper.java new file mode 100644 index 00000000..8bc657bc --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/mapper/MyBaseMapper.java @@ -0,0 +1,7 @@ +package com.mybatisflex.test.mapper; + +import com.mybatisflex.core.BaseMapper; + +public interface MyBaseMapper extends BaseMapper { + +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java index ea4ec7e6..96d4b11a 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java @@ -28,15 +28,15 @@ public class Account extends BaseEntity { private Integer age; private Date birthday; - private Gender gender; - - public Gender getGender() { - return gender; - } - - public void setGender(Gender gender) { - this.gender = gender; - } +// private Gender gender; +// +// public Gender getGender() { +// return gender; +// } +// +// public void setGender(Gender gender) { +// this.gender = gender; +// } /*public Long getId() { return id; diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Article.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Article.java new file mode 100644 index 00000000..1a94ed49 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Article.java @@ -0,0 +1,54 @@ +package com.mybatisflex.test.model; + +import com.mybatisflex.annotation.Column; +import com.mybatisflex.annotation.Table; + +@Table(value = "tb_article") +public class Article extends IdEntity { + + private static final long serialVersionUID = 1L; + + + private Long accountId; + + private String title; + + private String content; + + @Column(isLogicDelete = true) + private Boolean isDelete; + + public Long getAccountId() { + return accountId; + } + + public void setAccountId(Long accountId) { + this.accountId = accountId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + @Override + public String toString() { + return "Article{" + + "id=" + id + + ", accountId=" + accountId + + ", title='" + title + '\'' + + ", content='" + content + '\'' + + '}'; + } +} diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml index d04e0990..da226307 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml @@ -3,16 +3,20 @@ spring: # h2: # console: # enabled: true +# datasource: +## driver-class-name: com.mysql.cj.jdbc.Driver +# url: jdbc:mysql://localhost:3306/flex_test +# username: root +# password: 12345678 +# driver-class-name: datasource: -# driver-class-name: com.mysql.cj.jdbc.Driver - url: jdbc:mysql://localhost:3306/flex_test + driver-class-name: org.h2.Driver username: root - password: 12345678 - driver-class-name: -# sql: -# init: -# schema-locations: classpath:schema.sql -# data-locations: classpath:data.sql + password: test + sql: + init: + schema-locations: classpath:schema.sql + data-locations: classpath:data.sql mybatis-flex: mapper-locations: - classpath*:/mapper/*.xml diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/data.sql b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/data.sql index 9a09ed8a..6734810a 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/data.sql +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/data.sql @@ -1,3 +1,9 @@ INSERT INTO tb_account -VALUES (1, 'Michael Yang', 18, '2020-01-11'), - (2, 'Zhang san', 19, '2021-03-21'); \ No newline at end of file +VALUES (1, '张三', 18, 0,'2020-01-11', null,0), + (2, '王麻子叔叔', 19, 1, '2021-03-21', null,0); + + +INSERT INTO tb_article +VALUES (1, 1,'标题1', '内容1',0), + (2, 2,'标题2', '内容2',0), + (3, 1,'标题3', '内容3',0); diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/schema.sql b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/schema.sql index d7e21fac..b0b66881 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/schema.sql +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/schema.sql @@ -3,5 +3,18 @@ CREATE TABLE IF NOT EXISTS `tb_account` `id` INTEGER PRIMARY KEY auto_increment, `user_name` VARCHAR(100), `age` Integer, - `birthday` DATETIME + `sex` Integer, + `birthday` DATETIME, + `options` VARCHAR(1024), + `is_delete` Integer +); + + +CREATE TABLE IF NOT EXISTS `tb_article` +( + `id` INTEGER PRIMARY KEY auto_increment, + `account_id` Integer, + `title` VARCHAR(100), + `content` text, + `is_delete` Integer ); \ No newline at end of file diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AccountMapperTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AccountMapperTest.java index 6c7db8b6..e09df032 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AccountMapperTest.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AccountMapperTest.java @@ -21,7 +21,6 @@ import com.mybatisflex.core.row.Db; import com.mybatisflex.core.row.Row; import com.mybatisflex.test.model.Account; import com.mybatisflex.test.model.AccountVO; -import com.mybatisflex.test.model.Gender; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -86,7 +85,7 @@ class AccountMapperTest { void testEnum() { Account account = new Account(); account.setId(1L); - account.setGender(Gender.MALE); +// account.setGender(Gender.MALE); accountMapper.update(account); } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/CombinedMapperTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/CombinedMapperTest.java new file mode 100644 index 00000000..70ef5ed5 --- /dev/null +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/CombinedMapperTest.java @@ -0,0 +1,34 @@ +package com.mybatisflex.test.mapper; + +import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.test.model.Account; +import com.mybatisflex.test.model.Article; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.List; + +@SpringBootTest +public class CombinedMapperTest { + + @Autowired + private AccountMapper accountMapper; + + @Autowired + private ArticleMapper articleMapper; + + @Test + void testQuery() { + + for (int i = 0; i < 10; i++) { + List accounts = accountMapper.selectListByQuery(QueryWrapper.create()); + List
articles = articleMapper.selectListByQuery(QueryWrapper.create()); + } + + + System.out.println(">>>>>>finished!!!"); + } + + +}