從 crud 認識設計模式

從 crud 認識設計模式

  • 在業務系統中增刪改查(crud)是常常須要開發的內容,本文主要從增刪改查來對設計模式進行一個學習或使用.
  • 通常咱們對一個表的增刪改查有以下一些接口定義
boolean insert(Object o);
Object byId(Integer id);
boolean del(Integer id);
boolean editor(Integer interfaces, Object o;
  • 再帶上 redis 的 crud 操做,具體不進行列舉.

總體操做

  • 在這裏咱們僅針對單表而言

C-建立

  • 一般咱們的建立流程是這樣子的.java

    1. 接收請求,將請求轉換爲db對象,持久化
    2. 若是須要製做緩存,則放入緩存中
  • 僞代碼以下
boolean b =  mapper.save(s)
if (b){
  redis.save(s)
}

R-讀取

  • 此處以byId爲例. where 條件不易放入 redis 做爲 key ,若是做爲 key 數據量會過多
  • 一般操做git

    1. 嘗試從 redis 中讀取
    2. 讀取到了則返回,沒有讀取到經過db進行查詢
  • 僞代碼
Object o =null
o = redis.read(id)
if (o ==null) {
  o = mapper.read(id)
}

U-更新

  • 一般操做github

    1. 刪除 redis 的記錄(避免讀到假數據)
    2. 更新db
    3. 放入 redis
  • 僞代碼
redis.del()
mapper.update(o)
redis.save(o)

D-刪除

  • 一般操做redis

    1. redis 刪除
    2. db 刪除
  • 僞代碼
redis.del()
mapper.del(o)

設計改良

  • 上述的操做大部份內容類似度很高, 在平常編寫的時候咱們可能會常常寫這樣子的代碼.代碼量過多, 以及代碼複用率不是那麼好.所以對其作出改良.
  • 抽象數據庫操做和緩存操做. 其操做內容就是 CRUD 幾個基本操做. 再次強調:單表操做
  • 在抽象 crud 基本操做前須要在提出一個問題.sql

    • 咱們的主鍵多是多種類型的. 常見有 int 、 varchar 這兩種, 如何讓咱們的 crud 接口更好的使用?
    • 下面是筆者提供的解決方案.

id 多類型的解決

  • 首先定義一個空接口 IdInterface 它用來標識這是一個 id 接口
public interface IdInterface {

}
  • 根據經常使用 int 和 varchar 設計出以下兩個子接口. 他們的做用會在後續獲得進一步的體現. 當前階段僅須要知道他們是 id 接口的繼承便可.數據庫

    • 方法 id() 用來返回具體的 id 值
public interface IntIdInterface extends IdInterface {
  int id();
}

public interface StrIdInterface extends IdInterface {
  String id();
}

CRUD 基礎接口抽象

  • 首先定義 db 操做接口
public interface DbOperation<T, I extends IdInterface> {

  boolean insert(T t);

  T byId(I interfaces);

  boolean del(I interfaces);

  boolean editor(I interfaces, T t);

}
  • 代碼解釋, 此處使用泛型 T 來標識接受的類型, I 來標識接收的接口, 這裏存在2中狀況. 第一種 I = IntIdInterface , 第二種 I = StrIdInterface
  • 繼續定義 redis 的操做
public interface RedisOperation<T, I extends IdInterface> {

  void insert(T t);

  void update(I i, T t);

  void del(I i);

  T byId(I i);

}
  • redis 緩存的數據結構選擇. 這裏爲了方便使用 id 進行查詢選擇 hash 進行開發. 若有需求可自定義選擇數據結構.
  • redis 操做 hash 的行爲也能夠抽象出來.這裏就不使用接口了. 使用一個父類來進行解決.
public abstract class RedisHashKeyOperation<T> {

  Gson gson = new Gson();
  @Autowired
  private StringRedisTemplate redisTemplate;

  protected void update(String key, String id, T t) {

    T redisObj = this.byId(key, id, t.getClass());
    if (!Objects.isNull(redisObj)) {

      // 若是是redis中的類型和當前傳入的類型相同
      if (redisObj.getClass().equals(t.getClass())) {
        this.insert(key, id, t);
      }
    }
  }

  protected void insert(String key, String id, T t) {
    redisTemplate.opsForHash().put(key, id, gson.toJson(t));
  }

  protected T byId(String key, String id, Class<?> clazz) {
    String o = (String) redisTemplate.opsForHash().get(key, id);
    return (T) gson.fromJson(o, clazz);
  }

  protected void delete(String key, String id) {
    this.redisTemplate.opsForHash().delete(key, id);
  }
}
  • 通常的對 redis hash 操做基本就是這樣子的. 不會有過多的修改.
  • 建立兩個表來使用如下. 實體對象 mapper 不在此進行展開代碼
CREATE TABLE `project_int` (
  `id` int(32) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

CREATE TABLE `project_str` (
  `id` varchar(50) COLLATE utf8mb4_bin NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
  • 貼出一個int爲主鍵的操做
@Service("ProjectIntDbOperationImpl")
public class ProjectIntDbOperationImpl implements DbOperation<ProjectInt, IntIdInterface> {

  @Autowired
  private ProjectIntMapper projectIntMapper;

  @Override
  public boolean insert(ProjectInt projectInt) {
    return projectIntMapper.insert(projectInt) > 0;
  }

  @Override
  public ProjectInt byId(IntIdInterface interfaces) {
    return projectIntMapper.selectByPrimaryKey(interfaces.id());
  }

  @Override
  public boolean del(IntIdInterface interfaces) {
    return projectIntMapper.deleteByPrimaryKey(interfaces.id()) > 0;
  }

  @Override
  public boolean editor(IntIdInterface interfaces, ProjectInt projectInt) {
    // 更新存在策略
    ProjectInt projectInt1 = this.byId(interfaces);
    projectInt1.setName(projectInt.getName());

    return this.projectIntMapper.updateByPrimaryKey(projectInt1) > 0;
  }

}



@Service("ProjectIntRedisOperationImpl")
public class ProjectIntRedisOperationImpl
    extends RedisHashKeyOperation<ProjectInt>
    implements
    RedisOperation<ProjectInt, IntIdInterface> {

  public static final String CACHE_PROJECT_INT = "cache:project_int";

  public void insert(ProjectInt projectInt) {
    super.insert(CACHE_PROJECT_INT, String.valueOf(projectInt.getId()), projectInt);
  }

  public void update(IntIdInterface strIdInterface, ProjectInt projectInt) {
    super.update(CACHE_PROJECT_INT, String.valueOf(strIdInterface.id()), projectInt);
  }

  public void del(IntIdInterface strIdInterface) {
    super.delete(CACHE_PROJECT_INT, String.valueOf(strIdInterface.id()));
  }

  public ProjectInt byId(IntIdInterface intIdInterface) {
    return super.byId(CACHE_PROJECT_INT, String.valueOf(intIdInterface.id()), ProjectInt.class);
  }

}
  • varchar 做爲主鍵
@Service("ProjectStrDbOperationImpl")
public class ProjectStrDbOperationImpl implements DbOperation<ProjectStr, StrIdInterface> {

  @Autowired
  private ProjectStrMapper projectStrMapper;

  @Override
  public boolean insert(ProjectStr projectInt) {
    return projectStrMapper.insert(projectInt) > 0;
  }

  @Override
  public ProjectStr byId(StrIdInterface interfaces) {
    return projectStrMapper.selectByPrimaryKey(interfaces.id());
  }

  @Override
  public boolean del(StrIdInterface interfaces) {
    return projectStrMapper.deleteByPrimaryKey(interfaces.id()) > 0;
  }

  @Override
  public boolean editor(StrIdInterface interfaces, ProjectStr projectInt) {
    // 更新存在策略
    ProjectStr projectInt1 = this.byId(interfaces);
    projectInt1.setName(projectInt.getName());

    return this.projectStrMapper.updateByPrimaryKey(projectInt1) > 0;
  }
}


@Service("ProjectStrRedisOperationImpl")
public class ProjectStrRedisOperationImpl
    extends RedisHashKeyOperation<ProjectStr>
    implements
    RedisOperation<ProjectStr, StrIdInterface> {

  public static final String CACHE_PROJECT_INT = "cache:project_int";

  public void insert(ProjectStr projectStr) {
    super.insert(CACHE_PROJECT_INT, String.valueOf(projectStr.getId()), projectStr);
  }

  public void update(StrIdInterface strIdInterface, ProjectStr projectStr) {
    super.update(CACHE_PROJECT_INT, String.valueOf(strIdInterface.id()), projectStr);
  }

  public void del(StrIdInterface strIdInterface) {
    super.delete(CACHE_PROJECT_INT, String.valueOf(strIdInterface.id()));
  }

  public ProjectStr byId(StrIdInterface intIdInterface) {
    return super.byId(CACHE_PROJECT_INT, String.valueOf(intIdInterface.id()), ProjectStr.class);

  }

}
  • 基本操做已經具有. 可是對外的方法還須要再一次進行抽象. 在總體操做中,CRUD的邏輯相似. 繼續進行改進.
  • 統一對外提供的接口以下
public interface ByIdOperationFacade<T, I extends IdInterface> {

  boolean insert(T t);

  T byId(I i);

  boolean del(I i);

  boolean editor(I i, T t);

  DbOperation getDbOperation();

}
  • 光有這個還不夠. 咱們提供的統一 crud 方法還須要獲得兩個關鍵對象, 1. DbOperation 2. RedisOperation. 定義出操做集合類
public class OperationCollections {

  private DbOperation dbOperation;

  private RedisOperation redisOperation;
}
  • 有了操做集合. 缺乏一個生成它的工具
public interface ByIdOperationFactory {

  OperationCollections factory(Class<?> clazz);
  
}
  • 實現以下. 注意:此處爲了讓泛型更好的使用, 這裏採用的是類型對應一個操做對象(DbOperation,RedisOperation).設計模式

    • 須要改進的地方: 給DbOperationRedisOperation 添加一個函數用來獲取類型
@Service("ByIdOperationFactoryImpl")
public class ByIdOperationFactoryImpl implements ByIdOperationFactory {

  static Map<Class, DbOperation> dbOperationMap = new HashMap<>();
  static Map<Class, RedisOperation> redisOperationHashMap = new HashMap<>();
  @Autowired
  private ApplicationContext context;

  @PostConstruct
  public void init() {
    Map<String, DbOperation> beansOfType = context.getBeansOfType(DbOperation.class);
    beansOfType.forEach(
        (k, v) -> {
          Class type = v.type();
          dbOperationMap.put(type, v);
        }
    );

    Map<String, RedisOperation> beansOfType1 = context.getBeansOfType(RedisOperation.class);
    beansOfType1.forEach((k, v) -> {
      Class type = v.type();
      redisOperationHashMap.put(type, v);
    });

  }

  @Override
  public OperationCollections factory(Class<?> clazz) {
    OperationCollections operationCollections = new OperationCollections();
    DbOperation dbOperation = dbOperationMap.get(clazz);
    RedisOperation redisOperation = redisOperationHashMap.get(clazz);

    operationCollections.setDbOperation(dbOperation);
    operationCollections.setRedisOperation(redisOperation);

    return operationCollections;
  }
}
  • 實現一個通用的 id 操做對象
public abstract class CommonByIdOperation<T, I extends IdInterface>
    implements ByIdOperationFacade<T, I> {

  @Autowired
  @Qualifier("ByIdOperationFactoryImpl")
  ByIdOperationFactory byIdOperationFactory;

  @Override
  public boolean insert(T t) {
    throw new RuntimeException("沒有實現");
  }

  @Override
  public DbOperation getDbOperation() {
    throw new RuntimeException("沒有實現");
  }

  public boolean editor(I i, T t) {

    boolean editor = false;

    OperationCollections operationCollections = this.operationCollection();
    RedisOperation redisOperation = operationCollections.getRedisOperation();

    if (redisOperation != null) {
      redisOperation.del(i);
    }

    DbOperation dbOperation = operationCollections.getDbOperation();

    if (dbOperation != null) {
      editor = dbOperation.editor(i, t);
    }

    if (redisOperation != null) {
      redisOperation.insert(t);
    }

    return editor;
  }


  public boolean del(I i) {
    boolean del = false;

    OperationCollections operationCollections = this.operationCollection();

    RedisOperation redisOperation = operationCollections.getRedisOperation();
    if (redisOperation != null) {
      redisOperation.del(i);
    }

    DbOperation dbOperation = operationCollections.getDbOperation();
    if (dbOperation != null) {
      del = dbOperation.del(i);
    }

    return del;
  }

  public T byId(I i) {
    T result = null;
    RedisOperation redisOperation = this.operationCollection().getRedisOperation();
    if (redisOperation != null) {
      result = (T) redisOperation.byId(i);
    }

    DbOperation dbOperation = this.operationCollection().getDbOperation();

    if (dbOperation != null) {
      if (result == null) {
        System.out.println("從數據庫獲取");
        result = (T) dbOperation.byId(i);
      }
    }

    return result;
  }


  public OperationCollections operationCollection() {
    return byIdOperationFactory.factory(clazz());
  }

  protected Class<?> clazz() {
    throw new IllegalArgumentException("類型異常");
  }
}
  • 說明: 這裏的 insert 操做不建議在此進行處理. 應爲id是在mybatis插入後纔會有. 固然也能夠經過一個bean—copy的方式在這裏獲取. 可是咱們的命名若是不一樣. beanCopy 就不行了.
  • 簡單概述bean-copy的方式
public class BeanCopy {

  public static void main(String[] args) {
    StrId strId = new StrId();
    strId.setId("str_id");

    CpId cpId = new CpId();
    BeanUtils.copyProperties(strId, cpId);
    System.out.println(cpId.getId());
  }
  @Data
  static class CpId {
    private Object id;
  }

  @Data
  static class StrId {

    private String id;
  }
}
  • 最後的組裝過程
@Service("ProjectIntDbOperationImpl")
public class ProjectIntDbOperationImpl implements DbOperation<ProjectInt, IntIdInterface> {

  @Autowired
  private ProjectIntMapper projectIntMapper;

  @Override
  public boolean insert(ProjectInt projectInt) {
    return projectIntMapper.insert(projectInt) > 0;
  }

  @Override
  public ProjectInt byId(IntIdInterface interfaces) {
    return projectIntMapper.selectByPrimaryKey(interfaces.id());
  }

  @Override
  public boolean del(IntIdInterface interfaces) {
    return projectIntMapper.deleteByPrimaryKey(interfaces.id()) > 0;
  }

  @Override
  public boolean editor(IntIdInterface interfaces, ProjectInt projectInt) {
    // 更新存在策略
    ProjectInt projectInt1 = this.byId(interfaces);
    projectInt1.setName(projectInt.getName());

    return this.projectIntMapper.updateByPrimaryKey(projectInt1) > 0;
  }

  @Override
  public Class<?> type() {
    return ProjectInt.class;
  }
}
@Service("ProjectStrFacade")
public class ProjectStrFacade extends CommonByIdOperation<ProjectStr, StrIdInterface> implements
    ByIdOperationFacade<ProjectStr, StrIdInterface> {

  @Override
  public boolean insert(ProjectStr projectInt) {
    DbOperation dbOperation = this.getDbOperation();
    boolean insert = false;
    if (dbOperation != null) {
      insert = dbOperation.insert(projectInt);
    }
    RedisOperation redisOperation = this.operationCollection().getRedisOperation();
    if (redisOperation != null) {
      redisOperation.insert(projectInt);
    }
    return insert;
  }

  @Override
  public ProjectStr byId(StrIdInterface strIdInterface) {
    return super.byId(strIdInterface);
  }

  @Override
  public boolean del(StrIdInterface strIdInterface) {
    return super.del(strIdInterface);
  }

  public boolean editor(StrIdInterface strIdInterface, ProjectStr projectInt) {
    return super.editor(strIdInterface, projectInt);
  }

  @Override
  public DbOperation getDbOperation() {
    return this.operationCollection().getDbOperation();
  }

  @Override
  protected Class<?> clazz() {
    return ProjectStr.class;
  }
}

測試

@SpringBootTest
class DemoApplicationTests {

  Gson gson = new Gson();
  @Autowired
  private ProjectStrMapper projectStrMapper;
  @Autowired
  @Qualifier("projectIntFacade")
  private ByIdOperationFacade<ProjectInt, IntIdInterface> byIdOperationFacade;
  @Autowired
  private StringRedisTemplate stringRedisTemplate;

  @Test
  void testInsert() {
    ProjectInt projectInt = new ProjectInt();
    projectInt.setName("JJJ");
    this.byIdOperationFacade.insert(projectInt);
  }

  @Test
  void testUpdate() {
    ProjectInt projectInt = this.byIdOperationFacade.byId(new IntIdInterface() {
      @Override
      public int id() {
        return 1;
      }
    });

    projectInt.setName("update");
    this.byIdOperationFacade.editor(new IntIdInterface() {
      @Override
      public int id() {
        return projectInt.getId();
      }
    }, projectInt);
  }

  @Test
  void testDel() {
    this.byIdOperationFacade.del(new IntIdInterface() {
      @Override
      public int id() {
        return 1;
      }
    });
  }

  @Test
  void testById() {
    ProjectInt projectInt = this.byIdOperationFacade.byId(new IntIdInterface() {
      @Override
      public int id() {
        return 1;
      }
    });
    System.out.println();
  }
}
  • 咱們對外暴露的類只有ByIdOperationFacade.
@Autowired
  @Qualifier("projectIntFacade")
  private ByIdOperationFacade<ProjectInt, IntIdInterface> byIdOperationFacade;
  • 經過不一樣的實現來操做不一樣的數據庫對象

總結

相關文章
相關標籤/搜索