diff --git a/orion-ops-dependencies/pom.xml b/orion-ops-dependencies/pom.xml index 8e7baf9c..b63bc82a 100644 --- a/orion-ops-dependencies/pom.xml +++ b/orion-ops-dependencies/pom.xml @@ -75,6 +75,11 @@ orion-ops-spring-boot-starter-datasource ${revision} + + com.orion.ops + orion-ops-spring-boot-starter-mybatis + ${revision} + diff --git a/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/Const.java b/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/Const.java new file mode 100644 index 00000000..ce10abc0 --- /dev/null +++ b/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/Const.java @@ -0,0 +1,15 @@ +package com.orion.ops.framework.common.constant; + +/** + * 常量 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/23 18:49 + */ +public class Const extends com.orion.lang.constant.Const { + + private Const() { + } + +} diff --git a/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/enums/ExceptionMessageConst.java b/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/ExceptionMessageConst.java similarity index 95% rename from orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/enums/ExceptionMessageConst.java rename to orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/ExceptionMessageConst.java index d64d4943..88e5685e 100644 --- a/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/enums/ExceptionMessageConst.java +++ b/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/ExceptionMessageConst.java @@ -1,4 +1,4 @@ -package com.orion.ops.framework.common.enums; +package com.orion.ops.framework.common.constant; /** * 消息常量 diff --git a/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/enums/FilterOrderConst.java b/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/FilterOrderConst.java similarity index 82% rename from orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/enums/FilterOrderConst.java rename to orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/FilterOrderConst.java index e60b1578..6ef542e5 100644 --- a/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/enums/FilterOrderConst.java +++ b/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/FilterOrderConst.java @@ -1,4 +1,4 @@ -package com.orion.ops.framework.common.enums; +package com.orion.ops.framework.common.constant; /** * 过滤器排序常量 diff --git a/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/enums/InterceptorOrderConst.java b/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/InterceptorOrderConst.java similarity index 79% rename from orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/enums/InterceptorOrderConst.java rename to orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/InterceptorOrderConst.java index c5a1640b..87f769da 100644 --- a/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/enums/InterceptorOrderConst.java +++ b/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/InterceptorOrderConst.java @@ -1,4 +1,4 @@ -package com.orion.ops.framework.common.enums; +package com.orion.ops.framework.common.constant; /** * 拦截器排序常量 diff --git a/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/enums/OrionOpsProConst.java b/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/OrionOpsProConst.java similarity index 88% rename from orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/enums/OrionOpsProConst.java rename to orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/OrionOpsProConst.java index c426330d..8215a668 100644 --- a/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/enums/OrionOpsProConst.java +++ b/orion-ops-framework/orion-ops-common/src/main/java/com/orion/ops/framework/common/constant/OrionOpsProConst.java @@ -1,4 +1,4 @@ -package com.orion.ops.framework.common.enums; +package com.orion.ops.framework.common.constant; /** * 项目常量 diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/pom.xml b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/pom.xml new file mode 100644 index 00000000..e07cc1f3 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/pom.xml @@ -0,0 +1,47 @@ + + + + com.orion.ops + orion-ops-framework + ${revision} + + + 4.0.0 + orion-ops-spring-boot-starter-mybatis + ${project.artifactId} + jar + + 项目 mybatis 配置包 + https://github.com/lijiahangmax/orion-ops-pro + + + + com.orion.ops + orion-ops-common + + + + org.springframework.boot + spring-boot-starter + + + + + com.orion.ops + orion-ops-spring-boot-starter-datasource + + + + + com.baomidou + mybatis-plus-boot-starter + + + com.baomidou + mybatis-plus-generator + + + + \ No newline at end of file diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/config/OrionMybatisAutoConfiguration.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/config/OrionMybatisAutoConfiguration.java new file mode 100644 index 00000000..18645e96 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/config/OrionMybatisAutoConfiguration.java @@ -0,0 +1,29 @@ +package com.orion.ops.framework.mybatis.config; + +import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; +import com.orion.ops.framework.mybatis.handler.FieldFillHandler; +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.context.annotation.Bean; + +/** + * mybatis 配置类 + * TODO 扫描的包 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/23 18:35 + */ +@AutoConfiguration +@MapperScan(value = "com.orion.ops.module.*.dao", lazyInitialization = "true") +public class OrionMybatisAutoConfiguration { + + /** + * @return 字段填充元数据处理器 + */ + @Bean + public MetaObjectHandler defaultMetaObjectHandler() { + return new FieldFillHandler(); + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/domain/BaseDO.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/domain/BaseDO.java new file mode 100644 index 00000000..9072f91d --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/domain/BaseDO.java @@ -0,0 +1,55 @@ +package com.orion.ops.framework.mybatis.domain; + +import com.baomidou.mybatisplus.annotation.FieldFill; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableLogic; +import lombok.Data; +import org.apache.ibatis.type.JdbcType; + +import java.io.Serializable; +import java.util.Date; + +/** + * 公共实体对象 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/23 18:42 + */ +@Data +public class BaseDO implements Serializable { + + /** + * 创建时间 + */ + @TableField(fill = FieldFill.INSERT) + private Date createTime; + + /** + * 更新时间 + */ + @TableField(fill = FieldFill.INSERT_UPDATE) + private Date updateTime; + + /** + * 创建人 + */ + @TableField(fill = FieldFill.INSERT, jdbcType = JdbcType.VARCHAR) + private String creator; + + /** + * 更新人 + */ + @TableField(fill = FieldFill.INSERT_UPDATE, jdbcType = JdbcType.VARCHAR) + private String updater; + + /** + * 是否删除 1未删除 2已删除 + * + * @see com.orion.lang.constant.Const#NOT_DELETED + * @see com.orion.lang.constant.Const#IS_DELETED + */ + @TableLogic + private Boolean deleted; + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/handler/FieldFillHandler.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/handler/FieldFillHandler.java new file mode 100644 index 00000000..1d9102dc --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/handler/FieldFillHandler.java @@ -0,0 +1,64 @@ +package com.orion.ops.framework.mybatis.handler; + +import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; +import com.orion.ops.framework.mybatis.domain.BaseDO; +import org.apache.ibatis.reflection.MetaObject; + +import java.util.Date; +import java.util.Objects; + +/** + * 字段填充处理器 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/23 19:01 + */ +public class FieldFillHandler implements MetaObjectHandler { + + @Override + public void insertFill(MetaObject metaObject) { + if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BaseDO) { + BaseDO baseDO = (BaseDO) metaObject.getOriginalObject(); + + Date now = new Date(); + // 创建时间 + if (Objects.isNull(baseDO.getCreateTime())) { + baseDO.setCreateTime(now); + } + // 更新时间 + if (Objects.isNull(baseDO.getUpdateTime())) { + baseDO.setUpdateTime(now); + } + + // TODO 当前用户 + Long userId = null; + // 创建人 + if (Objects.nonNull(userId) && Objects.isNull(baseDO.getCreator())) { + baseDO.setCreator(userId.toString()); + } + // 更新人 + if (Objects.nonNull(userId) && Objects.isNull(baseDO.getUpdater())) { + baseDO.setUpdater(userId.toString()); + } + } + } + + @Override + public void updateFill(MetaObject metaObject) { + // 更新时间 + Object modifyTime = getFieldValByName("updateTime", metaObject); + if (Objects.isNull(modifyTime)) { + setFieldValByName("updateTime", new Date(), metaObject); + } + + // 更新人 + Object updater = getFieldValByName("updater", metaObject); + // TODO 当前用户 + Long userId = null; + if (Objects.nonNull(userId) && Objects.isNull(updater)) { + setFieldValByName("updater", userId.toString(), metaObject); + } + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/mapper/IMapper.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/mapper/IMapper.java new file mode 100644 index 00000000..b3e5ecab --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/mapper/IMapper.java @@ -0,0 +1,100 @@ +package com.orion.ops.framework.mybatis.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.baomidou.mybatisplus.extension.toolkit.Db; +import com.orion.ops.framework.mybatis.query.DataQuery; + +import java.util.Collection; + +/** + * 公共 mapper + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/23 18:42 + */ +public interface IMapper extends BaseMapper { + + /** + * 批量插入 + * + * @param entities entities + * @return 是否成功 + */ + default boolean insertBatch(Collection entities) { + return Db.saveBatch(entities); + } + + /** + * 批量插入 + * + * @param entities entities + * @param size size + * @return 是否成功 + */ + default boolean insertBatch(Collection entities, int size) { + return Db.saveBatch(entities, size); + } + + /** + * 批量更新 (通过id) + * + * @param entities entities + * @return 是否成功 + */ + default boolean updateBatch(Collection entities) { + return Db.updateBatchById(entities); + } + + /** + * 批量更新 (通过id) + * + * @param entities entities + * @param size size + * @return 是否成功 + */ + default boolean updateBatch(Collection entities, int size) { + return Db.updateBatchById(entities, size); + } + + /** + * 插入或更新 (通过id) + * + * @param entity entity + * @return 是否成功 + */ + default boolean insertOrUpdate(T entity) { + return Db.saveOrUpdate(entity); + } + + /** + * 批量插入或更新 (通过id) + * + * @param entities entities + * @return 是否成功 + */ + default boolean insertOrUpdateBatch(Collection entities) { + return Db.saveOrUpdateBatch(entities); + } + + /** + * 批量插入或更新 (通过id) + * + * @param entities entities + * @param size size + * @return 是否成功 + */ + default boolean insertOrUpdateBatch(Collection entities, int size) { + return Db.saveOrUpdateBatch(entities, size); + } + + /** + * 获取 DataQuery 对象 + * + * @return DataQuery + */ + default DataQuery of() { + return DataQuery.of(this); + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/query/Conditions.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/query/Conditions.java new file mode 100644 index 00000000..b3a287c6 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/query/Conditions.java @@ -0,0 +1,27 @@ +package com.orion.ops.framework.mybatis.query; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; + +/** + * 条件构建类 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/25 12:24 + */ +public class Conditions { + + private Conditions() { + } + + /** + * 条件有效性验证 wrapper + * + * @param T + * @return wrapper + */ + public static LambdaQueryWrapper validateWrapper() { + return new ValidateLambdaWrapper<>(); + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/query/DataQuery.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/query/DataQuery.java new file mode 100644 index 00000000..556cfe0b --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/query/DataQuery.java @@ -0,0 +1,108 @@ +package com.orion.ops.framework.mybatis.query; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.orion.lang.define.wrapper.DataGrid; +import com.orion.lang.define.wrapper.PageRequest; +import com.orion.lang.define.wrapper.Pager; +import com.orion.lang.utils.Valid; +import com.orion.lang.utils.collect.Lists; +import com.orion.lang.utils.convert.Converts; + +import java.util.List; +import java.util.Optional; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * 数据查询器 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/23 18:52 + */ +public class DataQuery { + + private final BaseMapper dao; + + private PageRequest page; + + private LambdaQueryWrapper wrapper; + + private DataQuery(BaseMapper dao) { + this.dao = dao; + } + + public static DataQuery of(BaseMapper dao) { + Valid.notNull(dao, "dao is null"); + return new DataQuery<>(dao); + } + + public DataQuery page(PageRequest page) { + this.page = Valid.notNull(page, "page is null"); + return this; + } + + public DataQuery wrapper(LambdaQueryWrapper wrapper) { + this.wrapper = Valid.notNull(wrapper, "wrapper is null"); + return this; + } + + public Optional get() { + return Optional.ofNullable(dao.selectOne(wrapper)); + } + + // TODO mapstruct + public Optional get(Class c) { + return Optional.ofNullable(dao.selectOne(wrapper)) + .map(s -> Converts.to(s, c)); + } + + public Stream list() { + return dao.selectList(wrapper).stream(); + } + + // TODO mapstruct + public List list(Class c) { + return Converts.toList(dao.selectList(wrapper), c); + } + + public Long count() { + return dao.selectCount(wrapper); + } + + public boolean present() { + return dao.selectCount(wrapper) > 0; + } + + public DataGrid dataGrid() { + return this.dataGrid(Function.identity()); + } + + // TODO mapstruct + public DataGrid dataGrid(Class c) { + return this.dataGrid(t -> Converts.to(t, c)); + } + + public DataGrid dataGrid(Function convert) { + Valid.notNull(convert, "convert is null"); + Valid.notNull(page, "page is null"); + Valid.notNull(wrapper, "wrapper is null"); + Long count = dao.selectCount(wrapper); + Pager pager = new Pager<>(page); + pager.setTotal(count.intValue()); + boolean next = pager.hasMoreData(); + if (next) { + wrapper.last(pager.getSql()); + List rows = dao.selectList(wrapper).stream() + .map(convert) + .collect(Collectors.toList()); + pager.setRows(rows); + } else { + pager.setRows(Lists.empty()); + } + return DataGrid.of(pager); + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/query/ValidateLambdaWrapper.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/query/ValidateLambdaWrapper.java new file mode 100644 index 00000000..b6080558 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/query/ValidateLambdaWrapper.java @@ -0,0 +1,192 @@ +package com.orion.ops.framework.mybatis.query; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.toolkit.support.SFunction; +import com.orion.lang.utils.Strings; + +import java.util.Collection; + +/** + * TODO TEST + *

+ * 有效性验证 wrapper + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/25 11:43 + */ +public class ValidateLambdaWrapper extends LambdaQueryWrapper { + + /** + * 有效性验证 + * + * @param objects objects + * @return isValidate + */ + private boolean isIllegal(Object... objects) { + for (Object object : objects) { + // 非 null 检测 + if (object == null) { + return false; + } + // 字符串 非空校验 + if (object instanceof String) { + if (Strings.isBlank((String) object)) { + return false; + } + } + // 集合 非空校验 + if (object instanceof Collection) { + if (((Collection) object).isEmpty()) { + return false; + } + } + } + return true; + } + + @Override + public LambdaQueryWrapper eq(SFunction column, Object val) { + if (this.isIllegal(val)) { + return this; + } + return super.eq(column, val); + } + + @Override + public LambdaQueryWrapper ne(SFunction column, Object val) { + if (this.isIllegal(val)) { + return this; + } + return super.ne(column, val); + } + + @Override + public LambdaQueryWrapper gt(SFunction column, Object val) { + if (this.isIllegal(val)) { + return this; + } + return super.gt(column, val); + } + + @Override + public LambdaQueryWrapper ge(SFunction column, Object val) { + if (this.isIllegal(val)) { + return this; + } + return super.ge(column, val); + } + + @Override + public LambdaQueryWrapper lt(SFunction column, Object val) { + if (this.isIllegal(val)) { + return this; + } + return super.lt(column, val); + } + + @Override + public LambdaQueryWrapper le(SFunction column, Object val) { + if (this.isIllegal(val)) { + return this; + } + return super.le(column, val); + } + + @Override + public LambdaQueryWrapper between(SFunction column, Object val1, Object val2) { + if (this.isIllegal(val1, val2)) { + return this; + } + return super.between(column, val1, val2); + } + + @Override + public LambdaQueryWrapper notBetween(SFunction column, Object val1, Object val2) { + if (this.isIllegal(val1, val2)) { + return this; + } + return super.notBetween(column, val1, val2); + } + + @Override + public LambdaQueryWrapper like(SFunction column, Object val) { + if (this.isIllegal(val)) { + return this; + } + return super.like(column, val); + } + + @Override + public LambdaQueryWrapper notLike(SFunction column, Object val) { + if (this.isIllegal(val)) { + return this; + } + return super.notLike(column, val); + } + + @Override + public LambdaQueryWrapper notLikeLeft(SFunction column, Object val) { + if (this.isIllegal(val)) { + return this; + } + return super.notLikeLeft(column, val); + } + + @Override + public LambdaQueryWrapper notLikeRight(SFunction column, Object val) { + if (this.isIllegal(val)) { + return this; + } + return super.notLikeRight(column, val); + } + + @Override + public LambdaQueryWrapper likeLeft(SFunction column, Object val) { + if (this.isIllegal(val)) { + return this; + } + return super.likeLeft(column, val); + } + + @Override + public LambdaQueryWrapper likeRight(SFunction column, Object val) { + if (this.isIllegal(val)) { + return this; + } + return super.likeRight(column, val); + } + + @Override + public LambdaQueryWrapper in(SFunction column, Collection coll) { + if (this.isIllegal(coll)) { + return this; + } + return super.in(column, coll); + } + + @Override + public LambdaQueryWrapper in(SFunction column, Object... values) { + if (this.isIllegal(values)) { + return this; + } + return super.in(column, values); + } + + @Override + public LambdaQueryWrapper notIn(SFunction column, Collection coll) { + if (this.isIllegal(coll)) { + return this; + } + return super.notIn(column, coll); + } + + @Override + public LambdaQueryWrapper notIn(SFunction column, Object... value) { + if (this.isIllegal(value)) { + return this; + } + return super.notIn(column, value); + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/ITypeHandler.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/ITypeHandler.java new file mode 100644 index 00000000..ab888ca3 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/ITypeHandler.java @@ -0,0 +1,48 @@ +package com.orion.ops.framework.mybatis.type; + +import org.apache.ibatis.type.TypeHandler; + +import java.util.List; + +/** + * mybatis 类型转换 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/25 10:38 + */ +public interface ITypeHandler extends TypeHandler { + + // TODO KIT + String COMMA = ","; + + /** + * 数据类型转换 + * + * @param param param + * @return result + */ + R getResult(P param); + + /** + * // TODO kit + * 用 , 连接 + * + * @param list list + * @return res + */ + default String join(List list) { + if (list == null) { + return null; + } + StringBuilder sb = new StringBuilder(); + for (int i = 0, size = list.size(); i < size; i++) { + sb.append(list.get(i)); + if (i != size - 1) { + sb.append(COMMA); + } + } + return sb.toString(); + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/IntegerListTypeHandler.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/IntegerListTypeHandler.java new file mode 100644 index 00000000..6c98e5d0 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/IntegerListTypeHandler.java @@ -0,0 +1,60 @@ +package com.orion.ops.framework.mybatis.type; + +import com.orion.lang.utils.Strings; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * TODO TEST + * varchar -> List + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/25 10:33 + */ +@MappedJdbcTypes(JdbcType.VARCHAR) +@MappedTypes(List.class) +public class IntegerListTypeHandler implements ITypeHandler> { + + @Override + public void setParameter(PreparedStatement ps, int i, List res, JdbcType jdbcType) throws SQLException { + // 设置占位符 + ps.setString(i, this.join(res)); + } + + @Override + public List getResult(ResultSet rs, String columnName) throws SQLException { + return this.getResult(rs.getString(columnName)); + } + + @Override + public List getResult(ResultSet rs, int columnIndex) throws SQLException { + return this.getResult(rs.getString(columnIndex)); + } + + @Override + public List getResult(CallableStatement cs, int columnIndex) throws SQLException { + return this.getResult(cs.getString(columnIndex)); + } + + @Override + public List getResult(String value) { + if (value == null) { + return null; + } + return Arrays.stream(value.split(COMMA)) + .filter(Strings::isNumber) + .map(Integer::valueOf) + .collect(Collectors.toList()); + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/JSONArrayTypeHandler.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/JSONArrayTypeHandler.java new file mode 100644 index 00000000..f364144d --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/JSONArrayTypeHandler.java @@ -0,0 +1,60 @@ +package com.orion.ops.framework.mybatis.type; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * TODO TEST + * varchar -> JSONArray + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/25 10:33 + */ +@MappedJdbcTypes(JdbcType.VARCHAR) +@MappedTypes(JSONArray.class) +public class JSONArrayTypeHandler implements ITypeHandler { + + @Override + public void setParameter(PreparedStatement ps, int i, JSONArray res, JdbcType jdbcType) throws SQLException { + // todo TEST NULL + // 设置占位符 + ps.setString(i, res.toString()); + } + + @Override + public JSONArray getResult(ResultSet rs, String columnName) throws SQLException { + return this.getResult(rs.getString(columnName)); + } + + @Override + public JSONArray getResult(ResultSet rs, int columnIndex) throws SQLException { + return this.getResult(rs.getString(columnIndex)); + } + + @Override + public JSONArray getResult(CallableStatement cs, int columnIndex) throws SQLException { + return this.getResult(cs.getString(columnIndex)); + } + + @Override + public JSONArray getResult(String value) { + if (value == null) { + return null; + } + try { + return JSON.parseArray(value); + } catch (Exception e) { + return null; + } + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/JSONObjectTypeHandler.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/JSONObjectTypeHandler.java new file mode 100644 index 00000000..f2315af1 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/JSONObjectTypeHandler.java @@ -0,0 +1,60 @@ +package com.orion.ops.framework.mybatis.type; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * TODO TEST + * varchar -> JSONObject + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/25 10:33 + */ +@MappedJdbcTypes(JdbcType.VARCHAR) +@MappedTypes(JSONObject.class) +public class JSONObjectTypeHandler implements ITypeHandler { + + @Override + public void setParameter(PreparedStatement ps, int i, JSONObject res, JdbcType jdbcType) throws SQLException { + // todo TEST NULL + // 设置占位符 + ps.setString(i, res.toString()); + } + + @Override + public JSONObject getResult(ResultSet rs, String columnName) throws SQLException { + return this.getResult(rs.getString(columnName)); + } + + @Override + public JSONObject getResult(ResultSet rs, int columnIndex) throws SQLException { + return this.getResult(rs.getString(columnIndex)); + } + + @Override + public JSONObject getResult(CallableStatement cs, int columnIndex) throws SQLException { + return this.getResult(cs.getString(columnIndex)); + } + + @Override + public JSONObject getResult(String value) { + if (value == null) { + return null; + } + try { + return JSON.parseObject(value); + } catch (Exception e) { + return null; + } + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/LongListTypeHandler.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/LongListTypeHandler.java new file mode 100644 index 00000000..e72267b3 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/LongListTypeHandler.java @@ -0,0 +1,60 @@ +package com.orion.ops.framework.mybatis.type; + +import com.orion.lang.utils.Strings; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * TODO TEST + * varchar -> List + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/25 10:33 + */ +@MappedJdbcTypes(JdbcType.VARCHAR) +@MappedTypes(List.class) +public class LongListTypeHandler implements ITypeHandler> { + + @Override + public void setParameter(PreparedStatement ps, int i, List res, JdbcType jdbcType) throws SQLException { + // 设置占位符 + ps.setString(i, this.join(res)); + } + + @Override + public List getResult(ResultSet rs, String columnName) throws SQLException { + return this.getResult(rs.getString(columnName)); + } + + @Override + public List getResult(ResultSet rs, int columnIndex) throws SQLException { + return this.getResult(rs.getString(columnIndex)); + } + + @Override + public List getResult(CallableStatement cs, int columnIndex) throws SQLException { + return this.getResult(cs.getString(columnIndex)); + } + + @Override + public List getResult(String value) { + if (value == null) { + return null; + } + return Arrays.stream(value.split(COMMA)) + .filter(Strings::isNumber) + .map(Long::valueOf) + .collect(Collectors.toList()); + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/StringListTypeHandler.java b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/StringListTypeHandler.java new file mode 100644 index 00000000..b4011257 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/java/com/orion/ops/framework/mybatis/type/StringListTypeHandler.java @@ -0,0 +1,55 @@ +package com.orion.ops.framework.mybatis.type; + +import com.orion.lang.utils.collect.Lists; +import org.apache.ibatis.type.JdbcType; +import org.apache.ibatis.type.MappedJdbcTypes; +import org.apache.ibatis.type.MappedTypes; + +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +/** + * TODO TEST + * varchar -> List + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/6/25 10:33 + */ +@MappedJdbcTypes(JdbcType.VARCHAR) +@MappedTypes(List.class) +public class StringListTypeHandler implements ITypeHandler> { + + @Override + public void setParameter(PreparedStatement ps, int i, List res, JdbcType jdbcType) throws SQLException { + // 设置占位符 + ps.setString(i, this.join(res)); + } + + @Override + public List getResult(ResultSet rs, String columnName) throws SQLException { + return this.getResult(rs.getString(columnName)); + } + + @Override + public List getResult(ResultSet rs, int columnIndex) throws SQLException { + return this.getResult(rs.getString(columnIndex)); + } + + @Override + public List getResult(CallableStatement cs, int columnIndex) throws SQLException { + return this.getResult(cs.getString(columnIndex)); + } + + @Override + public List getResult(String value) { + if (value == null) { + return null; + } + return Lists.of(value.split(COMMA)); + } + +} diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..06e74745 --- /dev/null +++ b/orion-ops-framework/orion-ops-spring-boot-starter-mybatis/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.orion.ops.framework.mybatis.config.OrionMybatisAutoConfiguration \ No newline at end of file diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionLogPrinterConfiguration.java b/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionLogPrinterConfiguration.java index c3e41711..e668151b 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionLogPrinterConfiguration.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionLogPrinterConfiguration.java @@ -1,6 +1,6 @@ package com.orion.ops.framework.web.config; -import com.orion.ops.framework.common.enums.InterceptorOrderConst; +import com.orion.ops.framework.common.constant.InterceptorOrderConst; import com.orion.ops.framework.web.core.interceptor.LogPrintInterceptor; import org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor; import org.springframework.boot.autoconfigure.AutoConfiguration; diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionWebAutoConfiguration.java b/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionWebAutoConfiguration.java index 54ed66c2..b93a90c2 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionWebAutoConfiguration.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/config/OrionWebAutoConfiguration.java @@ -4,7 +4,7 @@ import com.alibaba.fastjson.serializer.SerializerFeature; import com.alibaba.fastjson.support.config.FastJsonConfig; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import com.orion.lang.utils.collect.Lists; -import com.orion.ops.framework.common.enums.FilterOrderConst; +import com.orion.ops.framework.common.constant.FilterOrderConst; import com.orion.ops.framework.web.core.filter.TraceIdFilter; import com.orion.ops.framework.web.core.handler.GlobalExceptionHandler; import com.orion.ops.framework.web.core.handler.WrapperResultHandler; diff --git a/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/core/handler/GlobalExceptionHandler.java b/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/core/handler/GlobalExceptionHandler.java index c0b5399b..2bf0ea6a 100644 --- a/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/core/handler/GlobalExceptionHandler.java +++ b/orion-ops-framework/orion-ops-spring-boot-starter-web/src/main/java/com/orion/ops/framework/web/core/handler/GlobalExceptionHandler.java @@ -7,7 +7,7 @@ import com.orion.lang.exception.argument.HttpWrapperException; import com.orion.lang.exception.argument.InvalidArgumentException; import com.orion.lang.exception.argument.RpcWrapperException; import com.orion.lang.utils.Exceptions; -import com.orion.ops.framework.common.enums.ExceptionMessageConst; +import com.orion.ops.framework.common.constant.ExceptionMessageConst; import lombok.extern.slf4j.Slf4j; import org.apache.poi.EncryptedDocumentException; import org.springframework.dao.DataAccessResourceFailureException; diff --git a/orion-ops-framework/pom.xml b/orion-ops-framework/pom.xml index 80c2fb62..b81d870e 100644 --- a/orion-ops-framework/pom.xml +++ b/orion-ops-framework/pom.xml @@ -21,6 +21,7 @@ orion-ops-spring-boot-starter-banner orion-ops-spring-boot-starter-swagger orion-ops-spring-boot-starter-datasource + orion-ops-spring-boot-starter-mybatis \ No newline at end of file diff --git a/orion-ops-server/pom.xml b/orion-ops-server/pom.xml index 1c3ae837..ee95d39e 100644 --- a/orion-ops-server/pom.xml +++ b/orion-ops-server/pom.xml @@ -40,10 +40,9 @@ com.orion.ops orion-ops-spring-boot-starter-datasource - - org.springframework.boot - spring-boot-starter-web + com.orion.ops + orion-ops-spring-boot-starter-mybatis diff --git a/orion-ops-server/src/main/resources/application-dev.yaml b/orion-ops-server/src/main/resources/application-dev.yaml index 13f001f3..431b52b8 100644 --- a/orion-ops-server/src/main/resources/application-dev.yaml +++ b/orion-ops-server/src/main/resources/application-dev.yaml @@ -1,10 +1,14 @@ spring: datasource: druid: - url: - username: - password: + url: jdbc:mysql://127.0.0.1:3306/orion-ops-pro?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai&autoReconnect=true + username: root + password: Data@123456 initial-size: 0 min-idle: 1 max-active: 5 +mybatis-plus: + configuration: + # 日志打印 + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl diff --git a/orion-ops-server/src/main/resources/application-prod.yaml b/orion-ops-server/src/main/resources/application-prod.yaml index 4cfba9cc..20249af4 100644 --- a/orion-ops-server/src/main/resources/application-prod.yaml +++ b/orion-ops-server/src/main/resources/application-prod.yaml @@ -1,9 +1,9 @@ spring: datasource: druid: - url: - username: - password: + url: jdbc:mysql://127.0.0.1:3306/orion-ops-pro?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=Asia/Shanghai&autoReconnect=true + username: root + password: Data@123456 # 初始连接数 initial-size: 5 # 最小连接池数量 diff --git a/orion-ops-server/src/main/resources/application.yaml b/orion-ops-server/src/main/resources/application.yaml index 853cf5e3..39d2142d 100644 --- a/orion-ops-server/src/main/resources/application.yaml +++ b/orion-ops-server/src/main/resources/application.yaml @@ -54,6 +54,16 @@ spring: config: multi-statement-allow: true +mybatis-plus: + configuration: + map-underscore-to-camel-case: true + mapper-locations: classpath*:mapper/*Mapper.xml + global-config: + db-config: + logic-delete-field: deleted + logic-not-delete-value: 1 + logic-delete-value: 2 + springdoc: api-docs: enabled: true