BeanUtils.copyProperties的用法
BeanUtils.copyProperties(source,target)有两个参数,用于将事件源的数据拷贝到目标源中。
如果两个对象之间存在名称不相同的属性,则 BeanUtils 不对这些属性进行处理,需要程序手动处理。
但是BeanUtils有两个,一个是spring的,一个是apache的commons下的。用法一样,但是两者的区别在于:一个是将前者拷贝给后者,另一个是将后者拷贝给前面的。
For Example:
public Boolean insert(AppealUserAddRequest addRequest) {
LambdaQueryWrapper<AppealUserEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(AppealUserEntity::getEmail, addRequest.getEmail());
List<AppealUserEntity> list = this.list(lambdaQueryWrapper);
if (CollectionUtils.isNotEmpty(list)) {
throw new BusinessException("邮箱重复,无法添加");
}
AppealUserEntity appealUserEntity = new AppealUserEntity();
BeanUtils.copyProperties(addRequest, appealUserEntity);
appealUserEntity.setUserCode(UUIDUtil.getGeneratorUUID());
return this.save(appealUserEntity);
}
this.baseMapper用法
@Service
public class AppealUserServiceImpl extends ServiceImpl<AppealUserMapper, AppealUserEntity> implements AppealUserService {
@Override
public Boolean update(AppealUserUpdateRequest updateRequest) {
LambdaQueryWrapper<AppealUserEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(AppealUserEntity::getEmail, updateRequest.getEmail());
lambdaQueryWrapper.select(AppealUserEntity::getTrnKey, AppealUserEntity::getEmail);//设置查询字段
List<AppealUserEntity> list = this.list(lambdaQueryWrapper);
//this.baseMapper.selectList(lambdaQueryWrapper);
//this.getBaseMapper().selectList(lambdaQueryWrapper);
if (CollectionUtils.isNotEmpty(list)) {
list.forEach(entity ->{
if (!entity.getTrnKey().equals(updateRequest.getTrnKey()) && !AppealRoleEnum.DM.getRoleCode().equals(updateRequest.getRoleCode())) {
throw new BusinessException("邮箱重复,无法添加");
}
});
}
AppealUserEntity old = this.getBaseMapper().selectById(updateRequest.getTrnKey());
old.setEmail(updateRequest.getEmail());
old.setDept(updateRequest.getDept());
old.setUserName(updateRequest.getUserName());
old.setStaffCode(updateRequest.getStaffCode());
old.setPositionCode(updateRequest.getPositionCode());
return this.updateById(old);
}
}
.builder用法
public PageResult<AppealDetailsResponse> selectPage(AppealDetailsPageRequest appealDetailsPageRequest) {
Page<AppealDetailsListPojo> page = new Page<>(appealDetailsPageRequest.getCurrentPageIndex(), appealDetailsPageRequest.getPageSize());
Page<AppealDetailsResponse> pageResult = this.baseMapper.selectByPage(page, appealDetailsPageRequest);
return PageResult.<AppealDetailsResponse>builder()
.data(pageResult.getRecords())
.totals(pageResult.getTotal())
.currentPageIndex(pageResult.getCurrent())
.pageSize(pageResult.getSize())
.build();
}
builder类:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.etl.commons.page;
import java.util.List;
public class PageResult<T> {
private List<T> data;
private long totals;
private long currentPageIndex;
private long pageSize;
public static <T> PageResultBuilder<T> builder() {
return new PageResultBuilder();
}
public List<T> getData() {
return this.data;
}
public long getTotals() {
return this.totals;
}
public long getCurrentPageIndex() {
return this.currentPageIndex;
}
public long getPageSize() {
return this.pageSize;
}
public void setData(final List<T> data) {
this.data = data;
}
public void setTotals(final long totals) {
this.totals = totals;
}
public void setCurrentPageIndex(final long currentPageIndex) {
this.currentPageIndex = currentPageIndex;
}
public void setPageSize(final long pageSize) {
this.pageSize = pageSize;
}
public boolean equals(final Object o) {
if (o == this) {
return true;
} else if (!(o instanceof PageResult)) {
return false;
} else {
PageResult<?> other = (PageResult)o;
if (!other.canEqual(this)) {
return false;
} else if (this.getTotals() != other.getTotals()) {
return false;
} else if (this.getCurrentPageIndex() != other.getCurrentPageIndex()) {
return false;
} else if (this.getPageSize() != other.getPageSize()) {
return false;
} else {
Object this$data = this.getData();
Object other$data = other.getData();
if (this$data == null) {
if (other$data != null) {
return false;
}
} else if (!this$data.equals(other$data)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(final Object other) {
return other instanceof PageResult;
}
public int hashCode() {
int PRIME = true;
int result = 1;
long $totals = this.getTotals();
result = result * 59 + (int)($totals >>> 32 ^ $totals);
long $currentPageIndex = this.getCurrentPageIndex();
result = result * 59 + (int)($currentPageIndex >>> 32 ^ $currentPageIndex);
long $pageSize = this.getPageSize();
result = result * 59 + (int)($pageSize >>> 32 ^ $pageSize);
Object $data = this.getData();
result = result * 59 + ($data == null ? 43 : $data.hashCode());
return result;
}
public String toString() {
return "PageResult(data=" + this.getData() + ", totals=" + this.getTotals() + ", currentPageIndex=" + this.getCurrentPageIndex() + ", pageSize=" + this.getPageSize() + ")";
}
public PageResult(final List<T> data, final long totals, final long currentPageIndex, final long pageSize) {
this.data = data;
this.totals = totals;
this.currentPageIndex = currentPageIndex;
this.pageSize = pageSize;
}
public static class PageResultBuilder<T> {
private List<T> data;
private long totals;
private long currentPageIndex;
private long pageSize;
PageResultBuilder() {
}
public PageResultBuilder<T> data(final List<T> data) {
this.data = data;
return this;
}
public PageResultBuilder<T> totals(final long totals) {
this.totals = totals;
return this;
}
public PageResultBuilder<T> currentPageIndex(final long currentPageIndex) {
this.currentPageIndex = currentPageIndex;
return this;
}
public PageResultBuilder<T> pageSize(final long pageSize) {
this.pageSize = pageSize;
return this;
}
public PageResult<T> build() {
return new PageResult(this.data, this.totals, this.currentPageIndex, this.pageSize);
}
public String toString() {
return "PageResult.PageResultBuilder(data=" + this.data + ", totals=" + this.totals + ", currentPageIndex=" + this.currentPageIndex + ", pageSize=" + this.pageSize + ")";
}
}
}
Dozer工具类:
在该项目中用于做数据对象映射
一个映射的框架在一个分层的体系架构中非常有用,特别是你在创建一个抽象的分层去包装一些特殊数据的变化 vs 这些数据传输到其它层(外部服务的数据对象、领域的数据对象、数据传输对象、内部服务数据对象)。因此一个映射框架非常适合于使用在映射器类型的类中,负责将数据从一个数据对象映射到另一个数据对象
使用:
private List<AppealUserPageResponse> convertAppealUser(List<AppealUserEntity> entities) {
return entities.stream().map(entityClass -> {
AppealUserPageResponse convert = Dozer.convert(entityClass, AppealUserPageResponse.class);
convert.setRoleName(AppealRoleEnum.getRoleNameByCode(convert.getRoleCode()));
return convert;
}).collect(Collectors.toList());
}
封装:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.etl.commons.dozer;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
@Component
public class Dozer {
private static Mapper dozerMapper;
@Autowired
private Mapper mapper;
public Dozer() {
}
public Mapper getMapper() {
return this.mapper;
}
@PostConstruct
private void construct() {
setDozerMapper(this.mapper);
}
private static void setDozerMapper(Mapper dozerMapper) {
Dozer.dozerMapper = dozerMapper;
}
@NonNull
public static <T> T convert(@NonNull Object source, @NonNull Class<T> clazz) {
return dozerMapper.map(source, clazz);
}
@Nullable
public static <T> List<T> convert(@Nullable List<?> source, @NonNull Class<T> clazz) {
return (List)((List)Optional.ofNullable(source).orElse(Collections.emptyList())).stream().map((bean) -> {
return dozerMapper.map(bean, clazz);
}).collect(Collectors.toList());
}
}
参考连接:@PostConstruct的作用,以及加载先后顺序
@Transactional事务回滚
spring的@Transactional注解可以很方便的开启事务,但是默认只在遇到运行时异常和Error时才会回滚
提示
@Transactional注解只能应用到public可见度的方法上,可以被应用于接口定义和接口方法,方法会覆盖类上面声明的事务。
@Transactional(rollbackFor = Exception.class)
@Override
public void insert(String year, List<WindowPeriodRequest> windowPeriodRequest) {
if (StringUtils.isNotBlank(year)) {
LambdaQueryWrapper<WindowPeriodEntity> queryWrapper = new LambdaQueryWrapper<WindowPeriodEntity>()
.eq(WindowPeriodEntity::getYear, year);
windowPeriodMapper.delete(queryWrapper);
}
List<WindowPeriodEntity> entities = Dozer.convert(windowPeriodRequest, WindowPeriodEntity.class);
entities.forEach(e -> e.setYear(e.getAppealStart().substring(0,4)));
windowPeriodRepository.saveBatch(entities);
}
List 转成 String 类型数组
使用List的toArray(T[] a)方法 ,可以转成任意类型。
@Test
public void listToArray2() {
String[] array2 = list.toArray(new String[list.size()]);
print(array);//打印
}