Springboot Mybatis 实现通用Dao、Service、Controller

简述

若手动编写数据库层是繁琐, 重复且容易出错。这里尝试实现通用的Dao、Service、Controllerc 减少重复的代码。使用generator来生成Mapper.xml、DO、Query。使编写数据库层自动化。

下面以app表为例

Dao 层

编写Dao层泛型接口, 对于每一个Mapper.xml 建一个Dao继承该接口, 其中无需编写代码

AppDao.java

1
2
3
4
@Mapper
public interface AppDao extends Dao<AppDO> {

}

Dao.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public interface Dao<D> {

D selectById(Long id);

List<D> select(D bean);

int selectCount(D bean);

List<D> select(Query<D> query);

int selectCount(Query<D> query);

int insert(D bean);

int update(D bean);

int delete(Long id);
}

Service 层

service层编写 Service接口以及BaseService, service中在dao的基础上实现扩展方法, 详见service接口

AppService.java

1
2
3
4
@Service
public class AppService extends BaseService<AppDO> {

}

BaseService.java

此处注入dao 需要使用@Autowired, 若使用@Resource则会报错

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
public abstract class BaseService<D> implements Service<D> {

@Autowired
private Dao<D> mapper;

@Override
public D selectById(Long id){
return mapper.selectById(id);
}

@Override
public List<D> select(D bean){
return mapper.select(bean);
}

@Override
public int selectCount(D bean){
return mapper.selectCount(bean);
}

@Override
public List<D> selectByQuery(Query<D> query){
return mapper.select(query);
}

@Override
public int selectCountByQuery(Query<D> query){
return mapper.selectCount(query);
}

@Override
public List<D> selectAll(){
return select(null);
}

@Override
public D selectOne(D bean){
if(selectCount(bean)!=1){
return null;
}
List<D> list = select(bean);
return list.get(0);
}

@Override
public D selectOneByQuery(Query<D> query){
if(selectCountByQuery(query)!=1){
return null;
}
List<D> list = selectByQuery(query);
return list.get(0);
}

@Override
public Page<D> selectPageByQuery(Query<D> query){
Page<D> page = new Page<>();
page.setList(selectByQuery(query));
page.setTotal(selectCountByQuery(query));
return page;
}

@Override
public int insert(D bean){
return mapper.insert(bean);
}

@Override
public int update(D bean){
return mapper.update(bean);
}

@Override
public int delete(Long id){
return mapper.delete(id);
}
}

Service.java

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
public interface Service<D> {

D selectById(Long id);

List<D> select(D bean);

int selectCount(D bean);

List<D> selectByQuery(Query<D> query);

int selectCountByQuery(Query<D> query);

List<D> selectAll();

D selectOne(D bean);

D selectOneByQuery(Query<D> query);

Page<D> selectPageByQuery(Query<D> query);

int insert(D bean);

int update(D bean);

int delete(Long id);
}

Controller 层

有了 编写Controller接口和BaseController, 普通增删改查控制器只需继承BaseController

AppController.java

1
2
3
4
5
@RestController
@RequestMapping("app")
public class AdminAppController extends BaseController<AppDO,AppQuery> {

}

BaseController.java

这里注入service同样要用@Autowired

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
public abstract class BaseController<D, Q extends Query<D>> implements Controller<D,Q> {

@Autowired
private Service<D> service;

@Override
@RequestMapping("list")
public ResponseData list(@RequestBody Q query) {
return ResponseData.successResponse(service.selectPageByQuery(query));
}

@Override
@RequestMapping("save")
public ResponseData save(@RequestBody D bean) {
service.insert(bean);
return ResponseData.successResponse();
}

@Override
@RequestMapping("update")
public ResponseData update(@RequestBody D bean) {
service.update(bean);
return ResponseData.successResponse();
}
}

Controller.java

1
2
3
4
5
6
7
8
public interface Controller<D, Q extends Query<D>> {

ResponseData list(Q query);

ResponseData save(D bean);

ResponseData update(D bean);
}
 欢迎关注我的博客公众号
如果觉得我的文章对你有用,请随意赞赏
0%