mercredi 14 avril 2021

There are many repetitive functions in my codes how to simplify them

It's a spring-boot project, using MVC structure

I have 3 types of demands that extend to BaseDemand

public class DropTableDemand extends BaseDemand{}
public class ExpandTableDemand extends BaseDemand{}
public class CreateTableDemand extends BaseDemand{}

And I have 3 services to control them and save them with mybatis-plus DAO

public class DropTableDemandService {

    @Autowired
    private DropTableDemandDao dropTableDemandDao;

    public List<DropTableDemand> queryByOrgIDs(List<String> orgIDs, Integer page, Integer rows) {
        if (page != null && rows != null) {
            PageHelper.startPage(page, rows);
        }
        Weekend<DropTableDemand> weekend = Weekend.of(DropTableDemand.class);
        WeekendCriteria<DropTableDemand, Object> criteria = weekend.weekendCriteria();
        criteria.andIn(DropTableDemand::getTableOrg, orgIDs);
        return dropTableDemandDao.selectByExample(weekend);
    }
}
public class ExpandTableDemandService {

    @Autowired
    private ExpandTableDemandDao expandTableDemandDao;

    public List<ExpandTableDemand> queryByOrgIDs(List<String> orgIDs, Integer page, Integer rows) {
        if (page != null && rows != null) {
            PageHelper.startPage(page, rows);
        }
        Weekend<ExpandTableDemand> weekend = Weekend.of(ExpandTableDemand.class);
        WeekendCriteria<ExpandTableDemand, Object> criteria = weekend.weekendCriteria();
        criteria.andIn(ExpandTableDemand::getTableOrg, orgIDs);
        return expandTableDemandDao.selectByExample(weekend);
    }
}
public class CreateTableDemandService {

    @Autowired
    private CreateTableDemandDao createTableDemandDao;

    public List<CreateTableDemand> queryByOrgIDs(List<String> orgIDs, Integer page, Integer rows) {
        if (page != null && rows != null) {
            PageHelper.startPage(page, rows);
        }
        Weekend<CreateTableDemand> weekend = Weekend.of(CreateTableDemand.class);
        WeekendCriteria<CreateTableDemand, Object> criteria = weekend.weekendCriteria();
        criteria.andIn(CreateTableDemand::getTableOrg, orgIDs);
        return createTableDemandDao.selectByExample(weekend);
    }
}

DAO is like these

public interface CreateTableDemandDao extends MyMapper<CreateTableDemand> {

}
public interface DropTableDemandDao extends MyMapper<DropTableDemand> {

}

There are many repetitive functions in these services but control different class, how can I simplify them

Aucun commentaire:

Enregistrer un commentaire