Skip to content

Commit

Permalink
feat($starter): remove the usage of BeanUtil
Browse files Browse the repository at this point in the history
New coding rules:
1. The class annotated with `@org.mapstruct.Mapper`
   should be named with the postfix `-MapStructMapper`.
   For example, QuartzJobConfigurationMapStructMapper.java
2. MyBatis mapper classes should be under package `repository`

BREAKING CHANGE: ban using any packages of BeanUtil; the package name of MyBatis mappers has changed to `repository`
  • Loading branch information
johnnymillergh committed Feb 5, 2022
1 parent 6e5121a commit 3e2872e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@Import({
DataSourceConfiguration.class
})
@MapperScan("com.jmsoftware.maf.springcloudstarter.*.mapper")
@MapperScan("com.jmsoftware.maf.springcloudstarter.*.repository")
@ConditionalOnClass({MybatisPlusAutoConfiguration.class, MasterSlaveAutoRoutingPlugin.class})
public class MyBatisPlusConfiguration {
@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright By ZATI
* Copyright By 3a3c88295d37870dfd3b25056092d1a9209824b256c341f2cdc296437f671617
* All rights reserved.
*
* If you are not the intended user, you are hereby notified that any use, disclosure, copying, printing, forwarding or
* dissemination of this property is strictly prohibited. If you have got this file in error, delete it from your
* system.
*/
package com.jmsoftware.maf.springcloudstarter.quartz.converter;

import com.jmsoftware.maf.springcloudstarter.quartz.entity.QuartzJobConfigurationExcel;
import com.jmsoftware.maf.springcloudstarter.quartz.entity.persistence.QuartzJobConfiguration;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

/**
* Description: QuartzJobConfigurationMapStructMapper, change description here.
*
* @author Johnny Miller (鍾俊), e-mail: johnnysviva@outlook.com, date: 2/5/2022 2:44 PM
**/
@Mapper
public interface QuartzJobConfigurationMapStructMapper {
QuartzJobConfigurationMapStructMapper INSTANCE = Mappers.getMapper(QuartzJobConfigurationMapStructMapper.class);

/**
* Convert from quartz job configuration.
*
* @param quartzJobConfigurationExcel the quartz job configuration excel
* @return the quartz job configuration
*/
@Mapping(target = "id", ignore = true)
@Mapping(target = "deleted", ignore = true)
QuartzJobConfiguration of(QuartzJobConfigurationExcel quartzJobConfigurationExcel);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,19 @@
*/
@Slf4j
abstract class AbstractQuartzJob extends QuartzJobBean {
private static final String QUARTZ_JOB_CONFIGURATION_CLASS = QuartzJobConfiguration.class.getSimpleName();

@Override
protected final void executeInternal(JobExecutionContext context) {
val sourceQuartzJobConfiguration = context.getMergedJobDataMap().get(QUARTZ_JOB_CONFIGURATION);
if (!(QUARTZ_JOB_CONFIGURATION_CLASS.equals(sourceQuartzJobConfiguration.getClass().getSimpleName()))) {
if (!(sourceQuartzJobConfiguration instanceof QuartzJobConfiguration)) {
throw new IllegalArgumentException(
"Invalid job data! Not the instance of QuartzJobConfiguration. Runtime actual class: "
+ sourceQuartzJobConfiguration.getClass());
}
val quartzJobConfiguration = new QuartzJobConfiguration();
BeanUtil.copyProperties(sourceQuartzJobConfiguration, quartzJobConfiguration);
if (log.isDebugEnabled()) {
log.debug("Found and QuartzJobConfiguration from job data map: {}", sourceQuartzJobConfiguration);
}
try {
this.invoke(context, quartzJobConfiguration);
this.invoke(context, (QuartzJobConfiguration) sourceQuartzJobConfiguration);
} catch (Exception e) {
log.error("Exception occurred when invoking method", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.jmsoftware.maf.springcloudstarter.quartz.mapper;
package com.jmsoftware.maf.springcloudstarter.quartz.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
Expand All @@ -8,15 +8,17 @@
import com.jmsoftware.maf.springcloudstarter.quartz.entity.persistence.QuartzJobConfiguration;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* Description: QuartzJobConfigurationMapper
* Description: QuartzJobConfigurationMapStructMapper
*
* @author Johnny Miller (锺俊), email: johnnysviva@outlook.com, date: 9/23/2021 8:23 AM
*/
@Mapper
@Repository
public interface QuartzJobConfigurationMapper extends BaseMapper<QuartzJobConfiguration> {
/**
* Select page list page.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.jmsoftware.maf.springcloudstarter.quartz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.extra.validation.ValidationUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jmsoftware.maf.common.bean.PageResponseBodyBean;
import com.jmsoftware.maf.springcloudstarter.property.MafProjectProperties;
import com.jmsoftware.maf.springcloudstarter.quartz.converter.QuartzJobConfigurationMapStructMapper;
import com.jmsoftware.maf.springcloudstarter.quartz.entity.CreateOrModifyQuartzJobConfigurationPayload;
import com.jmsoftware.maf.springcloudstarter.quartz.entity.GetQuartzJobConfigurationPageListItem;
import com.jmsoftware.maf.springcloudstarter.quartz.entity.GetQuartzJobConfigurationPageListPayload;
import com.jmsoftware.maf.springcloudstarter.quartz.entity.QuartzJobConfigurationExcel;
import com.jmsoftware.maf.springcloudstarter.quartz.entity.persistence.QuartzJobConfiguration;
import com.jmsoftware.maf.springcloudstarter.quartz.mapper.QuartzJobConfigurationMapper;
import com.jmsoftware.maf.springcloudstarter.quartz.repository.QuartzJobConfigurationMapper;
import com.jmsoftware.maf.springcloudstarter.quartz.service.QuartzJobConfigurationService;
import com.jmsoftware.maf.springcloudstarter.quartz.util.CronUtil;
import com.jmsoftware.maf.springcloudstarter.quartz.util.ScheduleUtil;
Expand Down Expand Up @@ -102,11 +102,9 @@ public void validateBeforeAddToBeanList(List<QuartzJobConfigurationExcel> beanLi
@SneakyThrows
@Transactional(rollbackFor = Throwable.class)
public void save(@NotEmpty List<@Valid QuartzJobConfigurationExcel> beanList) {
val quartzJobConfigurationList = beanList.stream().map(quartzJobConfigurationExcel -> {
val quartzJobConfiguration = new QuartzJobConfiguration();
BeanUtil.copyProperties(quartzJobConfigurationExcel, quartzJobConfiguration);
return quartzJobConfiguration;
}).collect(Collectors.toList());
val quartzJobConfigurationList = beanList.stream()
.map(QuartzJobConfigurationMapStructMapper.INSTANCE::of)
.collect(Collectors.toList());
requireTrue(
this.saveBatch(quartzJobConfigurationList),
saved -> log.info("Saved quartzJobConfigurationList, saved: {}", saved)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jmsoftware.maf.springcloudstarter.quartz.mapper.QuartzJobConfigurationMapper">
<mapper namespace="com.jmsoftware.maf.springcloudstarter.quartz.repository.QuartzJobConfigurationMapper">
<resultMap id="BaseResultMap"
type="com.jmsoftware.maf.springcloudstarter.quartz.entity.persistence.QuartzJobConfiguration">
<!--@mbg.generated-->
Expand Down

0 comments on commit 3e2872e

Please sign in to comment.