Mybatis-plus中Service和Mapper

Mybatis-plus中Service和Mapper,第1张

熟悉 mybatis-plus 的人都知道,mybatis-plus 提供两种包含预定义增删改查 *** 作的接口:

com.baomidou.mybatisplus.core.mapper.BaseMappercom.baomidou.mybatisplus.extension.service.IService

Mybatis-plus提供了2个接口1个类:

BaseMapper 接口针对dao层的方法封装 CRUDIService 接口针对业务逻辑层的封装 需要指定Dao层类和对应的实体类 是在BaseMapper基础上的加强ServiceImpl, T> 类针对业务逻辑层的实现

两者提供的方法略有不同:
对比这两个接口, *** 作都差不多,名字有一点点改变,比如 BaseMapper 里面叫 insert() 的方法,在 IService 里面叫 save()

再有就是 IService 提供批处理 *** 作,BaseMapper 没有。

除此之外还有就是 IService 依赖于 Spring 容器,而 BaseMapper 不依赖。

所以,如果你既要使用批处理 *** 作,又要添加自己的数据库 *** 作,那就必须两个接口一起用。

下面让我们看一下继承结构:

🔔重要: 既然ServiceImpl类也实现了IService接口,那么如果我的UserServiceImpl直接继承ServiceImpl类不就行了吗?为何还要自定义一个继承了IService接口的IUserService接口?这是因为Spring自动注入要求是以接口为标准,在Controller里注入的Service要是一个接口才符合Spring的规范(当然你注入类也行)!

再来看看ServiceImpl类的源代码:

/**
 * IService 实现类( 泛型:M 是 mapper 对象,T 是实体 )
 *
 * @author hubin
 * @since 2018-06-23
 */
@SuppressWarnings("unchecked")
public class ServiceImpl<M extends BaseMapper<T>, T> implements IService<T> {

    protected Log log = LogFactory.getLog(getClass());

    @Autowired
    protected M baseMapper;

    @Override
    public M getBaseMapper() {
        return baseMapper;
    }

    protected Class<T> entityClass = currentModelClass();

    @Override
    public Class<T> getEntityClass() {
        return entityClass;
    }

    protected Class<M> mapperClass = currentMapperClass();
    
  ......
}

里面依赖Spring容器给自动注入了baseMapper

下面我们对比一下BaseMapperIService 两个接口的方法。

总结:

BaseMapper 针对dao层的方法封装 CRUD

IService 针对业务逻辑层的封装,需要指定Dao层类和对应的实体类,是在BaseMapper基础上的加强

ServiceImpl 针对业务逻辑层的实现

一般典型的使用例子中,自己的${Entity}ServiceImpl类继承自ServiceImpl类,并实例化BaseMapper的子类${Entity}Mapper和持久化${Entity}类,实现自定义的$I{Entity}Service接口(继承IService接口),在${Entity}ServiceImpl类实现CRUD的增删改查功能,并重写在I${Entity}Service接口定义的方法

例如:

public interface IMyEntityService extends IService<MyEntity> {
  public List<MyEntity> selectAllList();
  public Future<Boolean> updateBaseInfo(MyEntity cgr);
}  

@Service
public class MyEntityImpl extends ServiceImpl<MyEntityMapper,MyEntity> implements MyEntityService {
 
    @Autowired
    private GetResponse getResponse;
 
    @Override
    public List<MyEntity> selectAllList() {
        return this.baseMapper.selectAllList();
    }
 
    @Override
    public Future<Boolean> updateBaseInfo(MyEntity 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);
        }
 
    }
}

自定义的IMyEntityService接口里面可以自定义一些针对自己业务封装的一些方法


<<<<<<<<<<<< ⌛ >>>>>>>>>>>>

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/2990424.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-23
下一篇 2022-09-23

发表评论

登录后才能评论

评论列表(0条)

保存