Skip to content

Commit b8b23c3

Browse files
committed
# 修改适配类
1 parent eba524e commit b8b23c3

File tree

4 files changed

+146
-49
lines changed

4 files changed

+146
-49
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package me.zhengjie.base;
2+
3+
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
4+
import com.baomidou.mybatisplus.extension.service.IService;
5+
import com.google.common.collect.Sets;
6+
import me.zhengjie.utils.WhereFun;
7+
import me.zhengjie.utils.WrapperUtils;
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.data.jpa.repository.JpaRepository;
10+
11+
import java.io.Serializable;
12+
import java.util.List;
13+
14+
/**
15+
* mybatisPlus & JPA 适配
16+
*
17+
* @author liaojinlong
18+
* @since 2020/6/29 10:22
19+
*/
20+
public class BaseDao<I extends IService<T>, J extends JpaRepository<T, ID>, T, ID extends Serializable> {
21+
protected I mpService;
22+
protected J jpaRepository;
23+
@Value("${db.type.switch:true}")
24+
protected boolean dbSwitch;
25+
26+
public BaseDao(I mpService, J jpaRepository) {
27+
this.mpService = mpService;
28+
this.jpaRepository = jpaRepository;
29+
}
30+
31+
public void save(T columnInfo) {
32+
if (dbSwitch) {
33+
jpaRepository.save(columnInfo);
34+
} else {
35+
mpService.save(columnInfo);
36+
}
37+
}
38+
39+
public List<T> saveAll(List<T> entities) {
40+
if (dbSwitch) {
41+
return jpaRepository.saveAll(entities);
42+
} else {
43+
mpService.saveBatch(entities);
44+
return entities;
45+
}
46+
}
47+
48+
public void delete(T entity) {
49+
if (dbSwitch) {
50+
jpaRepository.delete(entity);
51+
} else {
52+
mpService.remove(WrapperUtils.excute(entity, Wrappers.query(), WhereFun.DEFAULT));
53+
}
54+
}
55+
56+
public void deleteById(ID id) {
57+
if (dbSwitch) {
58+
jpaRepository.deleteById(id);
59+
} else {
60+
mpService.removeById(id);
61+
}
62+
}
63+
64+
public void update(T columnInfo) {
65+
if (dbSwitch) {
66+
jpaRepository.saveAndFlush(columnInfo);
67+
} else {
68+
mpService.saveOrUpdate(columnInfo);
69+
}
70+
}
71+
72+
public void batUpdate(List<T> entities) {
73+
if (dbSwitch) {
74+
jpaRepository.saveAll(entities);
75+
} else {
76+
mpService.saveOrUpdateBatch(entities);
77+
}
78+
}
79+
80+
public T selectById(ID id) {
81+
T t;
82+
if (dbSwitch) {
83+
t = jpaRepository.getOne(id);
84+
} else {
85+
t = mpService.getById(id);
86+
}
87+
return t;
88+
}
89+
90+
public List<T> selectAllById(Iterable<ID> ids) {
91+
List<T> t;
92+
if (dbSwitch) {
93+
t = jpaRepository.findAllById(ids);
94+
} else {
95+
t = mpService.listByIds(Sets.newHashSet(ids));
96+
}
97+
return t;
98+
}
99+
100+
public I getMpService() {
101+
return mpService;
102+
}
103+
104+
public void setMpService(I mpService) {
105+
this.mpService = mpService;
106+
}
107+
108+
public J getJpaRepository() {
109+
return jpaRepository;
110+
}
111+
112+
public void setJpaRepository(J jpaRepository) {
113+
this.jpaRepository = jpaRepository;
114+
}
115+
116+
public boolean isDbSwitch() {
117+
return dbSwitch;
118+
}
119+
120+
public void setDbSwitch(boolean dbSwitch) {
121+
this.dbSwitch = dbSwitch;
122+
}
123+
}
Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package me.zhengjie.repository;
22

33
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
4+
import me.zhengjie.base.BaseDao;
45
import me.zhengjie.domain.ColumnInfo;
56
import me.zhengjie.repository.jpa.ColumnInfoRepository;
67
import me.zhengjie.repository.mp.ColumnInfoService;
7-
import me.zhengjie.utils.WhereFun;
8-
import me.zhengjie.utils.WrapperUtils;
98
import org.springframework.stereotype.Component;
109

1110
import java.util.List;
@@ -15,57 +14,19 @@
1514
* @since 2020/6/28 14:59
1615
*/
1716
@Component
18-
public class ColumnInfoDao {
19-
private Boolean dbType = false;
20-
private ColumnInfoService columnInfoService;
21-
private ColumnInfoRepository columnInfoRepository;
17+
public class ColumnInfoDao extends BaseDao<ColumnInfoService, ColumnInfoRepository, ColumnInfo, Long> {
2218

23-
public ColumnInfoDao(ColumnInfoService columnInfoService, ColumnInfoRepository columnInfoRepository) {
24-
this.columnInfoService = columnInfoService;
25-
this.columnInfoRepository = columnInfoRepository;
19+
public ColumnInfoDao(ColumnInfoService baseService, ColumnInfoRepository jpaRepository ) {
20+
super(baseService, jpaRepository);
2621
}
2722

2823
public List<ColumnInfo> findByTableNameOrderByIdAsc(String tableName) {
29-
if (false) {
30-
return columnInfoRepository.findByTableNameOrderByIdAsc(tableName);
24+
if (dbSwitch) {
25+
return jpaRepository.findByTableNameOrderByIdAsc(tableName);
3126
} else {
32-
return columnInfoService
33-
.selectList(Wrappers.<ColumnInfo>query().eq(true, "TABLE_NAME", tableName));
27+
return mpService
28+
.list(Wrappers.<ColumnInfo>query().eq(true, "TABLE_NAME", tableName));
3429
}
3530
}
3631

37-
public List<ColumnInfo> saveAll(List<ColumnInfo> columnInfos) {
38-
if (true) {
39-
return columnInfoRepository.saveAll(columnInfos);
40-
} else {
41-
columnInfos.forEach(columnInfo -> {
42-
columnInfoService.insert(columnInfo);
43-
});
44-
return columnInfos;
45-
}
46-
}
47-
48-
public void delete(ColumnInfo columnInfo) {
49-
if (dbType) {
50-
columnInfoRepository.delete(columnInfo);
51-
} else {
52-
columnInfoService.delete(WrapperUtils.excute(columnInfo, Wrappers.query(), WhereFun.DEFAULT));
53-
}
54-
}
55-
56-
public void save(ColumnInfo columnInfo) {
57-
if (dbType) {
58-
columnInfoRepository.save(columnInfo);
59-
} else {
60-
columnInfoService.insert(columnInfo);
61-
}
62-
}
63-
64-
public void update(ColumnInfo columnInfo) {
65-
if (dbType) {
66-
columnInfoRepository.saveAndFlush(columnInfo);
67-
} else {
68-
columnInfoService.updateById(columnInfo);
69-
}
70-
}
7132
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package me.zhengjie.repository.mp;
2+
3+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4+
import me.zhengjie.domain.ColumnInfo;
5+
6+
/**
7+
* @author liaojinlong
8+
* @since 2020/6/28 14:57
9+
*/
10+
public interface ColumnInfoMapper extends BaseMapper<ColumnInfo> {
11+
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package me.zhengjie.repository.mp;
22

3-
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
3+
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
44
import me.zhengjie.domain.ColumnInfo;
5+
import org.springframework.stereotype.Repository;
56

67
/**
78
* @author liaojinlong
89
* @since 2020/6/28 14:57
910
*/
10-
public interface ColumnInfoService extends BaseMapper<ColumnInfo> {
11+
@Repository
12+
public class ColumnInfoService extends ServiceImpl<ColumnInfoMapper, ColumnInfo> {
1113
}

0 commit comments

Comments
 (0)