对于常用 crud 的一些思考和设计

2020-08-31 14:03:16 +08:00
 huifer

Crud 项目介绍

简化单表的 CRUD 基本代码.

项目地址

为什么使用

假设现在有表格

| 字段 |类型 | | ---- | ---- | | id | int | | name | varchar |

常规实现

  1. 创建 controller 类
  2. 创建 service 类
  3. 创建 一些验证方法
  4. 组合起来
@RestController
@RequestMapping("/demo")
public class ProjectDemoController {

  @Autowired
  private ProjectDemoMapper projectDemoMapper;

  @PostMapping("/add")
  public ResultVO add(
      @RequestBody ProjectDemo req
  ) {
    int i = projectDemoMapper.insertSelective(req);
    if (i > 0) {
      return ResultVO.success();
    }
    else {
      return ResultVO.failed();
    }
  }
}

crud 实现

省略 controller 的编写

@CrudController(uri = "/project/demo", idType = Integer.class)
public class ProjectInt extends AbsEntity implements Serializable {

  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

自定义入参验证

@Service
public class ProjectIntValidated implements ValidatedInterface<ProjectInt> {

  Gson gson = new Gson();


  public Class<?> entityClass() {
    return ProjectInt.class;
  }

  public void validateDelete(ProjectInt projectInt) {
    System.out.println(gson.toJson(projectInt));
  }

  public void validateAdd(ProjectInt projectInt) {
    System.out.println(gson.toJson(projectInt));
  }

  public void validateById(ProjectInt projectInt) {
    System.out.println(gson.toJson(projectInt));
  }

  public void validateEditor(ProjectInt projectInt) {
    System.out.println(gson.toJson(projectInt));
  }
}

省略 mybatis + redis 编写


public class IssuesEntity implements BaseEntity {


  private Integer id;
  private String newTitle;
  private Date date;
 
}

@Mapper
@CacheKey(key = "issues", type = IssuesEntity.class)
public interface IssuesMapper extends A<Integer, IssuesEntity> {

  @Insert("   insert into issue(new_title)values(#{newTitle,jdbcType=VARCHAR})")
  @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
  int insertSelective(IssuesEntity record);

  @Select("select id as id , new_title as newTitle from issue where id = #{integer} ")
  IssuesEntity selectByPrimaryKey(Integer integer);

  @Override
  @Update("UPDATE `issue` SET `new_title` = #{newTitle}  WHERE `id` = #{id} ")
  int updateByPrimaryKeySelective(IssuesEntity record);

  @Override
  @Delete("delete from issue where id = #{integer}")
  int deleteByPrimaryKey(Integer integer);
}
  @Autowired
  private CrudFacade<IssuesEntity, IntIdInterface<Integer>> crudFacade;


 @Test
  void testInsert() {
    IssuesEntity issuesEntity = new IssuesEntity();
    issuesEntity.setNewTitle("mybatis_test");
    crudFacade.insert(issuesEntity);
  }

省略 redis 编写

@CacheKey(key = "tt", type = IssuesEntity.class, idMethod = "ooo")
public class IssuesEntity implements BaseEntity {


  private Integer id;
  private String newTitle;
  private Date date;
}
@Autowired
private CrudEntityFacade<IssuesEntity> crudEntityFacade;

@Test
void testInsert() {
  IssuesEntity issuesEntity = new IssuesEntity();
  issuesEntity.setNewTitle("insert");
  issuesEntity.setDate(new Date());
  crudEntityFacade.insert(issuesEntity);
}

自定义 json 序列化规则

注意

目前 redis 仅支持 hash 操作

欢迎各位提出意见 : https://github.com/huifer/crud/issues

1962 次点击
所在节点    Java
3 条回复
yumenawei
2020-08-31 14:39:46 +08:00
OcaServlet 是个啥,没看到相关定义
chendy
2020-08-31 14:46:43 +08:00
感谢分享
东西一般
huifer
2020-08-31 15:44:15 +08:00
@yumenawei 自定义的一个 servlet

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/702807

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX