#临时提交 转到windows 开发

pull/432/head
廖金龙 2020-07-02 21:39:43 +08:00
parent 39fe0c3fc8
commit 41eadc4477
19 changed files with 319 additions and 56 deletions

View File

@ -1,12 +1,17 @@
package me.zhengjie.base.mybatis;
package me.zhengjie.base;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.google.common.collect.Sets;
import me.zhengjie.utils.WhereFun;
import me.zhengjie.utils.WrapperUtils;
import me.zhengjie.utils.enums.DbType;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.lang.Nullable;
import java.io.Serializable;
import java.util.List;
@ -17,12 +22,12 @@ import java.util.List;
* @author liaojinlong
* @since 2020/6/29 10:22
*/
public class BaseDao<I extends IService<T>, J extends JpaRepository<T, ID>, T, ID extends Serializable> {
public class BaseRepository<I extends IService<T>, J extends JpaRepository<T, ID>, T, ID extends Serializable> {
protected I mpService;
protected J jpaRepository;
protected DbType dbType = DbType.JPA;
public BaseDao(I mpService, J jpaRepository) {
public BaseRepository(I mpService, J jpaRepository) {
this.mpService = mpService;
this.jpaRepository = jpaRepository;
}
@ -141,6 +146,48 @@ public class BaseDao<I extends IService<T>, J extends JpaRepository<T, ID>, T, I
return t;
}
public List<T> findAll(@Nullable Specification<T> spec) {
List<T> t = null;
switch (dbType) {
case JPA:
t = ((JpaSpecificationExecutor) jpaRepository).findAll(spec);
break;
case MYBATIS:
t = mpFindAll(spec, null);
break;
default:
throw new IllegalStateException("Unexpected value: " + dbType);
}
return t;
}
public Object findAll(@Nullable Specification<T> spec, Pageable pageable) {
Object t = null;
switch (dbType) {
case JPA:
t = ((JpaSpecificationExecutor) jpaRepository).findAll(spec, pageable);
break;
case MYBATIS:
t = mpFindAll(spec, pageable);
break;
default:
throw new IllegalStateException("Unexpected value: " + dbType);
}
return t;
}
/**
* Mp
*
* @param spec
* @param pageable
* @return
*/
protected List<T> mpFindAll(Specification<T> spec, Pageable pageable) {
final QueryWrapper<T> query = Wrappers.<T>query();
return mpService.page(null, null);
}
public I getMpService() {
return mpService;
}

View File

@ -0,0 +1,122 @@
/*
* Copyright 2019-2020
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 me.zhengjie.db;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.Assert;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
/**
* MyBatis & JPA
*
* @author liaojinlong
* @since 2020/7/2 19:18
*/
public class ElPredicate<T> implements Predicate<T> {
private Predicate<T> predicate;
private QueryWrapper queryWrapper;
public ElPredicate(Predicate<T> predicate, QueryWrapper queryWrapper) {
this.predicate = predicate;
this.queryWrapper = queryWrapper;
}
/**
* Evaluates this predicate on the given argument.
*
* @param o the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
@Override
public boolean test(T o) {
return predicate.test(o);
}
/**
* Returns a composed predicate that represents a short-circuiting logical
* AND of this predicate and another. When evaluating the composed
* predicate, if this predicate is {@code false}, then the {@code other}
* predicate is not evaluated.
*
* <p>Any exceptions thrown during evaluation of either predicate are relayed
* to the caller; if evaluation of this predicate throws an exception, the
* {@code other} predicate will not be evaluated.
*
* @param other a predicate that will be logically-ANDed with this
* predicate
* @return a composed predicate that represents the short-circuiting logical
* AND of this predicate and the {@code other} predicate
* @throws NullPointerException if other is null
*/
@Override
public Predicate and(Predicate other) {
return predicate.and(other);
}
/**
* Returns a predicate that represents the logical negation of this
* predicate.
*
* @return a predicate that represents the logical negation of this
* predicate
*/
@Override
public Predicate negate() {
return predicate.negate();
}
/**
* Returns a composed predicate that represents a short-circuiting logical
* OR of this predicate and another. When evaluating the composed
* predicate, if this predicate is {@code true}, then the {@code other}
* predicate is not evaluated.
*
* <p>Any exceptions thrown during evaluation of either predicate are relayed
* to the caller; if evaluation of this predicate throws an exception, the
* {@code other} predicate will not be evaluated.
*
* @param other a predicate that will be logically-ORed with this
* predicate
* @return a composed predicate that represents the short-circuiting logical
* OR of this predicate and the {@code other} predicate
* @throws NullPointerException if other is null
*/
@Override
public Predicate or(Predicate other) {
return predicate.or(other);
}
public static synchronized <T> List<ElPredicate<T>> toConvert(List<Predicate<T>> predicates, List<QueryWrapper<T>> queryWrappers) {
Assert.isTrue(predicates.size() == queryWrappers.size(), "二者数量必须匹配");
List<ElPredicate<T>> result = new ArrayList<>(queryWrappers.size());
for (int i = 0; i < predicates.size(); i++) {
Predicate<T> tPredicate = predicates.get(i);
QueryWrapper<T> queryWrapper = queryWrappers.get(i);
result.add(toConvert(tPredicate, queryWrapper));
}
return result;
}
public static synchronized <T> ElPredicate<T> toConvert(Predicate<T> predicate, QueryWrapper<T> queryWrapper) {
return new ElPredicate<T>(predicate, queryWrapper);
}
}

View File

@ -0,0 +1,27 @@
/*
* Copyright 2019-2020
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 me.zhengjie.db;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.data.jpa.domain.Specification;
/**
* @author liaojinlong
* @since 2020/7/2 19:39
*/
public abstract class ElSpecification<T> implements Specification {
protected QueryWrapper<T> queryWrapper;
}

View File

@ -13,7 +13,7 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package me.zhengjie.mybatis.annotation.impl;
package me.zhengjie.db.mybatis;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.FieldStrategy;

View File

@ -13,7 +13,7 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package me.zhengjie.mybatis.annotation.impl;
package me.zhengjie.db.mybatis;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;

View File

@ -13,7 +13,7 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
package me.zhengjie.mybatis.annotation.impl;
package me.zhengjie.db.mybatis;
import com.baomidou.mybatisplus.annotation.TableName;

View File

@ -21,6 +21,7 @@ import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.DataPermission;
import me.zhengjie.annotation.Query;
import javax.persistence.criteria.*;
import java.lang.reflect.Field;
import java.util.*;
@ -32,7 +33,16 @@ import java.util.*;
@Slf4j
@SuppressWarnings({"unchecked", "all"})
public class QueryHelp {
/**
* JPA
*
* @param root
* @param query
* @param cb
* @param <R>
* @param <Q>
* @return /
*/
public static <R, Q> Predicate getPredicate(Root<R> root, Q query, CriteriaBuilder cb) {
List<Predicate> list = new ArrayList<>();
if (query == null) {
@ -107,7 +117,8 @@ public class QueryHelp {
join = root.join(name, JoinType.INNER);
}
break;
default: break;
default:
break;
}
}
}
@ -159,7 +170,8 @@ public class QueryHelp {
list.add(cb.between(getExpression(attributeName, join, root).as((Class<? extends Comparable>) between.get(0).getClass()),
(Comparable) between.get(0), (Comparable) between.get(1)));
break;
default: break;
default:
break;
}
}
field.setAccessible(accessible);
@ -167,6 +179,7 @@ public class QueryHelp {
} catch (Exception e) {
log.error(e.getMessage(), e);
}
int size = list.size();
return cb.and(list.toArray(new Predicate[size]));
}

View File

@ -0,0 +1,49 @@
/*
* Copyright 2019-2020
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 me.zhengjie.utils;
import me.zhengjie.db.ElSpecification;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
/**
* @author liaojinlong
* @since 2020/7/2 19:44
*/
public class SpecificationUtils {
public static synchronized <T> ElSpecification getSpecification() {
return new ElSpecification<T>() {
/**
* Creates a WHERE clause for a query of the referenced entity in form of a {@link Predicate} for the given
* {@link Root} and {@link CriteriaQuery}.
*
* @param root must not be {@literal null}.
* @param query must not be {@literal null}.
* @param criteriaBuilder must not be {@literal null}.
* @return a {@link Predicate}, may be {@literal null}.
*/
@Override
public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) {
return QueryHelp.getPredicate(root, query, criteriaBuilder);
}
};
}
}

View File

@ -16,10 +16,10 @@
package me.zhengjie.repository;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import me.zhengjie.base.mybatis.BaseDao;
import me.zhengjie.base.BaseRepository;
import me.zhengjie.domain.ColumnInfo;
import me.zhengjie.repository.jpa.ColumnInfoRepository;
import me.zhengjie.repository.mp.ColumnInfoService;
import me.zhengjie.repository.jpa.ColumnInfoJpaRepository;
import me.zhengjie.repository.mp.ColumnInfoMpService;
import me.zhengjie.utils.enums.DbType;
import org.springframework.stereotype.Component;
@ -32,9 +32,9 @@ import java.util.List;
* @since 2020/6/28 14:59
*/
@Component
public class ColumnInfoDao extends BaseDao<ColumnInfoService, ColumnInfoRepository, ColumnInfo, Long> {
public class ColumnInfoRepository extends BaseRepository<ColumnInfoMpService, ColumnInfoJpaRepository, ColumnInfo, Long> {
public ColumnInfoDao(ColumnInfoService baseService, ColumnInfoRepository jpaRepository) {
public ColumnInfoRepository(ColumnInfoMpService baseService, ColumnInfoJpaRepository jpaRepository) {
super(baseService, jpaRepository);
setDbType(DbType.MYBATIS);
}

View File

@ -1,10 +1,10 @@
package me.zhengjie.repository;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import me.zhengjie.base.mybatis.BaseDao;
import me.zhengjie.base.BaseRepository;
import me.zhengjie.domain.GenConfig;
import me.zhengjie.repository.jpa.GenConfigRepository;
import me.zhengjie.repository.mp.GenConfigService;
import me.zhengjie.repository.jpa.GenConfigJpaRepository;
import me.zhengjie.repository.mp.GenConfigMpService;
import org.springframework.stereotype.Repository;
@ -15,8 +15,8 @@ import org.springframework.stereotype.Repository;
* @since 2020/7/1 23:00
*/
@Repository
public class GenConfigDao extends BaseDao<GenConfigService, GenConfigRepository, GenConfig, Long> {
public GenConfigDao(GenConfigService mpService, GenConfigRepository jpaRepository) {
public class GenConfigRepository extends BaseRepository<GenConfigMpService, GenConfigJpaRepository, GenConfig, Long> {
public GenConfigRepository(GenConfigMpService mpService, GenConfigJpaRepository jpaRepository) {
super(mpService, jpaRepository);
}

View File

@ -17,16 +17,19 @@ package me.zhengjie.repository.jpa;
import me.zhengjie.domain.ColumnInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.util.List;
/**
* @author Zheng Jie
* @date 2019-01-14
*/
public interface ColumnInfoRepository extends JpaRepository<ColumnInfo,Long> {
public interface ColumnInfoJpaRepository extends JpaRepository<ColumnInfo, Long>, JpaSpecificationExecutor<ColumnInfo> {
/**
*
*
* @param tableName
* @return
*/

View File

@ -15,17 +15,20 @@
*/
package me.zhengjie.repository.jpa;
import me.zhengjie.domain.ColumnInfo;
import me.zhengjie.domain.GenConfig;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
/**
* @author Zheng Jie
* @date 2019-01-14
*/
public interface GenConfigRepository extends JpaRepository<GenConfig,Long> {
public interface GenConfigJpaRepository extends JpaRepository<GenConfig, Long>, JpaSpecificationExecutor<ColumnInfo> {
/**
*
*
* @param tableName
* @return /
*/

View File

@ -22,5 +22,5 @@ import me.zhengjie.domain.ColumnInfo;
* @author liaojinlong
* @since 2020/6/28 14:57
*/
public interface ColumnInfoMapper extends BaseMapper<ColumnInfo> {
public interface ColumnInfoMpMapper extends BaseMapper<ColumnInfo> {
}

View File

@ -24,5 +24,5 @@ import org.springframework.stereotype.Repository;
* @since 2020/6/28 14:57
*/
@Repository
public class ColumnInfoService extends ServiceImpl<ColumnInfoMapper, ColumnInfo> {
public class ColumnInfoMpService extends ServiceImpl<ColumnInfoMpMapper, ColumnInfo> {
}

View File

@ -23,5 +23,5 @@ import me.zhengjie.domain.GenConfig;
* @author liaojinlong
* @since 2020/6/28 14:57
*/
public interface GenConfigMapper extends BaseMapper<GenConfig> {
public interface GenConfigMpMapper extends BaseMapper<GenConfig> {
}

View File

@ -24,5 +24,5 @@ import org.springframework.stereotype.Repository;
* @since 2020/6/28 14:57
*/
@Repository
public class GenConfigService extends ServiceImpl<GenConfigMapper, GenConfig> {
public class GenConfigMpService extends ServiceImpl<GenConfigMpMapper, GenConfig> {
}

View File

@ -17,8 +17,7 @@ package me.zhengjie.service.impl;
import lombok.RequiredArgsConstructor;
import me.zhengjie.domain.GenConfig;
import me.zhengjie.repository.GenConfigDao;
import me.zhengjie.repository.jpa.GenConfigRepository;
import me.zhengjie.repository.GenConfigRepository;
import me.zhengjie.service.GenConfigService;
import me.zhengjie.utils.StringUtils;
import org.springframework.stereotype.Service;
@ -32,7 +31,7 @@ import java.io.File;
@RequiredArgsConstructor
public class GenConfigServiceImpl implements GenConfigService {
private final GenConfigDao genConfigDao;
private final GenConfigRepository genConfigDao;
@Override
public GenConfig find(String tableName) {

View File

@ -23,7 +23,7 @@ import me.zhengjie.domain.GenConfig;
import me.zhengjie.domain.ColumnInfo;
import me.zhengjie.domain.vo.TableInfo;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.repository.ColumnInfoDao;
import me.zhengjie.repository.ColumnInfoRepository;
import me.zhengjie.service.GeneratorService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.GenUtil;
@ -58,7 +58,7 @@ public class GeneratorServiceImpl implements GeneratorService {
@PersistenceContext
private EntityManager em;
private final ColumnInfoDao columnInfoDao;
private final ColumnInfoRepository columnInfoDao;
@Override
public Object getTables() {

View File

@ -19,8 +19,8 @@ import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
import com.baomidou.mybatisplus.core.toolkit.*;
import me.zhengjie.mybatis.annotation.impl.TableFieldImp;
import me.zhengjie.mybatis.annotation.impl.TableIdImp;
import me.zhengjie.db.mybatis.TableFieldImp;
import me.zhengjie.db.mybatis.TableIdImp;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.executor.keygen.KeyGenerator;