feat IJPACommData基础数据操作接口

V0.5.x
jay 2023-06-01 10:40:32 +08:00
parent 7b5419ce94
commit 91f6b73b45
4 changed files with 138 additions and 23 deletions

View File

@ -0,0 +1,110 @@
package cc.iotkit.data.dao;
import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.api.Paging;
import cc.iotkit.data.ICommonData;
import cc.iotkit.data.util.PageBuilder;
import cc.iotkit.data.util.PredicateBuilder;
import cc.iotkit.model.Id;
import com.querydsl.core.types.Predicate;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.repository.JpaRepository;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
/**
* @Author: jay
* @Date: 2023/6/1 9:27
* @Version: V1.0
* @Description:
*/
public interface IJPACommData<T extends Id<ID>, ID> extends ICommonData<T , ID> {
JpaRepository getBaseRepository();
@Override
default T findById(ID id) {
return (T)getBaseRepository().findById(id).orElse(null);
}
@Override
default List<T> findByIds(Collection<ID> id) {
List allById = getBaseRepository().findAllById(id);
return allById;
}
@Override
default T save(T data) {
Object o = getBaseRepository().save(data);
return (T) o;
}
@Override
default void batchSave(List<T> data) {
getBaseRepository().saveAll(data);
}
@Override
default void deleteById(ID id) {
getBaseRepository().deleteById(id);
}
@Override
default void deleteByIds(Collection<ID> ids) {
getBaseRepository().deleteAllById(ids);
}
@Override
default long count() {
return getBaseRepository().count();
}
@Override
default List findAll() {
return getBaseRepository().findAll();
}
@Override
default Paging<T> findAll(PageRequest<T> pageRequest) {
Example example = genExample(pageRequest.getData());
Page<T> all = getBaseRepository().findAll(example, PageBuilder.toPageable(pageRequest));
return (Paging<T>) PageBuilder.toPaging(all);
}
/**
*
*/
@Override
default List<T> findAllByCondition(T data) {
Example example = genExample(data);
List all = getBaseRepository().findAll(example);
return all;
}
/**
*
*/
@Override
default T findOneByCondition(T data) {
Example example = genExample(data);
Optional one = getBaseRepository().findOne(example);
return (T) one.orElse(null);
}
default Example genExample(T data) {
return Example.of(data);
}
}

View File

@ -11,16 +11,19 @@ package cc.iotkit.data.service;
import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.data.dao.IJPACommData;
import cc.iotkit.data.manager.IDeviceConfigData;
import cc.iotkit.data.dao.DeviceConfigRepository;
import cc.iotkit.data.model.TbDeviceConfig;
import cc.iotkit.common.api.Paging;
import cc.iotkit.model.device.DeviceConfig;
import org.apache.commons.collections.IteratorUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import java.util.Collection;
@ -29,7 +32,7 @@ import java.util.UUID;
@Primary
@Service
public class DeviceConfigDataImpl implements IDeviceConfigData {
public class DeviceConfigDataImpl implements IDeviceConfigData, IJPACommData<DeviceConfig, String> {
@Autowired
private DeviceConfigRepository deviceConfigRepository;
@ -44,14 +47,20 @@ public class DeviceConfigDataImpl implements IDeviceConfigData {
return MapstructUtils.convert(deviceConfigRepository.findByDeviceId(deviceId), DeviceConfig.class);
}
@Override
public JpaRepository getBaseRepository() {
return deviceConfigRepository;
}
@Override
public DeviceConfig findById(String s) {
return MapstructUtils.convert(deviceConfigRepository.findById(s).orElse(null), DeviceConfig.class);
}
@Override
public List<DeviceConfig> findByIds(Collection<String> id) {
return null;
public List<DeviceConfig> findByIds(Collection<String> ids) {
List<TbDeviceConfig> allById = deviceConfigRepository.findAllById(ids);
return MapstructUtils.convert(allById, DeviceConfig.class);
}
@Override
@ -65,7 +74,7 @@ public class DeviceConfigDataImpl implements IDeviceConfigData {
@Override
public void batchSave(List<DeviceConfig> data) {
deviceConfigRepository.saveAll(IteratorUtils.toList(data.iterator()));
}
@ -75,8 +84,8 @@ public class DeviceConfigDataImpl implements IDeviceConfigData {
}
@Override
public void deleteByIds(Collection<String> strings) {
public void deleteByIds(Collection<String> ids) {
deviceConfigRepository.deleteAllById(ids);
}
@ -90,20 +99,8 @@ public class DeviceConfigDataImpl implements IDeviceConfigData {
return MapstructUtils.convert(deviceConfigRepository.findAll(), DeviceConfig.class);
}
@Override
public Paging<DeviceConfig> findAll(PageRequest<DeviceConfig> pageRequest) {
return null;
}
@Override
public List<DeviceConfig> findAllByCondition(DeviceConfig data) {
return null;
}
@Override
public DeviceConfig findOneByCondition(DeviceConfig data) {
return null;
}
}

View File

@ -2,6 +2,7 @@ package cc.iotkit.data.service;
import cc.iotkit.common.api.PageRequest;
import cc.iotkit.common.utils.MapstructUtils;
import cc.iotkit.data.dao.IJPACommData;
import cc.iotkit.data.manager.IHomeData;
import cc.iotkit.data.dao.HomeRepository;
import cc.iotkit.data.model.TbHome;
@ -10,6 +11,7 @@ import cc.iotkit.model.space.Home;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import java.util.Collection;
@ -18,11 +20,17 @@ import java.util.UUID;
@Primary
@Service
public class HomeDataImpl implements IHomeData {
public class HomeDataImpl implements IHomeData, IJPACommData<Home, String> {
@Autowired
private HomeRepository homeRepository;
@Override
public JpaRepository getBaseRepository() {
return homeRepository;
}
@Override
public Home findByUidAndCurrent(String uid, boolean current) {
return MapstructUtils.convert(homeRepository.findByUidAndCurrent(uid, current), Home.class);
@ -53,10 +61,6 @@ public class HomeDataImpl implements IHomeData {
return MapstructUtils.convert(homeRepository.findById(s).orElse(null), Home.class);
}
@Override
public List<Home> findByIds(Collection<String> id) {
return null;
}
@Override
public Home save(Home data) {

View File

@ -68,4 +68,8 @@ public class PageBuilder {
return new Paging<>(all.getTotalElements(),
MapstructUtils.convert(all.getContent(), clz));
}
public static Paging<SysConfig> toPaging(Page all) {
return new Paging<>(all.getTotalElements(), all.getContent());
}
}