avatar

前后端分离-后端简单增删改查编写

一、使用 generator 插件生成 pojo 和 mapper

参考:Mybatis配置generator插件

pojo

使用lombok自动生成getter,setter和toString方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import lombok.Setter;

import java.util.Date;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Score {
private Integer id;

private String subject;

private Float score;

private String type;

private Date tDate;

}

二、Controller编写

增删改查编写

ResultDTO为json统一格式类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@RestController
@RequestMapping("/score")
public class ScoresController {

@Autowired
private ScoresService scoresService;

/*添加成绩*/
@PostMapping("/addScore")
public ResultDTO addScore(@RequestBody Score score) {
this.scoresService.addScores(score);
return ResultDTO.success();
}

/*查询所有成绩*/
@GetMapping("/findScoreAll")
public ResultDTO findScoreAll(HttpServletRequest request) {
List<Score> list = this.scoresService.findScoreAll();
return ResultDTO.success().add("scorns", list);
}

/*更新成绩*/
@PostMapping("/updateScore")
public ResultDTO updateScore(@RequestBody Score score) {
this.scoresService.modifyScore(score);
return ResultDTO.success();
}

/*删除成绩*/
@GetMapping("/deleteScore")
public ResultDTO deleteScore(@RequestBody Integer id) {
this.scoresService.dropScoreById(id);
return ResultDTO.success();
}
}

三、业务层编写

ScoresServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*成绩管理业务层*/
@Service
public class ScoresServiceImpl implements ScoresService {
@Autowired
private ScoreMapper scoreMapper;

/*添加成绩*/
@Override
public void addScores(Score score) {
this.scoreMapper.insert(score);
}

/*查询全部成绩*/
@Override
public List<Score> findScoreAll() {
ScoreExample example = new ScoreExample();
return this.scoreMapper.selectByExample(example);
}

/*更新用户*/
@Override
@Transactional
public void modifyScore(Score score) {
this.scoreMapper.updateByPrimaryKey(score);
}

/*删除操作*/
@Override
@Transactional
public void dropScoreById(Integer id) {
this.scoreMapper.deleteByPrimaryKey(id);
}
}

四、json统一格式类

ResultDTO

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class ResultDTO {

private Integer status; // 返回状态
private String msg; // 返回消息
private Map<String, Object> body; // 返回数据


public boolean isSuccess() {
return status == 200;
}

public static ResultDTO success() {
return new ResultDTO(200, "OK", new LinkedHashMap<>());
}

public static ResultDTO error(Integer status, String message) {
return new ResultDTO(status, message, new LinkedHashMap<>());
}

public ResultDTO add(String key, Object value) {
body.put(key, value);
return this;
}

public ResultDTO addAll(Map<String, Object> map) {
body.putAll(map);
return this;
}

@Override
public String toString() {
return String.format(
"%s(status=%d, msg=%s, body[%s].size=%d",
this.getClass().getName(), status, msg, body.getClass().getName(), body.size()
);
}
}

评论