fix : replace method for convert bean(#725)

pull/762/head
pizihao 3 years ago
parent 700720bf3a
commit c6ad9b35a8

@ -44,7 +44,7 @@ public class PermissionServiceImpl implements PermissionService {
public IPage<PermissionRespDTO> listPermission(int pageNo, int pageSize) { public IPage<PermissionRespDTO> listPermission(int pageNo, int pageSize) {
PermissionQueryPageReqDTO queryPage = new PermissionQueryPageReqDTO(pageNo, pageSize); PermissionQueryPageReqDTO queryPage = new PermissionQueryPageReqDTO(pageNo, pageSize);
IPage<PermissionInfo> selectPage = permissionMapper.selectPage(queryPage, null); IPage<PermissionInfo> selectPage = permissionMapper.selectPage(queryPage, null);
return selectPage.convert(each -> BeanUtil.copyProperties(each, new PermissionRespDTO())); return selectPage.convert(each -> BeanUtil.convert(each, PermissionRespDTO.class));
} }
@Override @Override

@ -51,7 +51,7 @@ public class RoleServiceImpl implements RoleService {
public IPage<RoleRespDTO> listRole(int pageNo, int pageSize) { public IPage<RoleRespDTO> listRole(int pageNo, int pageSize) {
RoleQueryPageReqDTO queryPage = new RoleQueryPageReqDTO(pageNo, pageSize); RoleQueryPageReqDTO queryPage = new RoleQueryPageReqDTO(pageNo, pageSize);
IPage<RoleInfo> selectPage = roleMapper.selectPage(queryPage, null); IPage<RoleInfo> selectPage = roleMapper.selectPage(queryPage, null);
return selectPage.convert(each -> BeanUtil.copyProperties(each, new RoleRespDTO())); return selectPage.convert(each -> BeanUtil.convert(each, RoleRespDTO.class));
} }
@Override @Override

@ -58,7 +58,7 @@ public class UserServiceImpl implements UserService {
LambdaQueryWrapper<UserInfo> queryWrapper = Wrappers.lambdaQuery(UserInfo.class) LambdaQueryWrapper<UserInfo> queryWrapper = Wrappers.lambdaQuery(UserInfo.class)
.eq(StringUtil.isNotBlank(reqDTO.getUserName()), UserInfo::getUserName, reqDTO.getUserName()); .eq(StringUtil.isNotBlank(reqDTO.getUserName()), UserInfo::getUserName, reqDTO.getUserName());
IPage<UserInfo> selectPage = userMapper.selectPage(reqDTO, queryWrapper); IPage<UserInfo> selectPage = userMapper.selectPage(reqDTO, queryWrapper);
return selectPage.convert(each -> BeanUtil.copyProperties(each, new UserRespDTO())); return selectPage.convert(each -> BeanUtil.convert(each, UserRespDTO.class));
} }
@Override @Override
@ -70,7 +70,7 @@ public class UserServiceImpl implements UserService {
throw new RuntimeException("用户名重复"); throw new RuntimeException("用户名重复");
} }
reqDTO.setPassword(bCryptPasswordEncoder.encode(reqDTO.getPassword())); reqDTO.setPassword(bCryptPasswordEncoder.encode(reqDTO.getPassword()));
UserInfo insertUser = BeanUtil.copyProperties(reqDTO, new UserInfo()); UserInfo insertUser = BeanUtil.convert(reqDTO, UserInfo.class);
userMapper.insert(insertUser); userMapper.insert(insertUser);
} }
@ -79,7 +79,7 @@ public class UserServiceImpl implements UserService {
if (StringUtil.isNotBlank(reqDTO.getPassword())) { if (StringUtil.isNotBlank(reqDTO.getPassword())) {
reqDTO.setPassword(bCryptPasswordEncoder.encode(reqDTO.getPassword())); reqDTO.setPassword(bCryptPasswordEncoder.encode(reqDTO.getPassword()));
} }
UserInfo updateUser = BeanUtil.copyProperties(reqDTO, new UserInfo()); UserInfo updateUser = BeanUtil.convert(reqDTO, UserInfo.class);
LambdaUpdateWrapper<UserInfo> updateWrapper = Wrappers.lambdaUpdate(UserInfo.class) LambdaUpdateWrapper<UserInfo> updateWrapper = Wrappers.lambdaUpdate(UserInfo.class)
.eq(UserInfo::getUserName, reqDTO.getUserName()); .eq(UserInfo::getUserName, reqDTO.getUserName());
userMapper.update(updateUser, updateWrapper); userMapper.update(updateUser, updateWrapper);
@ -107,7 +107,7 @@ public class UserServiceImpl implements UserService {
Wrapper queryWrapper = Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getUserName, reqDTO.getUserName()); Wrapper queryWrapper = Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getUserName, reqDTO.getUserName());
UserInfo userInfo = userMapper.selectOne(queryWrapper); UserInfo userInfo = userMapper.selectOne(queryWrapper);
UserRespDTO respUser = Optional.ofNullable(userInfo) UserRespDTO respUser = Optional.ofNullable(userInfo)
.map(each -> BeanUtil.copyProperties(each, new UserRespDTO())) .map(each -> BeanUtil.convert(each, UserRespDTO.class))
.orElseThrow(() -> new ServiceException("查询无此用户, 可以尝试清空缓存或退出登录.")); .orElseThrow(() -> new ServiceException("查询无此用户, 可以尝试清空缓存或退出登录."));
return respUser; return respUser;
} }

@ -47,12 +47,17 @@ public class BeanUtil {
.orElse(null); .orElse(null);
} }
public static <T, S> T convert(S source, T target) {
Optional.ofNullable(source)
.ifPresent(each -> BEAN_MAPPER_BUILDER.map(each, target));
return target;
}
public static <T, S> List<T> convert(List<S> sources, Class<T> clazz) { public static <T, S> List<T> convert(List<S> sources, Class<T> clazz) {
return Optional.ofNullable(sources) return Optional.ofNullable(sources)
.map(each -> { .map(each -> {
List<T> targetList = new ArrayList<T>(each.size()); List<T> targetList = new ArrayList<T>(each.size());
each.stream() each.forEach(item -> targetList.add(BEAN_MAPPER_BUILDER.map(item, clazz)));
.forEach(item -> targetList.add(BEAN_MAPPER_BUILDER.map(item, clazz)));
return targetList; return targetList;
}) })
.orElse(null); .orElse(null);
@ -62,41 +67,12 @@ public class BeanUtil {
return Optional.ofNullable(sources) return Optional.ofNullable(sources)
.map(each -> { .map(each -> {
Set<T> targetSize = new HashSet<T>(each.size()); Set<T> targetSize = new HashSet<T>(each.size());
each.stream() each.forEach(item -> targetSize.add(BEAN_MAPPER_BUILDER.map(item, clazz)));
.forEach(item -> targetSize.add(BEAN_MAPPER_BUILDER.map(item, clazz)));
return targetSize; return targetSize;
}) })
.orElse(null); .orElse(null);
} }
/**
* copyProperties
*
* @param source source obj
* @param target target obj
* @param <T> target type
* @param <S> source type
* @return T
*/
public static <T, S> T copyProperties(S source, T target) {
return copyProperties(source, target, (String) null);
}
/**
* copyProperties
*
* @param source source obj
* @param target target obj
* @param ignoreProperties ignore name
* @param <T> target type
* @param <S> source type
* @return T
*/
public static <T, S> T copyProperties(S source, T target, String... ignoreProperties) {
BeanUtils.copyProperties(source, target, ignoreProperties);
return target;
}
public static <T> T mapToBean(Map<String, Object> map, Class<T> clazz, boolean toCamelCase) { public static <T> T mapToBean(Map<String, Object> map, Class<T> clazz, boolean toCamelCase) {
if (clazz == null) { if (clazz == null) {
return null; return null;
@ -127,7 +103,7 @@ public class BeanUtil {
} }
Class<?> clazz = bean.getClass(); Class<?> clazz = bean.getClass();
map.forEach((s, o) -> { map.forEach((s, o) -> {
String name = toCamelCase ? StringUtil.toUnderlineCase(s) : s; String name = toCamelCase ? StringUtil.toCamelCase(s, StringUtil.UNDERLINE) : s;
Method method = setter(clazz, name); Method method = setter(clazz, name);
if (method != null) { if (method != null) {
ReflectUtil.invoke(bean, method, o); ReflectUtil.invoke(bean, method, o);

@ -195,6 +195,34 @@ public class StringUtil {
return sb.toString(); return sb.toString();
} }
/**
* to camel case
*
* @param str CharSequence
* @param symbol symbol
* @return toCamelCase String
*/
public static String toCamelCase(CharSequence str, char symbol) {
if (null == str || str.length() == 0) {
return null;
}
int length = str.length();
StringBuilder sb = new StringBuilder(length);
boolean upperCase = false;
for (int i = 0; i < length; ++i) {
char c = str.charAt(i);
if (c == symbol) {
upperCase = true;
} else if (upperCase) {
sb.append(Character.toUpperCase(c));
upperCase = false;
} else {
sb.append(c);
}
}
return sb.toString();
}
/** /**
* Replace a portion of the string, replacing all found * Replace a portion of the string, replacing all found
* *

@ -104,13 +104,14 @@ public class BeanUtilTest {
// ----------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------------------
@Test
public void testMapToBean() { public void testMapToBean() {
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("name", "Test"); map.put("name", "Test");
map.put("status", 12); map.put("status_code", 12);
Customer customer = BeanUtil.mapToBean(map, Customer.class, true); Customer customer = BeanUtil.mapToBean(map, Customer.class, true);
Assert.assertEquals("Test", customer.getName()); Assert.assertEquals("Test", customer.getName());
Assert.assertEquals(Integer.valueOf(12), customer.getStatus()); Assert.assertEquals(Integer.valueOf(12), customer.getStatusCode());
} }
@Test @Test
@ -152,7 +153,7 @@ public class BeanUtilTest {
static class Customer { static class Customer {
String name; String name;
Integer status; Integer statusCode;
} }
@Getter @Getter
@ -160,6 +161,6 @@ public class BeanUtilTest {
static class PreCustomer { static class PreCustomer {
String name; String name;
Integer status; Integer statusCode;
} }
} }

@ -90,4 +90,11 @@ public class StringUtilTest {
String s = StringUtil.toSymbolCase(string, StringUtil.UNDERLINE); String s = StringUtil.toSymbolCase(string, StringUtil.UNDERLINE);
Assert.isTrue(Objects.equals(s, "str")); Assert.isTrue(Objects.equals(s, "str"));
} }
@Test
public void toCamelCase() {
String string = "str_str";
String s = StringUtil.toCamelCase(string, StringUtil.UNDERLINE);
Assert.isTrue(Objects.equals(s, "strStr"));
}
} }

@ -76,7 +76,7 @@ public class ThreadPoolRunStateHandler extends AbstractThreadPoolRuntime {
rejectedName = pool.getRejectedExecutionHandler().getClass().getSimpleName(); rejectedName = pool.getRejectedExecutionHandler().getClass().getSimpleName();
} }
poolRunStateInfo.setRejectedName(rejectedName); poolRunStateInfo.setRejectedName(rejectedName);
ManyThreadPoolRunStateInfo manyThreadPoolRunStateInfo = BeanUtil.copyProperties(poolRunStateInfo, new ManyThreadPoolRunStateInfo()); ManyThreadPoolRunStateInfo manyThreadPoolRunStateInfo = BeanUtil.convert(poolRunStateInfo, ManyThreadPoolRunStateInfo.class);
manyThreadPoolRunStateInfo.setIdentify(CLIENT_IDENTIFICATION_VALUE); manyThreadPoolRunStateInfo.setIdentify(CLIENT_IDENTIFICATION_VALUE);
String active = environment.getProperty("spring.profiles.active", "UNKNOWN"); String active = environment.getProperty("spring.profiles.active", "UNKNOWN");
manyThreadPoolRunStateInfo.setActive(active.toUpperCase()); manyThreadPoolRunStateInfo.setActive(active.toUpperCase());

@ -60,7 +60,7 @@ public class EsMonitorHandler extends AbstractDynamicThreadPoolMonitor {
@Override @Override
protected void execute(ThreadPoolRunStateInfo poolRunStateInfo) { protected void execute(ThreadPoolRunStateInfo poolRunStateInfo) {
EsThreadPoolRunStateInfo esThreadPoolRunStateInfo = BeanUtil.copyProperties(poolRunStateInfo, new EsThreadPoolRunStateInfo()); EsThreadPoolRunStateInfo esThreadPoolRunStateInfo = BeanUtil.convert(poolRunStateInfo, EsThreadPoolRunStateInfo.class);
Environment environment = ApplicationContextHolder.getInstance().getEnvironment(); Environment environment = ApplicationContextHolder.getInstance().getEnvironment();
String indexName = environment.getProperty("es.thread-pool-state.index.name", "thread-pool-state"); String indexName = environment.getProperty("es.thread-pool-state.index.name", "thread-pool-state");
String applicationName = environment.getProperty("spring.application.name", "application"); String applicationName = environment.getProperty("spring.application.name", "application");

@ -54,7 +54,7 @@ public class MicrometerMonitorHandler extends AbstractDynamicThreadPoolMonitor {
if (stateInfo == null) { if (stateInfo == null) {
RUN_STATE_CACHE.put(poolRunStateInfo.getTpId(), poolRunStateInfo); RUN_STATE_CACHE.put(poolRunStateInfo.getTpId(), poolRunStateInfo);
} else { } else {
BeanUtil.copyProperties(poolRunStateInfo, stateInfo); BeanUtil.convert(poolRunStateInfo, stateInfo);
} }
Environment environment = ApplicationContextHolder.getInstance().getEnvironment(); Environment environment = ApplicationContextHolder.getInstance().getEnvironment();
String applicationName = environment.getProperty("spring.application.name", "application"); String applicationName = environment.getProperty("spring.application.name", "application");

@ -64,7 +64,7 @@ public class AdapterExecutorsRefreshListener extends AbstractRefreshListener<Ada
|| !Objects.equals(adapterExecutorProperties.getMaximumPoolSize(), each.getMaximumPoolSize())) { || !Objects.equals(adapterExecutorProperties.getMaximumPoolSize(), each.getMaximumPoolSize())) {
threadPoolAdapterMap.forEach((key, val) -> { threadPoolAdapterMap.forEach((key, val) -> {
if (Objects.equals(val.mark(), each.getMark())) { if (Objects.equals(val.mark(), each.getMark())) {
val.updateThreadPool(BeanUtil.copyProperties(each, new ThreadPoolAdapterParameter())); val.updateThreadPool(BeanUtil.convert(each, ThreadPoolAdapterParameter.class));
DynamicThreadPoolAdapterRegister.ADAPTER_EXECUTORS_MAP.put(buildKey, each); DynamicThreadPoolAdapterRegister.ADAPTER_EXECUTORS_MAP.put(buildKey, each);
} }
}); });

@ -49,7 +49,7 @@ public class RunTimeInfoCollector extends AbstractThreadPoolRuntime implements C
List<String> listThreadPoolId = GlobalThreadPoolManage.listThreadPoolId(); List<String> listThreadPoolId = GlobalThreadPoolManage.listThreadPoolId();
for (String each : listThreadPoolId) { for (String each : listThreadPoolId) {
ThreadPoolRunStateInfo poolRunState = getPoolRunState(each); ThreadPoolRunStateInfo poolRunState = getPoolRunState(each);
RuntimeMessage runtimeMessage = BeanUtil.copyProperties(poolRunState, new RuntimeMessage()); RuntimeMessage runtimeMessage = BeanUtil.convert(poolRunState, RuntimeMessage.class);
runtimeMessage.setGroupKey(getThreadPoolIdentify(each, properties.getItemId(), properties.getNamespace())); runtimeMessage.setGroupKey(getThreadPoolIdentify(each, properties.getItemId(), properties.getNamespace()));
runtimeMessages.add(runtimeMessage); runtimeMessages.add(runtimeMessage);
} }

Loading…
Cancel
Save