mirror of https://github.com/elunez/eladmin
# 修改 ID生成方式,
parent
55f3797cad
commit
a90c851caf
|
@ -1,3 +1,18 @@
|
||||||
|
/*
|
||||||
|
* 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.base;
|
package me.zhengjie.base;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||||
|
@ -39,7 +54,13 @@ public class BaseRepository<I extends IService<T>, J extends JpaRepository<T, ID
|
||||||
this.jpaRepository = jpaRepository;
|
this.jpaRepository = jpaRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public T save(T t) {
|
/**
|
||||||
|
* JPA & MP 仅保存 ,不涉及 保存前判断存在时 转更新
|
||||||
|
*
|
||||||
|
* @param t
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
public T onlySave(T t) {
|
||||||
T t1;
|
T t1;
|
||||||
switch (dbType) {
|
switch (dbType) {
|
||||||
case JPA:
|
case JPA:
|
||||||
|
@ -55,7 +76,35 @@ public class BaseRepository<I extends IService<T>, J extends JpaRepository<T, ID
|
||||||
return t1;
|
return t1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JPA & MP 保存/ 更新 ,当ID 存在时 进行更新
|
||||||
|
*
|
||||||
|
* @param t
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
public T save(T t) {
|
||||||
|
T t1;
|
||||||
|
switch (dbType) {
|
||||||
|
case JPA:
|
||||||
|
t1 = jpaRepository.save(t);
|
||||||
|
break;
|
||||||
|
case MYBATIS:
|
||||||
|
mpService.saveOrUpdate(t);
|
||||||
|
t1 = t;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalStateException("Unexpected value: " + dbType);
|
||||||
|
}
|
||||||
|
return t1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量更新
|
||||||
|
*
|
||||||
|
* @param entities
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
public List<T> saveAll(List<T> entities) {
|
public List<T> saveAll(List<T> entities) {
|
||||||
List<T> result;
|
List<T> result;
|
||||||
switch (dbType) {
|
switch (dbType) {
|
||||||
|
@ -72,6 +121,11 @@ public class BaseRepository<I extends IService<T>, J extends JpaRepository<T, ID
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JPA & MP 删除
|
||||||
|
*
|
||||||
|
* @param entity
|
||||||
|
*/
|
||||||
public void delete(T entity) {
|
public void delete(T entity) {
|
||||||
switch (dbType) {
|
switch (dbType) {
|
||||||
case JPA:
|
case JPA:
|
||||||
|
@ -85,6 +139,11 @@ public class BaseRepository<I extends IService<T>, J extends JpaRepository<T, ID
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JPA & MP 更新
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
*/
|
||||||
public void deleteById(ID id) {
|
public void deleteById(ID id) {
|
||||||
switch (dbType) {
|
switch (dbType) {
|
||||||
case JPA:
|
case JPA:
|
||||||
|
@ -98,19 +157,29 @@ public class BaseRepository<I extends IService<T>, J extends JpaRepository<T, ID
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(T columnInfo) {
|
/**
|
||||||
|
* 通过 ID 保存
|
||||||
|
*
|
||||||
|
* @param t
|
||||||
|
*/
|
||||||
|
public void update(T t) {
|
||||||
switch (dbType) {
|
switch (dbType) {
|
||||||
case JPA:
|
case JPA:
|
||||||
jpaRepository.saveAndFlush(columnInfo);
|
jpaRepository.saveAndFlush(t);
|
||||||
break;
|
break;
|
||||||
case MYBATIS:
|
case MYBATIS:
|
||||||
mpService.saveOrUpdate(columnInfo);
|
mpService.updateById(t);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new IllegalStateException("Unexpected value: " + dbType);
|
throw new IllegalStateException("Unexpected value: " + dbType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量更新
|
||||||
|
*
|
||||||
|
* @param entities
|
||||||
|
*/
|
||||||
public void batUpdate(List<T> entities) {
|
public void batUpdate(List<T> entities) {
|
||||||
switch (dbType) {
|
switch (dbType) {
|
||||||
case JPA:
|
case JPA:
|
||||||
|
@ -124,6 +193,12 @@ public class BaseRepository<I extends IService<T>, J extends JpaRepository<T, ID
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 依据ID 查找
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
public T selectById(ID id) {
|
public T selectById(ID id) {
|
||||||
T t;
|
T t;
|
||||||
switch (dbType) {
|
switch (dbType) {
|
||||||
|
@ -139,10 +214,22 @@ public class BaseRepository<I extends IService<T>, J extends JpaRepository<T, ID
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 兼容JPA 依据ID 查询
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
public Optional<T> findById(ID id) {
|
public Optional<T> findById(ID id) {
|
||||||
return Optional.of(selectById(id));
|
return Optional.of(selectById(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 依据ID 批量查询
|
||||||
|
*
|
||||||
|
* @param ids
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
public List<T> selectAllById(Iterable<ID> ids) {
|
public List<T> selectAllById(Iterable<ID> ids) {
|
||||||
List<T> t;
|
List<T> t;
|
||||||
switch (dbType) {
|
switch (dbType) {
|
||||||
|
@ -158,6 +245,12 @@ public class BaseRepository<I extends IService<T>, J extends JpaRepository<T, ID
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 兼容JPA 查询
|
||||||
|
*
|
||||||
|
* @param spec
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
public List<T> findAll(@Nullable Specification<T> spec) {
|
public List<T> findAll(@Nullable Specification<T> spec) {
|
||||||
List<T> t = null;
|
List<T> t = null;
|
||||||
switch (dbType) {
|
switch (dbType) {
|
||||||
|
@ -226,6 +319,12 @@ public class BaseRepository<I extends IService<T>, J extends JpaRepository<T, ID
|
||||||
pageable, page.getTotal());
|
pageable, page.getTotal());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MP
|
||||||
|
*
|
||||||
|
* @param spec
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
protected List<T> mpFindAll(Specification<T> spec) {
|
protected List<T> mpFindAll(Specification<T> spec) {
|
||||||
ElSpecification<T> specifications = (ElSpecification<T>) spec;
|
ElSpecification<T> specifications = (ElSpecification<T>) spec;
|
||||||
final QueryWrapper<T> queryWrapper = specifications.getQueryWrapper();
|
final QueryWrapper<T> queryWrapper = specifications.getQueryWrapper();
|
||||||
|
|
|
@ -198,6 +198,14 @@ public class QueryHelp {
|
||||||
return cb.and(list.toArray(new Predicate[size]));
|
return cb.and(list.toArray(new Predicate[size]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mybatis Plus 查询构建
|
||||||
|
*
|
||||||
|
* @param criteria
|
||||||
|
* @param clazz
|
||||||
|
* @param <K>
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
public static <K> QueryWrapper<K> getQueryWrapper(Object criteria, Class clazz) {
|
public static <K> QueryWrapper<K> getQueryWrapper(Object criteria, Class clazz) {
|
||||||
QueryWrapper<K> queryWrapper = Wrappers.query();
|
QueryWrapper<K> queryWrapper = Wrappers.query();
|
||||||
final List<Field> allFields = getAllFields(criteria.getClass());
|
final List<Field> allFields = getAllFields(criteria.getClass());
|
||||||
|
@ -229,9 +237,15 @@ public class QueryHelp {
|
||||||
}
|
}
|
||||||
if (Objects.nonNull(query)) {
|
if (Objects.nonNull(query)) {
|
||||||
String propName = query.propName();
|
String propName = query.propName();
|
||||||
String joinName = query.joinName();
|
|
||||||
String blurry = query.blurry();
|
String blurry = query.blurry();
|
||||||
attributeName = isBlank(propName) ? attributeName : propName;
|
attributeName = isBlank(propName) ? attributeName : propName;
|
||||||
|
if (StringUtils.isNotBlank(blurry)) {
|
||||||
|
String[] blurrys = blurry.split(",");
|
||||||
|
for (String item : blurrys) {
|
||||||
|
queryWrapper.like(item, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
List<Object> between = null;
|
List<Object> between = null;
|
||||||
switch (query.type()) {
|
switch (query.type()) {
|
||||||
case EQUAL:
|
case EQUAL:
|
||||||
|
@ -302,7 +316,14 @@ public class QueryHelp {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getTableColumnFromField(TableInfo tableInfo, Field field) {
|
/**
|
||||||
|
* 依据Mybatis Plus 获取 Database 真实Column 字段
|
||||||
|
*
|
||||||
|
* @param tableInfo
|
||||||
|
* @param field
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
public static String getTableColumnFromField(TableInfo tableInfo, Field field) {
|
||||||
String columnName = null;
|
String columnName = null;
|
||||||
if (FIELD_CACHE.containsKey(field)) {
|
if (FIELD_CACHE.containsKey(field)) {
|
||||||
final ElField elField = COLUMN_CACHE.get(field);
|
final ElField elField = COLUMN_CACHE.get(field);
|
||||||
|
@ -348,6 +369,10 @@ public class QueryHelp {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author liaojinlong
|
||||||
|
* @since 2020/7/6 21:04
|
||||||
|
*/
|
||||||
class ElField {
|
class ElField {
|
||||||
private String column;
|
private String column;
|
||||||
private boolean status;
|
private boolean status;
|
||||||
|
|
|
@ -20,7 +20,6 @@ import me.zhengjie.base.BaseRepository;
|
||||||
import me.zhengjie.domain.ColumnInfo;
|
import me.zhengjie.domain.ColumnInfo;
|
||||||
import me.zhengjie.repository.jpa.ColumnInfoJpaRepository;
|
import me.zhengjie.repository.jpa.ColumnInfoJpaRepository;
|
||||||
import me.zhengjie.repository.mp.ColumnInfoMpService;
|
import me.zhengjie.repository.mp.ColumnInfoMpService;
|
||||||
import me.zhengjie.utils.enums.DbType;
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -36,7 +35,7 @@ public class ColumnInfoRepository extends BaseRepository<ColumnInfoMpService, Co
|
||||||
|
|
||||||
public ColumnInfoRepository(ColumnInfoMpService baseService, ColumnInfoJpaRepository jpaRepository) {
|
public ColumnInfoRepository(ColumnInfoMpService baseService, ColumnInfoJpaRepository jpaRepository) {
|
||||||
super(baseService, jpaRepository);
|
super(baseService, jpaRepository);
|
||||||
setDbType(DbType.MYBATIS);
|
// setDbType(DbType.MYBATIS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ColumnInfo> findByTableNameOrderByIdAsc(String tableName) {
|
public List<ColumnInfo> findByTableNameOrderByIdAsc(String tableName) {
|
||||||
|
|
|
@ -1,3 +1,18 @@
|
||||||
|
/*
|
||||||
|
* 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.repository;
|
package me.zhengjie.repository;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
|
|
|
@ -52,6 +52,9 @@ import static java.util.stream.Collectors.toList;
|
||||||
* @since 2020/6/29 18:07
|
* @since 2020/6/29 18:07
|
||||||
*/
|
*/
|
||||||
public class TableInfoHelper {
|
public class TableInfoHelper {
|
||||||
|
static {
|
||||||
|
System.out.println("MyBatis Plus (Jpa Patch)");
|
||||||
|
}
|
||||||
|
|
||||||
private static final Log logger = LogFactory.getLog(TableInfoHelper.class);
|
private static final Log logger = LogFactory.getLog(TableInfoHelper.class);
|
||||||
|
|
||||||
|
@ -136,9 +139,6 @@ public class TableInfoHelper {
|
||||||
// 兼容测试场景
|
// 兼容测试场景
|
||||||
globalConfig = GlobalConfigUtils.defaults();
|
globalConfig = GlobalConfigUtils.defaults();
|
||||||
}
|
}
|
||||||
if (globalConfig.isBanner()) {
|
|
||||||
System.out.println("MyBatis Plus (Jpa Patch)");
|
|
||||||
}
|
|
||||||
/* 初始化表名相关 */
|
/* 初始化表名相关 */
|
||||||
final String[] excludeProperty = initTableName(clazz, globalConfig, tableInfo);
|
final String[] excludeProperty = initTableName(clazz, globalConfig, tableInfo);
|
||||||
|
|
||||||
|
@ -320,8 +320,8 @@ public class TableInfoHelper {
|
||||||
}
|
}
|
||||||
GeneratedValue generatedValue = field.getAnnotation(GeneratedValue.class);
|
GeneratedValue generatedValue = field.getAnnotation(GeneratedValue.class);
|
||||||
if (Objects.nonNull(generatedValue)) {
|
if (Objects.nonNull(generatedValue)) {
|
||||||
tableIdImp.setType(IdType.ASSIGN_ID);
|
tableIdImp.setType(IdType.AUTO);
|
||||||
logger.warn("JPA compatible mode, []com.baomidou.mybatisplus.annotation.IdType.ASSIGN_ID] is the only way to generate primary key");
|
logger.warn("JPA compatible mode, []com.baomidou.mybatisplus.annotation.IdType.AUTO] is the only way to generate primary key");
|
||||||
}
|
}
|
||||||
tableId = tableIdImp;
|
tableId = tableIdImp;
|
||||||
jpaReadPK = true;
|
jpaReadPK = true;
|
||||||
|
|
|
@ -17,7 +17,6 @@ package me.zhengjie.modules.system.repository;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import me.zhengjie.base.BaseRepository;
|
import me.zhengjie.base.BaseRepository;
|
||||||
import me.zhengjie.domain.ColumnInfo;
|
|
||||||
import me.zhengjie.modules.system.domain.Job;
|
import me.zhengjie.modules.system.domain.Job;
|
||||||
import me.zhengjie.modules.system.repository.jpa.JobJpaRepository;
|
import me.zhengjie.modules.system.repository.jpa.JobJpaRepository;
|
||||||
import me.zhengjie.modules.system.repository.mp.JobService;
|
import me.zhengjie.modules.system.repository.mp.JobService;
|
||||||
|
@ -39,6 +38,9 @@ public class JobRepository extends BaseRepository<JobService, JobJpaRepository,
|
||||||
setDbType(DbType.MYBATIS);
|
setDbType(DbType.MYBATIS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ids
|
||||||
|
*/
|
||||||
public void deleteAllByIdIn(Set<Long> ids) {
|
public void deleteAllByIdIn(Set<Long> ids) {
|
||||||
switch (dbType) {
|
switch (dbType) {
|
||||||
case JPA:
|
case JPA:
|
||||||
|
@ -52,6 +54,10 @@ public class JobRepository extends BaseRepository<JobService, JobJpaRepository,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param name
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
public Job findByName(String name) {
|
public Job findByName(String name) {
|
||||||
Job result;
|
Job result;
|
||||||
switch (dbType) {
|
switch (dbType) {
|
||||||
|
|
Loading…
Reference in New Issue