tonglin0325的个人主页

SpringBoot学习笔记——统一的service接口

统一的service接口基于统一的mapper,参考:Mybatis学习笔记——通用mapper

接口AbstractService

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
package com.example.demo.core.service;

import java.util.List;

public interface AbstractService<T> {

/**
* 获取所有
*
* @return List<T>
*/
List<T> listObjects();

/**
* 通过key查找
*
* @param key Object
* @return T
*/
T selectByKey(Object key);

/**
* 根据实体条件查找
*
* @param example Object
* @return List<T>
*/
List<T> selectByExample(Object example);

/**
* 持久化
*
* @param entity T
* @return key
*/
int save(T entity);

/**
* 通过主鍵刪除
*
* @param key Object
*/
int deleteByKey(Object key);

/**
* 通过example条件刪除
*
* @param example T
*/
int deleteByExample(T example);

/**
* 更新
*
* @param entity T
*/
int update(T entity);

}

抽象类AbstractServiceImpl

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
package com.example.demo.core.service.impl;

import com.example.demo.core.mapper.MyMapper;
import com.example.demo.core.service.AbstractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public abstract class AbstractServiceImpl<T> implements AbstractService<T> {

@Autowired
protected MyMapper<T> mapper;

@Override
public List<T> listObjects() {
return mapper.selectAll();
}

@Override
public T selectByKey(Object key) {
return mapper.selectByPrimaryKey(key);
}

@Override
public List<T> selectByExample(Object example) {
return mapper.selectByExample(example);
}

@Override
@Transactional
public int save(T entity) {
return mapper.insert(entity);
}

@Override
@Transactional
public int deleteByKey(Object key) {
return mapper.deleteByPrimaryKey(key);
}

@Override
@Transactional
public int deleteByExample(Object key) {
return mapper.deleteByExample(key);
}

@Override
@Transactional
public int update(T entity) {
return mapper.updateByPrimaryKeySelective(entity);
}

}

参考:

1
2
https://github.com/febsteam/FEBS-Security/blob/master/febs-common/src/main/java/cc/mrbird/common/service/impl/BaseService.java

以及

1
2
https://github.com/Zoctan/WYUOnlineJudge/blob/master/api/src/main/java/com/zoctan/api/core/service/AbstractService.java

之后在使用的时候,可以通过继承AbstractServiceImpl的方式,来省略一些通用service方法的编写,比如

UserService接口

1
2
3
4
5
6
7
8
package com.example.demo.service;

import com.example.demo.core.service.AbstractService;
import com.example.demo.model.User;

public interface UserService extends AbstractService<User> {
}

UserServiceImpl实现类

1
2
3
4
5
6
7
8
9
10
11
12
package com.example.demo.service.impl;

import com.example.demo.core.service.impl.AbstractServiceImpl;
import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends AbstractServiceImpl<User> implements UserService {
}

进行测试

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
package com.example.demo.service.impl;

import com.example.demo.model.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class UserServiceImplTest {

@Autowired
private UserServiceImpl userService;

@Test
public void UserMapper() {
List<User> list = userService.listObjects();
for (User user: list) {
System.out.println(user.getId());
}
}

}