简化单表的 CRUD 基本代码.
比如学生管理系统.表设计有 课程表t_classes
等等...在管理系统中我们需要添加课程
的时候需要做一次 controller 、service 、redis 、dao 这几类操作. 每多一个表格都需要做这一批操作.
一般有新增、修改、删除、根据 id 查询.
假设现在有表格
| 字段 |类型 | | ---- | ---- | | id | int | | name | varchar |
@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();
}
}
}
省略验证方法, 省略 service 编写. 正常应该分一分.
现在 只有数据库层面的交互代码我们可能还需要一个 redis 上的操作
(hash 数据类型) 那么这部分代码还需要在写一次
@RestController
@RequestMapping("/demo")
public class ProjectDemoController {
@Autowired
private ProjectDemoMapper projectDemoMapper;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@PostMapping("/add")
public ResultVO add(
@RequestBody ProjectDemo req
) {
int i = projectDemoMapper.insertSelective(req);
if (i > 0) {
stringRedisTemplate.opsForHash()
.put("demo", String.valueOf(req.getId()), JSON.toJSONString(req));
return ResultVO.success();
}
else {
return ResultVO.failed();
}
}
}
路径说明: /rest
+ @CrudController#uri
+ add\editor\del\byId
定义一个 实体对象
@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;
}
}
定义一个 mapper
@Mapper
@CacheKey(type = ProjectInt.class,key = "asdc")
public interface ProjectIntMapper extends A<Integer, ProjectInt> {
@Override
@Insert("INSERT INTO `project_int`(`name`) VALUES (#{name} ) ")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
int insertSelective(ProjectInt record);
@Override
@Select("select * from project_int where id = #{id,javaType=INTEGER} ")
ProjectInt selectByPrimaryKey(@Param("id") Integer integer);
@Override
@Delete("DELETE FROM `dest`.`project_int` WHERE `id` = #{id} ")
int deleteByPrimaryKey(@Param("id") Integer integer);
@Override
@Update("UPDATE `dest`.`project_int` SET `name` = #{name} WHERE `id`= #{id} ")
int updateByPrimaryKeySelective(ProjectInt record);
}
注册 servlet
@Bean
ServletRegistrationBean myServletRegistration() {
ServletRegistrationBean srb = new ServletRegistrationBean();
srb.setServlet(new OcaServlet());
srb.setUrlMappings(Arrays.asList("/rest/*"));
return srb;
}
权限验证:在使用了两个 servlet 后通过拦截器验证会有问题,请使用 filter 进行权限验证
com.github.huifer.crud.ctr.validated.ValidatedInterface
entityClass
填写对应的数据库对象即可@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));
}
}
有时我们需要直接调用普通的 crud 不需要从 controller 进行调用 。在这个前提下我们可以使用
需要在 mapper 和 db 实体类进行标记
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 中而不是存储 db 。 在这个前提下可以使用
类标记
@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);
}
目前只支持 gson 和 jackjson 两种
com.github.huifer.crud.common.conf.json.GsonConfigSetting
com.github.huifer.crud.common.conf.json.JackJsonConfigSetting
具体用例请查看: https://github.com/huifer/crud/tree/dev/simple-example
目前 redis 仅支持 hash 操作
欢迎各位提出意见 : https://github.com/huifer/crud/issues
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.