【Spring】代码抽取思想

【Spring】代码抽取思想_第1张图片

       个人主页:五敷有你      
 系列专栏:Spring

⛺️稳中求进,晒太阳

Q代表查询实体类。

T代表实体类

        在编写Mapper,Service类时,反复书写基础的增删改查,那么有没有方法可以避免呢,答案是有的就是抽取出来,让他们继承。

抽取mapper包下的基础增删改查

1.抽取出来后单独写一个mapper接口

如下(用到了泛型,不懂得可以去看这篇文章Java 中的泛型(两万字超全详解)

//Q代表查询实体 T代表实体类
public interface BaseMapper {



    int deleteByPrimaryKey(Integer aid);

    int insert(T record);

    int insertSelective(T record);

    T selectByPrimaryKey(Integer aid);

    int updateByPrimaryKeySelective(T record);

    int updateByPrimaryKey(T record);


    List selectObjectByCondition(Q mq);

    Integer selectObjectByConditionCount(Q mq);
}

       

2.然后让所有的mapper继承

【Spring】代码抽取思想_第2张图片

        让所有的实体mapper继承它即可

        其他类就只需要继承这个类,即可含有它所有的方法,但是实体mapper的xml文件还是要写,但之前咱们已经逆向工程生成了实现了部分方法。

        如果之后还要添加公共的方法,就需要去自己在xml文件中写SQL了。


public interface AlbumMapper extends BaseMapper{


    Integer selectByAname(Map map);

    List selectAll();
}

OK了哦,到此mapper包的操作代码抽取完成。

抽取Service包下的基础增删改查

这个相比于mapper包下,大体思想一致,但还是有一部分不同。

        service包下的所有接口都没有任何区别与mapper层一样。

抽取出来让所有的service层接口继承

 抽取ServiceImpl

书写这个ServiceImpl也是为了简化代码,让所有冗余的代码抽取出来。

        因为在Service的impl包下需要调用实体类Mapper,但是不同的包下,实体类的mapper有不一样。有人就想到了泛型,可以吗,可以。学过mybatis-puls的是不是觉得似曾相识,没错它就是这样实现的。

【Spring】代码抽取思想_第3张图片

        由于我需要的泛型太多了,实现没有那么书写,换一种方法。我直接在serviceImpl中加入了书写的直接注入

        如下,当然你也可以直接在BaseMapper头上加上@Autowair但是效率比较低,底层会多次测试才能找到对的。所以直接注入即可。

        用protected是可以保证子类和父类是可以访问的。要用子类给这个父类注入。

package com.qcby.service.impl;

import com.qcby.mapper.BaseMapper;
import com.qcby.pojo.Songer;
import com.qcby.service.BaseService;
import com.qcby.util.Page;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class BaseServiceImpl implements BaseService {

    protected BaseMapper baseMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
       return baseMapper.deleteByPrimaryKey(id);

    }

    @Override
    public int insert(T record) {
        return baseMapper.insert(record);

    }

    @Override
    public int insertSelective(T record) {
       return 0;
    }

    @Override
    public T selectByPrimaryKey(Integer tid) {
       return  (T)baseMapper.selectByPrimaryKey(tid);


    }

    @Override
    public int updateByPrimaryKeySelective(T record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(T record) {
      int n=  baseMapper.updateByPrimaryKey(record);


        return n;
    }

 
}

其他impl包下的实现类继承

package com.qcby.service.impl;

import com.qcby.mapper.AlbumMapper;
import com.qcby.pojo.Songer;
import com.qcby.query.AlbumQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qcby.mapper.AlbumMapper;
import com.qcby.pojo.Album;
import com.qcby.service.AlbumService;

import java.awt.*;
import java.util.List;
import java.util.Map;

@Service
public class AlbumServiceImpl extends BaseServiceImpl implements AlbumService {

   private AlbumMapper albumMapper;

    //注入
   @Autowired
   public void setAlbumMapper(AlbumMapper albumMapper) {
      this.albumMapper = albumMapper;
      this.baseMapper=albumMapper;
   }




}

 在@Autowair放在albumMapper与放在setAlbumMapper是没有区别的。,但因为要给父类注入,所以还是要写在方法里面。

这样整体的架构就搭建好了。

你可能感兴趣的:(spring,spring,java,后端,开发语言)