optimize FlexConfiguration.addMapper

This commit is contained in:
开源海哥 2023-06-30 20:19:46 +08:00
parent be445c5907
commit 206e6874de
12 changed files with 238 additions and 28 deletions

View File

@ -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 <T> void addMapper(Class<T> 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> T getMapper(Class<T> type, SqlSession sqlSession) {
T mapper = super.getMapper(type, sqlSession);

View File

@ -0,0 +1,69 @@
/**
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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<Account> accounts = accountMapper.selectListByQuery(QueryWrapper.create());
List<Article> articles = articleMapper.selectListByQuery(QueryWrapper.create());
}
System.out.println(">>>>>>finished!!!");
}
}

View File

@ -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

View File

@ -64,11 +64,11 @@
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.h2database</groupId>-->
<!-- <artifactId>h2</artifactId>-->
<!-- <version>2.1.214</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.yaml</groupId>-->

View File

@ -0,0 +1,7 @@
package com.mybatisflex.test.mapper;
import com.mybatisflex.core.BaseMapper;
public interface MyBaseMapper<T> extends BaseMapper<T> {
}

View File

@ -28,15 +28,15 @@ public class Account extends BaseEntity<String, Long, String> {
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;

View File

@ -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<Long> {
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 + '\'' +
'}';
}
}

View File

@ -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

View File

@ -1,3 +1,9 @@
INSERT INTO tb_account
VALUES (1, 'Michael Yang', 18, '2020-01-11'),
(2, 'Zhang san', 19, '2021-03-21');
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);

View File

@ -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
);

View File

@ -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);
}

View File

@ -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<Account> accounts = accountMapper.selectListByQuery(QueryWrapper.create());
List<Article> articles = articleMapper.selectListByQuery(QueryWrapper.create());
}
System.out.println(">>>>>>finished!!!");
}
}