feat: replace mybatis-plus to mybatis-flex
This commit is contained in:
@ -30,8 +30,8 @@
|
||||
<artifactId>mybatis-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-spring-boot4-starter</artifactId>
|
||||
<groupId>com.jiangyc</groupId>
|
||||
<artifactId>mybatis-flex-spring-boot4-starter</artifactId>
|
||||
</dependency>
|
||||
<!-- Lombok -->
|
||||
<dependency>
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
package day.gitlab.dolphin.common.mybatis.config;
|
||||
|
||||
import com.mybatisflex.annotation.KeyType;
|
||||
import com.mybatisflex.core.FlexGlobalConfig;
|
||||
import com.mybatisflex.core.audit.AuditManager;
|
||||
import com.mybatisflex.core.keygen.KeyGeneratorFactory;
|
||||
import com.mybatisflex.spring.boot.MyBatisFlexCustomizer;
|
||||
import day.gitlab.dolphin.common.mybatis.util.UUIDv7Generator;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class MyBatisFlexConfiguration implements MyBatisFlexCustomizer {
|
||||
|
||||
static {
|
||||
AuditManager.setAuditEnable(true);
|
||||
|
||||
KeyGeneratorFactory.register("uuidv7", new UUIDv7Generator());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void customize(FlexGlobalConfig globalConfig) {
|
||||
FlexGlobalConfig.KeyConfig keyConfig = new FlexGlobalConfig.KeyConfig();
|
||||
keyConfig.setKeyType(KeyType.Sequence);
|
||||
keyConfig.setValue("uuidv7");
|
||||
keyConfig.setBefore(true);
|
||||
globalConfig.setKeyConfig(keyConfig);
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,8 @@
|
||||
package day.gitlab.dolphin.common.mybatis.util;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.mybatisflex.core.BaseMapper;
|
||||
import com.mybatisflex.core.paginate.Page;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import day.gitlab.dolphin.common.web.entity.PageRequest;
|
||||
import day.gitlab.dolphin.common.web.entity.PageResponse;
|
||||
|
||||
@ -13,24 +13,16 @@ public class Mappers {
|
||||
|
||||
public static <T, E> Page<E> page(PageRequest<T> pageRequest,
|
||||
BaseMapper<E> mapper,
|
||||
Function<T, LambdaQueryWrapper<E>> function) {
|
||||
Page<E> reqPage = toPage(pageRequest);
|
||||
LambdaQueryWrapper<E> wrapper = function.apply(pageRequest.getQuery());
|
||||
Function<T, QueryWrapper> function) {
|
||||
Page<E> reqPage = Page.of(pageRequest.getPageIndex(), pageRequest.getPageSize());
|
||||
QueryWrapper wrapper = function.apply(pageRequest.getQuery());
|
||||
|
||||
Page<E> resPage = mapper.selectPage(reqPage, wrapper);
|
||||
Long count = mapper.selectCount(wrapper);
|
||||
resPage.setTotal(count);
|
||||
|
||||
return resPage;
|
||||
}
|
||||
|
||||
public static <T, E> Page<E> toPage(PageRequest<T> pageRequest) {
|
||||
return new Page<>(pageRequest.getPageIndex(), pageRequest.getPageSize());
|
||||
return mapper.paginate(reqPage, wrapper);
|
||||
}
|
||||
|
||||
public static <T, E> PageResponse<E> mapPage(Page<T> page, Function<T, E> mapper) {
|
||||
List<E> records = page.getRecords().stream().map(mapper).toList();
|
||||
|
||||
return new PageResponse<>(page.getCurrent(), page.getSize(), page.getTotal(), records);
|
||||
return new PageResponse<>(page.getPageNumber(), page.getPageSize(), page.getTotalRow(), records);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,20 +1,17 @@
|
||||
package day.gitlab.dolphin.common.mybatis.util;
|
||||
|
||||
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
|
||||
import com.mybatisflex.core.keygen.IKeyGenerator;
|
||||
import day.gitlab.dolphin.common.core.util.UUIDv7;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
@Component
|
||||
public class UUIDv7Generator extends DefaultIdentifierGenerator {
|
||||
public class UUIDv7Generator implements IKeyGenerator {
|
||||
|
||||
public UUIDv7Generator() {
|
||||
super((InetAddress) null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String nextUUID(Object entity) {
|
||||
public Object generate(Object entity, String keyColumn) {
|
||||
return UUIDv7.randomUUID().toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user