mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-06 16:48:24 +08:00
Merge branch 'main' of gitee.com:mybatis-flex/mybatis-flex into main
Signed-off-by: 王帅 <1474983351@qq.com>
This commit is contained in:
commit
2bd44d354e
@ -23,7 +23,7 @@ public class Article {
|
|||||||
|
|
||||||
```java {9-13}
|
```java {9-13}
|
||||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
.select().form(ARTICLE)
|
.select().from(ARTICLE)
|
||||||
.where(ARTICLE.id.ge(100));
|
.where(ARTICLE.id.ge(100));
|
||||||
|
|
||||||
List<Article> articles = mapper.selectListByQuery(queryWrapper
|
List<Article> articles = mapper.selectListByQuery(queryWrapper
|
||||||
|
|||||||
@ -187,10 +187,10 @@ public class Tables {
|
|||||||
一般的场景中,我们查询内容应该如下:
|
一般的场景中,我们查询内容应该如下:
|
||||||
```java
|
```java
|
||||||
QueryWrapper.create()
|
QueryWrapper.create()
|
||||||
//使用的是 DEFAULT_COLUMNS
|
//使用的是 DEFAULT_COLUMNS
|
||||||
.select(ARTICLE.DEFAULT_COLUMNS)
|
.select(ARTICLE.DEFAULT_COLUMNS)
|
||||||
.form(DEFAULT_COLUMNS)
|
.from(DEFAULT_COLUMNS)
|
||||||
.where(...)
|
.where(...)
|
||||||
```
|
```
|
||||||
|
|
||||||
## isLogicDelete
|
## isLogicDelete
|
||||||
|
|||||||
@ -44,7 +44,10 @@ import org.apache.ibatis.session.*;
|
|||||||
import org.apache.ibatis.transaction.Transaction;
|
import org.apache.ibatis.transaction.Transaction;
|
||||||
import org.apache.ibatis.util.MapUtil;
|
import org.apache.ibatis.util.MapUtil;
|
||||||
|
|
||||||
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.lang.reflect.Proxy;
|
import java.lang.reflect.Proxy;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.lang.reflect.TypeVariable;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
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
|
@Override
|
||||||
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
|
public <T> T getMapper(Class<T> type, SqlSession sqlSession) {
|
||||||
T mapper = super.getMapper(type, sqlSession);
|
T mapper = super.getMapper(type, sqlSession);
|
||||||
|
|||||||
@ -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!!!");
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,5 +1,5 @@
|
|||||||
processor.mappersGenerateEnable=true
|
processor.mappersGenerateEnable=true
|
||||||
processor.entity.ignoreSuffixes=Entity
|
processor.entity.ignoreSuffixes=Entity
|
||||||
#processor.allInTables=true
|
#processor.allInTables=true
|
||||||
#processor.baseMapperClass=com.test.mapper.ExBaseMapper
|
processor.baseMapperClass=com.mybatisflex.test.mapper.MyBaseMapper
|
||||||
|
|
||||||
|
|||||||
@ -64,11 +64,11 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
<!-- <dependency>-->
|
<dependency>
|
||||||
<!-- <groupId>com.h2database</groupId>-->
|
<groupId>com.h2database</groupId>
|
||||||
<!-- <artifactId>h2</artifactId>-->
|
<artifactId>h2</artifactId>
|
||||||
<!-- <version>2.1.214</version>-->
|
<version>2.1.214</version>
|
||||||
<!-- </dependency>-->
|
</dependency>
|
||||||
|
|
||||||
<!-- <dependency>-->
|
<!-- <dependency>-->
|
||||||
<!-- <groupId>org.yaml</groupId>-->
|
<!-- <groupId>org.yaml</groupId>-->
|
||||||
|
|||||||
@ -0,0 +1,7 @@
|
|||||||
|
package com.mybatisflex.test.mapper;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.BaseMapper;
|
||||||
|
|
||||||
|
public interface MyBaseMapper<T> extends BaseMapper<T> {
|
||||||
|
|
||||||
|
}
|
||||||
@ -28,15 +28,15 @@ public class Account extends BaseEntity<String, Long, String> {
|
|||||||
private Integer age;
|
private Integer age;
|
||||||
private Date birthday;
|
private Date birthday;
|
||||||
|
|
||||||
private Gender gender;
|
// private Gender gender;
|
||||||
|
//
|
||||||
public Gender getGender() {
|
// public Gender getGender() {
|
||||||
return gender;
|
// return gender;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
public void setGender(Gender gender) {
|
// public void setGender(Gender gender) {
|
||||||
this.gender = gender;
|
// this.gender = gender;
|
||||||
}
|
// }
|
||||||
|
|
||||||
/*public Long getId() {
|
/*public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
|
|||||||
@ -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 + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,16 +3,20 @@ spring:
|
|||||||
# h2:
|
# h2:
|
||||||
# console:
|
# console:
|
||||||
# enabled: true
|
# 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:
|
datasource:
|
||||||
# driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: org.h2.Driver
|
||||||
url: jdbc:mysql://localhost:3306/flex_test
|
|
||||||
username: root
|
username: root
|
||||||
password: 12345678
|
password: test
|
||||||
driver-class-name:
|
sql:
|
||||||
# sql:
|
init:
|
||||||
# init:
|
schema-locations: classpath:schema.sql
|
||||||
# schema-locations: classpath:schema.sql
|
data-locations: classpath:data.sql
|
||||||
# data-locations: classpath:data.sql
|
|
||||||
mybatis-flex:
|
mybatis-flex:
|
||||||
mapper-locations:
|
mapper-locations:
|
||||||
- classpath*:/mapper/*.xml
|
- classpath*:/mapper/*.xml
|
||||||
|
|||||||
@ -1,3 +1,9 @@
|
|||||||
INSERT INTO tb_account
|
INSERT INTO tb_account
|
||||||
VALUES (1, 'Michael Yang', 18, '2020-01-11'),
|
VALUES (1, '张三', 18, 0,'2020-01-11', null,0),
|
||||||
(2, 'Zhang san', 19, '2021-03-21');
|
(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);
|
||||||
|
|||||||
@ -3,5 +3,18 @@ CREATE TABLE IF NOT EXISTS `tb_account`
|
|||||||
`id` INTEGER PRIMARY KEY auto_increment,
|
`id` INTEGER PRIMARY KEY auto_increment,
|
||||||
`user_name` VARCHAR(100),
|
`user_name` VARCHAR(100),
|
||||||
`age` Integer,
|
`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
|
||||||
);
|
);
|
||||||
@ -22,7 +22,6 @@ import com.mybatisflex.core.row.Row;
|
|||||||
import com.mybatisflex.test.model.Account;
|
import com.mybatisflex.test.model.Account;
|
||||||
import com.mybatisflex.test.model.AccountVO;
|
import com.mybatisflex.test.model.AccountVO;
|
||||||
import com.mybatisflex.test.model.AccountVO2;
|
import com.mybatisflex.test.model.AccountVO2;
|
||||||
import com.mybatisflex.test.model.Gender;
|
|
||||||
import org.junit.jupiter.api.Assertions;
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -88,7 +87,7 @@ class AccountMapperTest {
|
|||||||
void testEnum() {
|
void testEnum() {
|
||||||
Account account = new Account();
|
Account account = new Account();
|
||||||
account.setId(1L);
|
account.setId(1L);
|
||||||
account.setGender(Gender.MALE);
|
// account.setGender(Gender.MALE);
|
||||||
accountMapper.update(account);
|
accountMapper.update(account);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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!!!");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user