mirror of https://github.com/elunez/eladmin
# 修改适配类
parent
eba524e412
commit
b8b23c351e
|
@ -0,0 +1,123 @@
|
|||
package me.zhengjie.base;
|
||||
|
||||
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 org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* mybatisPlus & JPA 适配
|
||||
*
|
||||
* @author liaojinlong
|
||||
* @since 2020/6/29 10:22
|
||||
*/
|
||||
public class BaseDao<I extends IService<T>, J extends JpaRepository<T, ID>, T, ID extends Serializable> {
|
||||
protected I mpService;
|
||||
protected J jpaRepository;
|
||||
@Value("${db.type.switch:true}")
|
||||
protected boolean dbSwitch;
|
||||
|
||||
public BaseDao(I mpService, J jpaRepository) {
|
||||
this.mpService = mpService;
|
||||
this.jpaRepository = jpaRepository;
|
||||
}
|
||||
|
||||
public void save(T columnInfo) {
|
||||
if (dbSwitch) {
|
||||
jpaRepository.save(columnInfo);
|
||||
} else {
|
||||
mpService.save(columnInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public List<T> saveAll(List<T> entities) {
|
||||
if (dbSwitch) {
|
||||
return jpaRepository.saveAll(entities);
|
||||
} else {
|
||||
mpService.saveBatch(entities);
|
||||
return entities;
|
||||
}
|
||||
}
|
||||
|
||||
public void delete(T entity) {
|
||||
if (dbSwitch) {
|
||||
jpaRepository.delete(entity);
|
||||
} else {
|
||||
mpService.remove(WrapperUtils.excute(entity, Wrappers.query(), WhereFun.DEFAULT));
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteById(ID id) {
|
||||
if (dbSwitch) {
|
||||
jpaRepository.deleteById(id);
|
||||
} else {
|
||||
mpService.removeById(id);
|
||||
}
|
||||
}
|
||||
|
||||
public void update(T columnInfo) {
|
||||
if (dbSwitch) {
|
||||
jpaRepository.saveAndFlush(columnInfo);
|
||||
} else {
|
||||
mpService.saveOrUpdate(columnInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public void batUpdate(List<T> entities) {
|
||||
if (dbSwitch) {
|
||||
jpaRepository.saveAll(entities);
|
||||
} else {
|
||||
mpService.saveOrUpdateBatch(entities);
|
||||
}
|
||||
}
|
||||
|
||||
public T selectById(ID id) {
|
||||
T t;
|
||||
if (dbSwitch) {
|
||||
t = jpaRepository.getOne(id);
|
||||
} else {
|
||||
t = mpService.getById(id);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
public List<T> selectAllById(Iterable<ID> ids) {
|
||||
List<T> t;
|
||||
if (dbSwitch) {
|
||||
t = jpaRepository.findAllById(ids);
|
||||
} else {
|
||||
t = mpService.listByIds(Sets.newHashSet(ids));
|
||||
}
|
||||
return t;
|
||||
}
|
||||
|
||||
public I getMpService() {
|
||||
return mpService;
|
||||
}
|
||||
|
||||
public void setMpService(I mpService) {
|
||||
this.mpService = mpService;
|
||||
}
|
||||
|
||||
public J getJpaRepository() {
|
||||
return jpaRepository;
|
||||
}
|
||||
|
||||
public void setJpaRepository(J jpaRepository) {
|
||||
this.jpaRepository = jpaRepository;
|
||||
}
|
||||
|
||||
public boolean isDbSwitch() {
|
||||
return dbSwitch;
|
||||
}
|
||||
|
||||
public void setDbSwitch(boolean dbSwitch) {
|
||||
this.dbSwitch = dbSwitch;
|
||||
}
|
||||
}
|
|
@ -1,11 +1,10 @@
|
|||
package me.zhengjie.repository;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import me.zhengjie.base.BaseDao;
|
||||
import me.zhengjie.domain.ColumnInfo;
|
||||
import me.zhengjie.repository.jpa.ColumnInfoRepository;
|
||||
import me.zhengjie.repository.mp.ColumnInfoService;
|
||||
import me.zhengjie.utils.WhereFun;
|
||||
import me.zhengjie.utils.WrapperUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -15,57 +14,19 @@ import java.util.List;
|
|||
* @since 2020/6/28 14:59
|
||||
*/
|
||||
@Component
|
||||
public class ColumnInfoDao {
|
||||
private Boolean dbType = false;
|
||||
private ColumnInfoService columnInfoService;
|
||||
private ColumnInfoRepository columnInfoRepository;
|
||||
public class ColumnInfoDao extends BaseDao<ColumnInfoService, ColumnInfoRepository, ColumnInfo, Long> {
|
||||
|
||||
public ColumnInfoDao(ColumnInfoService columnInfoService, ColumnInfoRepository columnInfoRepository) {
|
||||
this.columnInfoService = columnInfoService;
|
||||
this.columnInfoRepository = columnInfoRepository;
|
||||
public ColumnInfoDao(ColumnInfoService baseService, ColumnInfoRepository jpaRepository ) {
|
||||
super(baseService, jpaRepository);
|
||||
}
|
||||
|
||||
public List<ColumnInfo> findByTableNameOrderByIdAsc(String tableName) {
|
||||
if (false) {
|
||||
return columnInfoRepository.findByTableNameOrderByIdAsc(tableName);
|
||||
if (dbSwitch) {
|
||||
return jpaRepository.findByTableNameOrderByIdAsc(tableName);
|
||||
} else {
|
||||
return columnInfoService
|
||||
.selectList(Wrappers.<ColumnInfo>query().eq(true, "TABLE_NAME", tableName));
|
||||
return mpService
|
||||
.list(Wrappers.<ColumnInfo>query().eq(true, "TABLE_NAME", tableName));
|
||||
}
|
||||
}
|
||||
|
||||
public List<ColumnInfo> saveAll(List<ColumnInfo> columnInfos) {
|
||||
if (true) {
|
||||
return columnInfoRepository.saveAll(columnInfos);
|
||||
} else {
|
||||
columnInfos.forEach(columnInfo -> {
|
||||
columnInfoService.insert(columnInfo);
|
||||
});
|
||||
return columnInfos;
|
||||
}
|
||||
}
|
||||
|
||||
public void delete(ColumnInfo columnInfo) {
|
||||
if (dbType) {
|
||||
columnInfoRepository.delete(columnInfo);
|
||||
} else {
|
||||
columnInfoService.delete(WrapperUtils.excute(columnInfo, Wrappers.query(), WhereFun.DEFAULT));
|
||||
}
|
||||
}
|
||||
|
||||
public void save(ColumnInfo columnInfo) {
|
||||
if (dbType) {
|
||||
columnInfoRepository.save(columnInfo);
|
||||
} else {
|
||||
columnInfoService.insert(columnInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public void update(ColumnInfo columnInfo) {
|
||||
if (dbType) {
|
||||
columnInfoRepository.saveAndFlush(columnInfo);
|
||||
} else {
|
||||
columnInfoService.updateById(columnInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package me.zhengjie.repository.mp;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import me.zhengjie.domain.ColumnInfo;
|
||||
|
||||
/**
|
||||
* @author liaojinlong
|
||||
* @since 2020/6/28 14:57
|
||||
*/
|
||||
public interface ColumnInfoMapper extends BaseMapper<ColumnInfo> {
|
||||
}
|
|
@ -1,11 +1,13 @@
|
|||
package me.zhengjie.repository.mp;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import me.zhengjie.domain.ColumnInfo;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* @author liaojinlong
|
||||
* @since 2020/6/28 14:57
|
||||
*/
|
||||
public interface ColumnInfoService extends BaseMapper<ColumnInfo> {
|
||||
@Repository
|
||||
public class ColumnInfoService extends ServiceImpl<ColumnInfoMapper, ColumnInfo> {
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue