Skip to content

Commit 748b941

Browse files
committed
支持数据库动态切换
1 parent cbc702f commit 748b941

28 files changed

+238
-97
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@
8686
<artifactId>sqljdbc4</artifactId>
8787
<version>4.0</version>
8888
</dependency>-->
89-
<dependency>
89+
<!--<dependency>
9090
<groupId>org.postgresql</groupId>
9191
<artifactId>postgresql</artifactId>
92-
</dependency>
92+
</dependency>-->
9393

9494
<!-- Mybatis -->
9595
<dependency>

src/main/java/com/example/Application.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
/**
88
* Application
9-
* @author dolyw.com
9+
*
10+
* @author wliduo[[email protected]]
1011
* @date 2018/11/16 19:29
1112
*/
1213
@SpringBootApplication
13-
@tk.mybatis.spring.annotation.MapperScan({ "com.example.mapper", "com.example.dao" })
14-
@PropertySource(value = { "classpath:config/generator.properties" })
14+
@tk.mybatis.spring.annotation.MapperScan({"com.example.mapper", "com.example.dao"})
15+
@PropertySource(value = {"classpath:config/generator.properties"})
1516
public class Application {
1617
public static void main(String[] args) {
1718
SpringApplication.run(Application.class, args);

src/main/java/com/example/common/IBaseService.java

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,179 +7,204 @@
77

88
/**
99
* IBaseService
10-
* @author dolyw.com
10+
*
11+
* @author wliduo[[email protected]]
1112
* @date 2018/8/9 15:45
1213
*/
1314
public interface IBaseService<T> {
1415

1516
// Select
17+
1618
/**
1719
* 根据实体中的属性值进行查询,查询条件使用等号
20+
*
1821
* @param record
1922
* @return java.util.List<T>
20-
* @author dolyw.com
23+
* @author wliduo[i@dolyw.com]
2124
* @date 2018/8/9 15:43
2225
*/
2326
List<T> select(T record);
2427

2528
/**
2629
* 根据主键字段进行查询,方法参数必须包含完整的主键属性,查询条件使用等号
30+
*
2731
* @param key
2832
* @return T
29-
* @author dolyw.com
33+
* @author wliduo[i@dolyw.com]
3034
* @date 2018/8/9 15:43
3135
*/
3236
T selectByPrimaryKey(Object key);
3337

3438
/**
3539
* 查询全部结果,select(null)方法能达到同样的效果
36-
* @param
40+
*
41+
* @param
3742
* @return java.util.List<T>
38-
* @author dolyw.com
43+
* @author wliduo[i@dolyw.com]
3944
* @date 2018/8/9 15:43
4045
*/
4146
List<T> selectAll();
4247

4348
/**
4449
* 根据实体中的属性进行查询,只能有一个返回值,有多个结果是抛出异常,查询条件使用等号
50+
*
4551
* @param record
4652
* @return T
47-
* @author dolyw.com
53+
* @author wliduo[i@dolyw.com]
4854
* @date 2018/8/9 15:43
4955
*/
5056
T selectOne(T record);
5157

5258
/**
5359
* 根据实体中的属性查询总数,查询条件使用等号
60+
*
5461
* @param record
5562
* @return int
56-
* @author dolyw.com
63+
* @author wliduo[i@dolyw.com]
5764
* @date 2018/8/9 15:43
5865
*/
5966
int selectCount(T record);
6067

6168
// Insert
69+
6270
/**
6371
* 保存一个实体,null的属性也会保存,不会使用数据库默认值
72+
*
6473
* @param record
6574
* @return int
66-
* @author dolyw.com
75+
* @author wliduo[i@dolyw.com]
6776
* @date 2018/8/9 15:43
6877
*/
6978
int insert(T record);
7079

7180
/**
7281
* 保存一个实体,null的属性不会保存,会使用数据库默认值
82+
*
7383
* @param record
7484
* @return int
75-
* @author dolyw.com
85+
* @author wliduo[i@dolyw.com]
7686
* @date 2018/8/9 15:43
7787
*/
7888
int insertSelective(T record);
7989

8090
// Update
91+
8192
/**
8293
* 根据主键更新实体全部字段,null值会被更新
94+
*
8395
* @param record
8496
* @return int
85-
* @author dolyw.com
97+
* @author wliduo[i@dolyw.com]
8698
* @date 2018/8/9 15:43
8799
*/
88100
int updateByPrimaryKey(T record);
89101

90102
/**
91103
* 根据主键更新属性不为null的值
104+
*
92105
* @param record
93106
* @return int
94-
* @author dolyw.com
107+
* @author wliduo[i@dolyw.com]
95108
* @date 2018/8/9 15:43
96109
*/
97110
int updateByPrimaryKeySelective(T record);
98111

99112
// Delete
113+
100114
/**
101115
* 根据实体属性作为条件进行删除,查询条件使用等号
116+
*
102117
* @param record
103118
* @return int
104-
* @author dolyw.com
119+
* @author wliduo[i@dolyw.com]
105120
* @date 2018/8/9 15:43
106121
*/
107122
int delete(T record);
108123

109124
/**
110125
* 根据主键字段进行删除,方法参数必须包含完整的主键属性
126+
*
111127
* @param key
112128
* @return int
113-
* @author dolyw.com
129+
* @author wliduo[i@dolyw.com]
114130
* @date 2018/8/9 15:44
115131
*/
116132
int deleteByPrimaryKey(Object key);
117133

118134
// Example
135+
119136
/**
120137
* 根据Example条件进行查询,这个查询支持通过Example类指定查询列,通过selectProperties方法指定查询列
138+
*
121139
* @param example
122140
* @return java.util.List<T>
123-
* @author dolyw.com
141+
* @author wliduo[i@dolyw.com]
124142
* @date 2018/8/9 15:44
125143
*/
126144
List<T> selectByExample(Object example);
127145

128146
/**
129147
* 根据Example条件进行查询总数
148+
*
130149
* @param example
131150
* @return int
132-
* @author dolyw.com
151+
* @author wliduo[i@dolyw.com]
133152
* @date 2018/8/9 15:44
134153
*/
135154
int selectCountByExample(Object example);
136155

137156
/**
138157
* 根据Example条件更新实体record包含的全部属性,null值会被更新
158+
*
139159
* @param record
140-
* @param example
160+
* @param example
141161
* @return int
142-
* @author dolyw.com
162+
* @author wliduo[i@dolyw.com]
143163
* @date 2018/8/9 15:44
144164
*/
145165
int updateByExample(@Param("record") T record, @Param("example") Object example);
146166

147167
/**
148168
* 根据Example条件更新实体record包含的不是null的属性值
169+
*
149170
* @param record
150-
* @param example
171+
* @param example
151172
* @return int
152-
* @author dolyw.com
173+
* @author wliduo[i@dolyw.com]
153174
* @date 2018/8/9 15:44
154175
*/
155176
int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
156177

157178
/**
158179
* 根据Example条件删除数据
180+
*
159181
* @param example
160182
* @return int
161-
* @author dolyw.com
183+
* @author wliduo[i@dolyw.com]
162184
* @date 2018/8/9 15:44
163185
*/
164186
int deleteByExample(Object example);
165187

166188
// RowBounds
189+
167190
/**
168191
* 根据实体属性和RowBounds进行分页查询
192+
*
169193
* @param record
170-
* @param rowBounds
194+
* @param rowBounds
171195
* @return java.util.List<T>
172-
* @author dolyw.com
196+
* @author wliduo[i@dolyw.com]
173197
* @date 2018/8/9 15:44
174198
*/
175199
List<T> selectByRowBounds(T record, RowBounds rowBounds);
176200

177201
/**
178202
* 根据example条件和RowBounds进行分页查询
203+
*
179204
* @param example
180-
* @param rowBounds
205+
* @param rowBounds
181206
* @return java.util.List<T>
182-
* @author dolyw.com
207+
* @author wliduo[i@dolyw.com]
183208
* @date 2018/8/9 15:44
184209
*/
185210
List<T> selectByExampleAndRowBounds(Object example, RowBounds rowBounds);

src/main/java/com/example/common/ResponseBean.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
/**
44
* ResponseBean
5-
* @author dolyw.com
5+
*
6+
* @author wliduo[[email protected]]
67
* @date 2018/8/30 11:39
78
*/
89
public class ResponseBean {

src/main/java/com/example/common/impl/BaseServiceImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99

1010
/**
1111
* BaseServiceImpl
12-
* @author dolyw.com
12+
*
13+
* @author wliduo[[email protected]]
1314
* @date 2018/8/9 15:45
1415
*/
1516
public abstract class BaseServiceImpl<T> implements IBaseService<T> {

src/main/java/com/example/config/DataBaseConfig.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
/**
1212
* Dao配置注入
13-
* @author dolyw.com
13+
*
14+
* @author wliduo[[email protected]]
1415
* @date 2019/4/5 17:56
1516
*/
1617
@Configuration
@@ -33,16 +34,17 @@ public class DataBaseConfig {
3334

3435
/**
3536
* 根据驱动判断注入那个类型数据库
37+
*
3638
* @param
37-
* @throws
3839
* @return com.example.dao.GeneratorDao
39-
* @author dolyw.com
40+
* @throws
41+
* @author wliduo[[email protected]]
4042
* @date 2019/4/5 17:59
4143
*/
4244
@Bean
4345
@Primary
4446
public GeneratorDao getGeneratorDao() {
45-
if(driver.indexOf(DataBaseEnum.MYSQL.getValue()) >= 0) {
47+
if (driver.indexOf(DataBaseEnum.MYSQL.getValue()) >= 0) {
4648
return mySqlGeneratorDao;
4749
} else if (driver.indexOf(DataBaseEnum.ORACLE.getValue()) >= 0) {
4850
return oracleGeneratorDao;

src/main/java/com/example/config/ExceptionAdvice.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313

1414
/**
1515
* 异常控制处理器
16-
* @author dolyw.com
16+
*
17+
* @author wliduo[[email protected]]
1718
* @date 2018/8/30 14:02
1819
*/
1920
@RestControllerAdvice
2021
public class ExceptionAdvice {
2122
/**
2223
* 捕捉自定义异常
24+
*
2325
* @return
2426
*/
2527
@ResponseStatus(HttpStatus.BAD_REQUEST)
@@ -30,6 +32,7 @@ public ResponseBean handle(CustomException e) {
3032

3133
/**
3234
* 捕捉系统异常
35+
*
3336
* @return
3437
*/
3538
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@@ -40,6 +43,7 @@ public ResponseBean handle(SystemException e) {
4043

4144
/**
4245
* 捕捉404异常
46+
*
4347
* @return
4448
*/
4549
@ResponseStatus(HttpStatus.NOT_FOUND)
@@ -50,6 +54,7 @@ public ResponseBean handle(NoHandlerFoundException e) {
5054

5155
/**
5256
* 捕捉其他所有异常
57+
*
5358
* @param request
5459
* @param ex
5560
* @return
@@ -62,6 +67,7 @@ public ResponseBean globalException(HttpServletRequest request, Throwable ex) {
6267

6368
/**
6469
* 获取状态码
70+
*
6571
* @param request
6672
* @return
6773
*/

src/main/java/com/example/config/WebMvcConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
/**
88
* SpringBoot全局支持CORS(跨源请求)的配置
9-
* @author dolyw.com
9+
*
10+
* @author wliduo[[email protected]]
1011
* @date 2018/8/9 17:28
1112
*/
1213
@Configuration

src/main/java/com/example/constant/Constant.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
/**
44
* Constant
5-
* @author dolyw.com
5+
*
6+
* @author wliduo[[email protected]]
67
* @date 2019/4/6 19:50
78
*/
89
public interface Constant {
@@ -22,4 +23,14 @@ public interface Constant {
2223
*/
2324
String TEMPLATE = "template";
2425

26+
/**
27+
* OS_NAME
28+
*/
29+
String OS_NAME = "os.name";
30+
31+
/**
32+
* Windows
33+
*/
34+
String WINDOWS = "Windows";
35+
2536
}

0 commit comments

Comments
 (0)