SpringBoot Practice (5): BaseMapper, Iservice and ServiceImpl in mybatis-plus

Mybatis-plus includes BaseMapper, Iservice two interfaces and ServiceImpl class. BaseMapper encapsulates CRUD for the dao layer method, and IService encapsulates the business logic layer. It is necessary to specify the Dao layer class and the corresponding entity class, which is an enhancement on the basis of BaseMapper , ServiceImpl is aimed at the realization of the business logic layer, and you can understand how to use it if you understand the relationship between the three;

 In a typical typical usage example, the XxImpl class inherits from the ServiceImpl class, and instantiates the subclass XxMapper and persistent bean class of BaseMapper, implements a custom XxService interface (inheriting the IService interface), and implements the CRUD addition, deletion, modification, and checking function in the XxImpl class , And rewrite the method defined in the XxService interface;

@Slf4j
@Service
@DS("ads")
public class TbAdsZtCgrxxDayImpl extends ServiceImpl<TbAdsZtCgrxxDayMapper,TbAdsZtCgrxxDay> implements TbAdsZtCgrxxDayService {

    @Autowired
    private GetResponse getResponse;

    @Override
    public List<TbAdsZtCgrxxDay> selectAllList() {
        return this.baseMapper.selectAllList();
    }

    @Override
    public Future<Boolean> updateBaseInfo(TbAdsZtCgrxxDay cgr) {
       String ztmc=cgr.getZzmc();
        log.info("当前正在处理的采购人是|{}",ztmc );
        try{
            String legalname =getResponse.getBaseInfo(ztmc).get(0).getData().get(0).getLegalName();
            String socialcode= getResponse.getBaseInfo(ztmc).get(0).getData().get(0).getSocialCode();
            cgr.setFrmc(legalname);
            cgr.setXydm(socialcode);
        }catch (Exception ex){
            log.error("法人或者信用代码有空{}",ex.getMessage());
        }finally {
            log.info("任务进行中,线程池剩余任务数量为|{}", CustomMultiThreadingConfig.executor.getThreadPoolExecutor().getQueue().size());
            int result = this.baseMapper.updateBaseInfo(cgr);
            return AsyncResult.forValue(result>0);
        }

    }
}

The relationship is as follows:

The ServiceImpl class holds an instance of the BaseMapper interface and implements the Iservice interface, so the XxImpl class (where the main business logic is implemented) can specify the sub-interface of BaseMapper for instantiation when inheriting the ServiceImpl class (that is, TdAdsZtCgrxxDayMapper in the example) , And TbAdsZtCgrxxDayService is a sub-interface of the Iservice interface, and the XxImpl class implements several business logic functions defined by the interface TdAdsZtCgrxxDaySerivice;

Guess you like

Origin blog.csdn.net/yezonggang/article/details/110954058