feat(xbatis): 完成xbatis框架的适配
This commit is contained in:
93
spring-boot-service-template-xbatis/pom.xml
Normal file
93
spring-boot-service-template-xbatis/pom.xml
Normal file
@@ -0,0 +1,93 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.lanyuanxiaoyao</groupId>
|
||||
<artifactId>spring-boot-service-template</artifactId>
|
||||
<version>1.1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-boot-service-template-xbatis</artifactId>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.lanyuanxiaoyao</groupId>
|
||||
<artifactId>spring-boot-service-template-common</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.xbatis</groupId>
|
||||
<artifactId>xbatis-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.gavlyukovskiy</groupId>
|
||||
<artifactId>p6spy-spring-boot-starter</artifactId>
|
||||
<version>2.0.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mapstruct</groupId>
|
||||
<artifactId>mapstruct</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jspecify</groupId>
|
||||
<artifactId>jspecify</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>cn.xbatis</groupId>
|
||||
<artifactId>xbatis-spring-boot-parent</artifactId>
|
||||
<version>1.9.6-spring-boot4</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar-no-fork</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<annotationProcessorPaths>
|
||||
<path>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
</path>
|
||||
</annotationProcessorPaths>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.lanyuanxiaoyao.service.template.xbatis.configuration;
|
||||
|
||||
import cn.xbatis.core.incrementer.GeneratorFactory;
|
||||
import cn.xbatis.core.mybatis.mapper.BasicMapper;
|
||||
import com.lanyuanxiaoyao.service.template.xbatis.entity.SnowflakeIdGenerator;
|
||||
import com.lanyuanxiaoyao.service.template.xbatis.mapper.MybatisBasicMapper;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@MapperScan(basePackageClasses = MybatisBasicMapper.class, markerInterface = BasicMapper.class)
|
||||
public class MybatisConfiguration {
|
||||
static {
|
||||
GeneratorFactory.register("snowflake", new SnowflakeIdGenerator());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
package com.lanyuanxiaoyao.service.template.xbatis.controller;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.common.controller.SimpleController;
|
||||
import com.lanyuanxiaoyao.service.template.common.entity.GlobalResponse;
|
||||
import com.lanyuanxiaoyao.service.template.common.entity.Query;
|
||||
import com.lanyuanxiaoyao.service.template.common.helper.ObjectHelper;
|
||||
import com.lanyuanxiaoyao.service.template.xbatis.entity.SimpleEntity;
|
||||
import com.lanyuanxiaoyao.service.template.xbatis.service.SimpleServiceSupport;
|
||||
import java.util.function.Function;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
/**
|
||||
* 简单控制器支持类,提供基础的CRUD操作实现
|
||||
* <p>
|
||||
* 该类实现了基本的增删改查功能,通过泛型支持不同类型的数据转换。
|
||||
* 子类需要实现对应的Mapper函数来完成实体类与传输对象之间的转换。
|
||||
* </p>
|
||||
*
|
||||
* <h3>设计特点</h3>
|
||||
* <ul>
|
||||
* <li>泛型设计,支持任意实体类型和数据转换</li>
|
||||
* <li>统一的异常处理和事务管理</li>
|
||||
* <li>支持条件查询、分页查询和详情查询</li>
|
||||
* <li>提供抽象的Mapper方法,便于子类实现数据转换逻辑</li>
|
||||
* </ul>
|
||||
*
|
||||
* <h3>使用说明</h3>
|
||||
* <p>子类需要实现以下抽象方法:</p>
|
||||
* <ul>
|
||||
* <li>saveItemMapper(): 保存项到实体的转换函数</li>
|
||||
* <li>listItemMapper(): 实体到列表项的转换函数</li>
|
||||
* <li>detailItemMapper(): 实体到详情项的转换函数</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param <ENTITY> 实体类型,必须继承SimpleEntity
|
||||
* @param <SAVE_ITEM> 保存项类型
|
||||
* @param <LIST_ITEM> 列表项类型
|
||||
* @param <DETAIL_ITEM> 详情项类型
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class SimpleControllerSupport<ENTITY extends SimpleEntity, SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> implements SimpleController<SAVE_ITEM, LIST_ITEM, DETAIL_ITEM> {
|
||||
protected final SimpleServiceSupport<ENTITY> service;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param service 简单服务支持类实例
|
||||
*/
|
||||
public SimpleControllerSupport(SimpleServiceSupport<ENTITY> service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存实体对象
|
||||
* <p>
|
||||
* 将保存项转换为实体对象后保存,返回保存后的实体ID。
|
||||
* 支持新增和更新操作,通过事务保证数据一致性。
|
||||
* </p>
|
||||
*
|
||||
* @param item 需要保存的项
|
||||
* @return 返回保存后的实体ID响应对象,格式:{status: 0, message: "OK", data: 实体ID}
|
||||
* @throws Exception 保存过程中可能抛出的异常
|
||||
*/
|
||||
@Transactional(rollbackFor = Throwable.class)
|
||||
@PostMapping(SAVE)
|
||||
@Override
|
||||
public GlobalResponse<Long> save(@RequestBody SAVE_ITEM item) throws Exception {
|
||||
var mapper = saveItemMapper();
|
||||
return GlobalResponse.responseSuccess(service.save(mapper.apply(item)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有实体列表
|
||||
* <p>
|
||||
* 查询所有记录,不带任何过滤条件,返回分页格式的数据。
|
||||
* 将实体对象转换为列表项对象后返回。
|
||||
* </p>
|
||||
*
|
||||
* @return 返回实体列表响应对象,格式:{status: 0, message: "OK", data: {items: [...], total: total}}
|
||||
* @throws Exception 查询过程中可能抛出的异常
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
@GetMapping(LIST)
|
||||
@Override
|
||||
public GlobalResponse<GlobalResponse.ListItem<LIST_ITEM>> list() throws Exception {
|
||||
var mapper = listItemMapper();
|
||||
var result = service.list();
|
||||
return GlobalResponse.responseListData(
|
||||
result
|
||||
.stream()
|
||||
.map(entity -> {
|
||||
try {
|
||||
return mapper.apply(entity);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
})
|
||||
.toList(),
|
||||
result.size()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据查询条件获取实体列表
|
||||
* <p>
|
||||
* 支持复杂的查询条件、排序和分页,返回符合条件的数据。
|
||||
* 将实体对象转换为列表项对象后返回。
|
||||
* </p>
|
||||
*
|
||||
* @param query 查询条件对象,包含过滤条件、排序规则和分页信息
|
||||
* @return 返回符合条件的实体列表响应对象,格式:{status: 0, message: "OK", data: {items: [...], total: total}}
|
||||
* @throws Exception 查询过程中可能抛出的异常
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
@PostMapping(LIST)
|
||||
@Override
|
||||
public GlobalResponse<GlobalResponse.ListItem<LIST_ITEM>> list(@RequestBody Query query) throws Exception {
|
||||
if (ObjectHelper.isNull(query)) {
|
||||
return GlobalResponse.responseListData();
|
||||
}
|
||||
var mapper = listItemMapper();
|
||||
var result = service.list(query);
|
||||
return GlobalResponse.responseListData(
|
||||
result.items()
|
||||
.stream()
|
||||
.map(entity -> {
|
||||
try {
|
||||
return mapper.apply(entity);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
})
|
||||
.toList(),
|
||||
result.total()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取实体详情
|
||||
* <p>
|
||||
* 根据主键ID查询单条记录的详细信息,转换为详情项对象后返回。
|
||||
* 如果记录不存在则抛出异常。
|
||||
* </p>
|
||||
*
|
||||
* @param id 实体主键ID
|
||||
* @return 返回实体详情响应对象,格式:{status: 0, message: "OK", data: 详情数据}
|
||||
* @throws Exception 查询过程中可能抛出的异常
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
@GetMapping(DETAIL)
|
||||
@Override
|
||||
public GlobalResponse<DETAIL_ITEM> detail(@PathVariable("id") Long id) throws Exception {
|
||||
var mapper = detailItemMapper();
|
||||
return GlobalResponse.responseSuccess(mapper.apply(service.detailOrThrow(id)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID删除实体对象
|
||||
* <p>
|
||||
* 根据主键ID删除指定的记录,执行成功后返回成功响应。
|
||||
* 通过事务保证删除操作的一致性。
|
||||
* </p>
|
||||
*
|
||||
* @param id 需要删除的实体主键ID
|
||||
* @return 返回删除结果响应对象,格式:{status: 0, message: "OK", data: null}
|
||||
* @throws Exception 删除过程中可能抛出的异常
|
||||
*/
|
||||
@Transactional(rollbackFor = Throwable.class)
|
||||
@GetMapping(REMOVE)
|
||||
@Override
|
||||
public GlobalResponse<Object> remove(@PathVariable("id") Long id) throws Exception {
|
||||
service.remove(id);
|
||||
return GlobalResponse.responseSuccess();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存项映射器,将保存项转换为实体对象
|
||||
* <p>
|
||||
* 子类需要实现此方法,定义保存项到实体的转换逻辑。
|
||||
* </p>
|
||||
*
|
||||
* @return Function<SAVE_ITEM, ENTITY> 保存项到实体的转换函数
|
||||
*/
|
||||
protected abstract Function<SAVE_ITEM, ENTITY> saveItemMapper();
|
||||
|
||||
/**
|
||||
* 列表项映射器,将实体对象转换为列表项
|
||||
* <p>
|
||||
* 子类需要实现此方法,定义实体到列表项的转换逻辑。
|
||||
* </p>
|
||||
*
|
||||
* @return Function<ENTITY, LIST_ITEM> 实体到列表项的转换函数
|
||||
*/
|
||||
protected abstract Function<ENTITY, LIST_ITEM> listItemMapper();
|
||||
|
||||
/**
|
||||
* 详情项映射器,将实体对象转换为详情项
|
||||
* <p>
|
||||
* 子类需要实现此方法,定义实体到详情项的转换逻辑。
|
||||
* </p>
|
||||
*
|
||||
* @return Function<ENTITY, DETAIL_ITEM> 实体到详情项的转换函数
|
||||
*/
|
||||
protected abstract Function<ENTITY, DETAIL_ITEM> detailItemMapper();
|
||||
|
||||
public interface Mapper<S, T> {
|
||||
T map(S source) throws Exception;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.lanyuanxiaoyao.service.template.xbatis.entity;
|
||||
|
||||
import cn.xbatis.db.IdAutoType;
|
||||
import cn.xbatis.db.annotations.TableId;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
@FieldNameConstants
|
||||
public class IdOnlyEntity {
|
||||
@TableId(value = IdAutoType.GENERATOR, generator = "snowflake")
|
||||
private Long id;
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.lanyuanxiaoyao.service.template.xbatis.entity;
|
||||
|
||||
import cn.xbatis.db.annotations.TableField;
|
||||
import java.time.LocalDateTime;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString(callSuper = true)
|
||||
@FieldNameConstants
|
||||
public class SimpleEntity extends IdOnlyEntity {
|
||||
@TableField(defaultValue = "{NOW}", defaultValueFillAlways = true)
|
||||
private LocalDateTime createdTime;
|
||||
@TableField(defaultValue = "{NOW}", defaultValueFillAlways = true, updateDefaultValue = "{NOW}", updateDefaultValueFillAlways = true)
|
||||
private LocalDateTime modifiedTime;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.lanyuanxiaoyao.service.template.xbatis.entity;
|
||||
|
||||
import cn.xbatis.core.incrementer.Generator;
|
||||
import com.lanyuanxiaoyao.service.template.common.helper.SnowflakeHelper;
|
||||
|
||||
public class SnowflakeIdGenerator implements Generator<Long> {
|
||||
@Override
|
||||
public Long nextId(Class<?> entity) {
|
||||
return SnowflakeHelper.next();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.lanyuanxiaoyao.service.template.xbatis.mapper;
|
||||
|
||||
import cn.xbatis.core.mybatis.mapper.BasicMapper;
|
||||
|
||||
public interface MybatisBasicMapper extends BasicMapper {
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
package com.lanyuanxiaoyao.service.template.xbatis.service;
|
||||
|
||||
import cn.xbatis.core.mybatis.mapper.context.Pager;
|
||||
import cn.xbatis.core.sql.MybatisCmdFactory;
|
||||
import cn.xbatis.core.sql.executor.chain.QueryChain;
|
||||
import com.lanyuanxiaoyao.service.template.common.entity.Page;
|
||||
import com.lanyuanxiaoyao.service.template.common.entity.Query;
|
||||
import com.lanyuanxiaoyao.service.template.common.exception.IdNotFoundException;
|
||||
import com.lanyuanxiaoyao.service.template.common.helper.ObjectHelper;
|
||||
import com.lanyuanxiaoyao.service.template.common.service.QueryParser;
|
||||
import com.lanyuanxiaoyao.service.template.common.service.SimpleService;
|
||||
import com.lanyuanxiaoyao.service.template.xbatis.entity.SimpleEntity;
|
||||
import com.lanyuanxiaoyao.service.template.xbatis.mapper.MybatisBasicMapper;
|
||||
import db.sql.api.cmd.LikeMode;
|
||||
import db.sql.api.impl.cmd.basic.OrderByDirection;
|
||||
import db.sql.api.impl.cmd.struct.Where;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implements SimpleService<ENTITY> {
|
||||
private static final int DEFAULT_PAGE_INDEX = 1;
|
||||
private static final int DEFAULT_PAGE_SIZE = 10;
|
||||
|
||||
protected final MybatisBasicMapper mapper;
|
||||
private final Class<ENTITY> target;
|
||||
|
||||
public SimpleServiceSupport(Class<ENTITY> target, MybatisBasicMapper mapper) {
|
||||
this.target = target;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long save(ENTITY entity) {
|
||||
return (long) mapper.save(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Iterable<ENTITY> entities) {
|
||||
mapper.save(entities);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long count() {
|
||||
return (long) mapper.countAll(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ENTITY> list() {
|
||||
return mapper.listAll(target);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ENTITY> list(Set<Long> ids) {
|
||||
return mapper.listByIds(target, ids);
|
||||
}
|
||||
|
||||
protected void commonPredicates(Where where) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<ENTITY> list(Query query) {
|
||||
var chain = QueryChain.of(mapper, target);
|
||||
var factory = chain.$();
|
||||
|
||||
var paging = Pager.<ENTITY>of(DEFAULT_PAGE_INDEX, DEFAULT_PAGE_SIZE);
|
||||
if (ObjectHelper.isNotNull(query.page())) {
|
||||
var index = Math.max(ObjectHelper.defaultIfNull(query.page().index(), DEFAULT_PAGE_INDEX), 1);
|
||||
var size = Math.max(ObjectHelper.defaultIfNull(query.page().size(), DEFAULT_PAGE_SIZE), 1);
|
||||
paging = Pager.of(index, size);
|
||||
}
|
||||
chain.paging(paging);
|
||||
|
||||
if (ObjectHelper.isNotEmpty(query.sort())) {
|
||||
query.sort().forEach(sort -> chain.orderBy(OrderByDirection.valueOf(sort.direction().name()), sort.column()));
|
||||
}
|
||||
|
||||
var where = chain.where();
|
||||
commonPredicates(where);
|
||||
new XBatisQueryParser<>(target, factory, where).build(query.query());
|
||||
|
||||
return new Page<>(chain.list(), chain.count());
|
||||
}
|
||||
|
||||
private Optional<ENTITY> detailOptional(Long id) {
|
||||
if (ObjectHelper.isNull(id)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.ofNullable(mapper.getById(target, id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public ENTITY detail(Long id) {
|
||||
return detailOptional(id).orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ENTITY detailOrThrow(Long id) {
|
||||
return detailOptional(id).orElseThrow(() -> new IdNotFoundException(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Long id) {
|
||||
mapper.deleteById(target, id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(Set<Long> ids) {
|
||||
mapper.deleteByIds(target, ids);
|
||||
}
|
||||
|
||||
private static final class XBatisQueryParser<ENTITY> extends QueryParser<Void> {
|
||||
private final Class<ENTITY> target;
|
||||
private final MybatisCmdFactory factory;
|
||||
private final Where where;
|
||||
|
||||
private XBatisQueryParser(Class<ENTITY> target, MybatisCmdFactory factory, Where where) {
|
||||
this.target = target;
|
||||
this.factory = factory;
|
||||
this.where = where;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void nullEqual(Query.Queryable queryable) {
|
||||
queryable.nullEqual().forEach(column -> where.isNull(factory.field(target, column)));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notNullEqual(Query.Queryable queryable) {
|
||||
queryable.notNullEqual().forEach(column -> where.isNotNull(factory.field(target, column)));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void empty(Query.Queryable queryable) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notEmpty(Query.Queryable queryable) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void equal(Query.Queryable queryable) {
|
||||
queryable.equal().forEach((column, value) -> where.eq(factory.field(target, column), value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notEqual(Query.Queryable queryable) {
|
||||
queryable.notEqual().forEach((column, value) -> where.ne(factory.field(target, column), value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void like(Query.Queryable queryable) {
|
||||
queryable.like().forEach((column, value) -> where.like(LikeMode.NONE, factory.field(target, column), value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notLike(Query.Queryable queryable) {
|
||||
queryable.notLike().forEach((column, value) -> where.notLike(LikeMode.NONE, factory.field(target, column), value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void contain(Query.Queryable queryable) {
|
||||
queryable.contain().forEach((column, value) -> where.like(factory.field(target, column), value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notContain(Query.Queryable queryable) {
|
||||
queryable.notContain().forEach((column, value) -> where.notLike(factory.field(target, column), value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void startWith(Query.Queryable queryable) {
|
||||
queryable.startWith().forEach((column, value) -> where.like(LikeMode.LEFT, factory.field(target, column), value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notStartWith(Query.Queryable queryable) {
|
||||
queryable.notStartWith().forEach((column, value) -> where.notLike(LikeMode.LEFT, factory.field(target, column), value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void endWith(Query.Queryable queryable) {
|
||||
queryable.endWith().forEach((column, value) -> where.like(LikeMode.RIGHT, factory.field(target, column), value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notEndWith(Query.Queryable queryable) {
|
||||
queryable.notEndWith().forEach((column, value) -> where.notLike(LikeMode.RIGHT, factory.field(target, column), value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void great(Query.Queryable queryable) {
|
||||
queryable.great().forEach((column, value) -> where.gt(factory.field(target, column), value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void less(Query.Queryable queryable) {
|
||||
queryable.less().forEach((column, value) -> where.lt(factory.field(target, column), value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void greatEqual(Query.Queryable queryable) {
|
||||
queryable.greatEqual().forEach((column, value) -> where.gte(factory.field(target, column), value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void lessEqual(Query.Queryable queryable) {
|
||||
queryable.lessEqual().forEach((column, value) -> where.lte(factory.field(target, column), value));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void inside(Query.Queryable queryable) {
|
||||
queryable.inside()
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> ObjectHelper.isNotEmpty(entry.getValue()))
|
||||
.forEach(entry -> where.in(factory.field(target, entry.getKey()), entry.getValue()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notInside(Query.Queryable queryable) {
|
||||
queryable.notInside()
|
||||
.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> ObjectHelper.isNotEmpty(entry.getValue()))
|
||||
.forEach(entry -> where.notIn(factory.field(target, entry.getKey()), entry.getValue()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void between(Query.Queryable queryable) {
|
||||
queryable.between().forEach((column, value) -> where.between(factory.field(target, column), value.start(), value.end()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void notBetween(Query.Queryable queryable) {
|
||||
queryable.notBetween().forEach((column, value) -> where.notBetween(factory.field(target, column), value.start(), value.end()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void build() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
17
spring-boot-service-template-xbatis/src/test/initial.sql
Normal file
17
spring-boot-service-template-xbatis/src/test/initial.sql
Normal file
@@ -0,0 +1,17 @@
|
||||
create table if not exists Company
|
||||
(
|
||||
id bigint primary key,
|
||||
name varchar(255) not null,
|
||||
members int not null,
|
||||
created_time timestamp not null,
|
||||
modified_time timestamp not null
|
||||
);
|
||||
|
||||
create table if not exists Employee
|
||||
(
|
||||
id bigint primary key,
|
||||
name varchar(255) not null,
|
||||
age int not null,
|
||||
created_time timestamp not null,
|
||||
modified_time timestamp not null
|
||||
);
|
||||
@@ -0,0 +1,173 @@
|
||||
package com.lanyuanxiaoyao.service.template.xbatis;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.mybatis.spring.annotation.MapperScan;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import tools.jackson.databind.JsonNode;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
|
||||
@Slf4j
|
||||
@MapperScan("com.lanyuanxiaoyao.service.template.xbatis.mapper")
|
||||
@SpringBootApplication
|
||||
public class TestApplication {
|
||||
private static final String BASE_URL = "http://localhost:2490";
|
||||
private static final RestTemplate REST_CLIENT = new RestTemplate();
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestApplication.class, args);
|
||||
}
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void runTests() {
|
||||
// 增
|
||||
var cid1 = saveItem("company", "{\"name\": \"Apple\",\"members\": 10}").get("data").asLong();
|
||||
var cid2 = saveItem("company", "{\"name\": \"Banana\",\"members\": 20}").get("data").asLong();
|
||||
var cid3 = saveItem("company", "{\"name\": \"Cheery\",\"members\": 20}").get("data").asLong();
|
||||
|
||||
// 查
|
||||
var companies = listItems("company");
|
||||
Assert.isTrue(companies.at("/data/items").size() == 3, "数量错误");
|
||||
Assert.isTrue(companies.at("/data/total").asLong() == 3, "返回数量错误");
|
||||
|
||||
// language=JSON
|
||||
var companies2 = listItems("company", "{\n" +
|
||||
" \"page\": {\n" +
|
||||
" \"index\": 1,\n" +
|
||||
" \"size\": 2\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
Assert.isTrue(companies2.at("/data/items").size() == 2, "数量错误");
|
||||
Assert.isTrue(companies2.at("/data/total").asLong() == 3, "返回数量错误");
|
||||
// language=JSON
|
||||
var companies3 = listItems("company", "{\n" +
|
||||
" \"query\": {\n" +
|
||||
" \"notNullEqual\": [\n" +
|
||||
" \"name\"\n" +
|
||||
" ],\n" +
|
||||
" \"equal\": {\n" +
|
||||
" \"name\": \"Apple\"\n" +
|
||||
" },\n" +
|
||||
" \"like\": {\n" +
|
||||
" \"name\": \"Appl%\"\n" +
|
||||
" },\n" +
|
||||
" \"contain\": {\n" +
|
||||
" \"name\": \"ple\"\n" +
|
||||
" },\n" +
|
||||
" \"startWith\": {\n" +
|
||||
" \"name\": \"Appl\"\n" +
|
||||
" },\n" +
|
||||
" \"endWith\": {\n" +
|
||||
" \"name\": \"le\"\n" +
|
||||
" },\n" +
|
||||
" \"less\": {\n" +
|
||||
" \"members\": 50\n" +
|
||||
" },\n" +
|
||||
" \"greatEqual\": {\n" +
|
||||
" \"members\": 0,\n" +
|
||||
" \"createdTime\": \"2025-01-01 00:00:00\"\n" +
|
||||
" },\n" +
|
||||
" \"inside\": {\n" +
|
||||
" \"name\": [\n" +
|
||||
" \"Apple\",\n" +
|
||||
" \"Banana\"\n" +
|
||||
" ]\n" +
|
||||
" },\n" +
|
||||
" \"between\": {\n" +
|
||||
" \"members\": {\n" +
|
||||
" \"start\": 0,\n" +
|
||||
" \"end\": 50\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" \"page\": {\n" +
|
||||
" \"index\": 1,\n" +
|
||||
" \"size\": 2\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
Assert.isTrue(companies3.at("/data/items").size() == 1, "数量错误");
|
||||
Assert.isTrue(companies3.at("/data/total").asLong() == 1, "返回数量错误");
|
||||
|
||||
var company1 = detailItem("company", cid1);
|
||||
Assert.isTrue(cid1 == company1.at("/data/id").asLong(), "id错误");
|
||||
Assert.isTrue("Apple".equals(company1.at("/data/name").asText()), "name错误");
|
||||
|
||||
// 改
|
||||
var cid4 = saveItem("company", "{\"id\": %d, \"name\": \"Dog\"}".formatted(cid2)).get("data").asLong();
|
||||
Assert.isTrue(cid2 == cid4, "id错误");
|
||||
var company2 = detailItem("company", cid2);
|
||||
Assert.isTrue("Dog".equals(company2.at("/data/name").asText()), "name错误");
|
||||
|
||||
// 删
|
||||
removeItem("company", cid3);
|
||||
Assert.isTrue(listItems("company").at("/data/items").size() == 2, "数量错误");
|
||||
Assert.isTrue(listItems("company").at("/data/total").asLong() == 2, "返回数量错误");
|
||||
|
||||
log.info(listItems("company").toPrettyString());
|
||||
System.exit(0);
|
||||
}
|
||||
|
||||
private HttpHeaders headers() {
|
||||
var headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
return headers;
|
||||
}
|
||||
|
||||
private JsonNode saveItem(String path, String body) {
|
||||
var response = REST_CLIENT.postForEntity(
|
||||
"%s/%s/save".formatted(BASE_URL, path),
|
||||
new HttpEntity<>(body, headers()),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
|
||||
private JsonNode listItems(String path) {
|
||||
var response = REST_CLIENT.getForEntity(
|
||||
"%s/%s/list".formatted(BASE_URL, path),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
|
||||
private JsonNode listItems(String path, String query) {
|
||||
var response = REST_CLIENT.postForEntity(
|
||||
"%s/%s/list".formatted(BASE_URL, path),
|
||||
new HttpEntity<>(query, headers()),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
|
||||
private JsonNode detailItem(String path, Long id) {
|
||||
var response = REST_CLIENT.getForEntity(
|
||||
"%s/%s/detail/%d".formatted(BASE_URL, path, id),
|
||||
String.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
Assert.notNull(response.getBody(), "请求失败");
|
||||
return MAPPER.readTree(response.getBody());
|
||||
}
|
||||
|
||||
private void removeItem(String path, Long id) {
|
||||
var response = REST_CLIENT.getForEntity(
|
||||
"%s/%s/remove/%d".formatted(BASE_URL, path, id),
|
||||
Void.class
|
||||
);
|
||||
Assert.isTrue(response.getStatusCode().is2xxSuccessful(), "请求失败");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.lanyuanxiaoyao.service.template.xbatis.controller;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.xbatis.entity.Company;
|
||||
import com.lanyuanxiaoyao.service.template.xbatis.service.CompanyService;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.function.Function;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("company")
|
||||
public class CompanyController extends SimpleControllerSupport<Company, CompanyController.SaveItem, CompanyController.ListItem, CompanyController.DetailItem> {
|
||||
public CompanyController(CompanyService service) {
|
||||
super(service);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<SaveItem, Company> saveItemMapper() {
|
||||
return item -> {
|
||||
var company = new Company();
|
||||
company.setId(item.id());
|
||||
company.setName(item.name());
|
||||
company.setMembers(item.members());
|
||||
return company;
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Company, ListItem> listItemMapper() {
|
||||
return company -> new ListItem(
|
||||
company.getId(),
|
||||
company.getName(),
|
||||
company.getMembers()
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Function<Company, DetailItem> detailItemMapper() {
|
||||
return company -> new DetailItem(
|
||||
company.getId(),
|
||||
company.getName(),
|
||||
company.getMembers(),
|
||||
company.getCreatedTime(),
|
||||
company.getModifiedTime()
|
||||
);
|
||||
}
|
||||
|
||||
public record SaveItem(
|
||||
Long id,
|
||||
String name,
|
||||
Integer members
|
||||
) {
|
||||
}
|
||||
|
||||
public record ListItem(
|
||||
Long id,
|
||||
String name,
|
||||
Integer members
|
||||
) {
|
||||
}
|
||||
|
||||
public record DetailItem(
|
||||
Long id,
|
||||
String name,
|
||||
Integer members,
|
||||
LocalDateTime createdTime,
|
||||
LocalDateTime modifiedTime
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.lanyuanxiaoyao.service.template.xbatis.entity;
|
||||
|
||||
import cn.xbatis.db.annotations.Table;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString(callSuper = true)
|
||||
@FieldNameConstants
|
||||
@Table
|
||||
public class Company extends SimpleEntity {
|
||||
private String name;
|
||||
private Integer members;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.lanyuanxiaoyao.service.template.xbatis.entity;
|
||||
|
||||
import cn.xbatis.db.annotations.Table;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
import lombok.experimental.FieldNameConstants;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString(callSuper = true)
|
||||
@FieldNameConstants
|
||||
@Table
|
||||
public class Employee extends SimpleEntity {
|
||||
private String name;
|
||||
private Integer age;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.lanyuanxiaoyao.service.template.xbatis.service;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.xbatis.entity.Company;
|
||||
import com.lanyuanxiaoyao.service.template.xbatis.mapper.MybatisBasicMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class CompanyService extends SimpleServiceSupport<Company> {
|
||||
public CompanyService(MybatisBasicMapper mapper) {
|
||||
super(Company.class, mapper);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.lanyuanxiaoyao.service.template.xbatis.service;
|
||||
|
||||
import com.lanyuanxiaoyao.service.template.xbatis.entity.Employee;
|
||||
import com.lanyuanxiaoyao.service.template.xbatis.mapper.MybatisBasicMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class EmployeeService extends SimpleServiceSupport<Employee> {
|
||||
public EmployeeService(MybatisBasicMapper mapper) {
|
||||
super(Employee.class, mapper);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
server:
|
||||
port: 2490
|
||||
spring:
|
||||
application:
|
||||
name: Test
|
||||
datasource:
|
||||
url: "jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;MODE=MySQL;DATABASE_TO_LOWER=TRUE;INIT=runscript from '/Users/lanyuanxiaoyao/Project/IdeaProjects/spring-boot-service-template/spring-boot-service-template-xbatis/src/test/initial.sql'"
|
||||
username: test
|
||||
password: test
|
||||
driver-class-name: org.h2.Driver
|
||||
mybatis:
|
||||
configuration:
|
||||
banner: false
|
||||
decorator:
|
||||
datasource:
|
||||
p6spy:
|
||||
multiline: false
|
||||
exclude-categories:
|
||||
- commit
|
||||
- result
|
||||
- resultset
|
||||
log-format: "%(category)|%(executionTime)|%(sqlSingleLine)"
|
||||
Reference in New Issue
Block a user