V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
huifer
V2EX  ›  程序员

crud 模板方法

  •  
  •   huifer · 2020-08-21 16:02:19 +08:00 · 846 次点击
    这是一个创建于 1351 天前的主题,其中的信息可能已经有所发展或是发生改变。

    crud template

    功能

    • 单个表的数据库增删改查操作
    • 单个表的数据库+缓存的增删改查操作
    • 单个实体的缓存增删改查操作

    如何使用

    • 在启动类上添加如下代码
      • daoType 可选项有 mybatis 和 mybatis-plus
      • scanPackages 用来填写需要扫描的实体包路径,支持多个
    @EnableCrudTemplate(daoType = DaoType.MYBATIS_PLUS, scanPackages = {
        "com.github.huifer.mybatis.plus.mybatis"})
    

    mybatis 支持

    • 添加依赖
        <dependency>
          <groupId>com.github.huifer</groupId>
          <artifactId>for-mybatis</artifactId>
          <version>0.0.4-SNAPSHOT</version>
        </dependency>
    
    • 对 mapper 进行修改
    @CacheKey(key = "issues", type = IssuesEntity.class)
    public interface IssuesMapper extends A<Integer, IssuesEntity> {}
    
    • 对实体进行修改
    public class IssuesEntity implements BaseEntity {}
    

    mybatis plus 支持

    • 添加依赖
        <dependency>
          <groupId>com.github.huifer</groupId>
          <artifactId>for-mybatis-plus</artifactId>
          <version>0.0.4-SNAPSHOT</version>
        </dependency>
    
    • 对 mapper 进行修改
    @CacheKey(key = "issues", type = IssuesEntity.class)
    public interface IssuesMapper extends BaseMapper<IssuesEntity>,
        AforMybatisPlus<Integer, IssuesEntity> 
    
    • 对实体进行修改
    public class IssuesEntity implements BaseEntity {}
    
    • 调用层 mybatis 和 mybatis-plus 都是同一个入口
      @Autowired
      private CrudFacade<IssuesEntity, IntIdInterface<Integer>> crudFacade;
    
    • 只需要引入CrudFacade组件就可以拥有数据库+缓存的 crud 操作了. 如果不需要缓存请将 mapper 上的@CacheKey删掉

    entity 支持

    • 给实体对象添加注解 @CacheKey
      • redis-hash field 取值说明: idFiled=实体类的某个字段, idMethod=实体类中的一个方法,如果两者同时存在以idMethod为准, 建议填写一个
    @CacheKey(key = "tt", type = IssuesEntity.class, idFiled = "newTitle", idMethod = "ooo")
    public class IssuesEntity  {
    
    
      private Integer id;
      private String newTitle;
    
      private String ooo() {
        return "OOO" + this.newTitle;
      }
    }
    

    Contributers


    • 希望各位可以发表一下想法、看法
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2012 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 01:37 · PVG 09:37 · LAX 18:37 · JFK 21:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.