博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springmvc+mybatis多数据源配置,AOP注解动态切换数据源
阅读量:7086 次
发布时间:2019-06-28

本文共 15759 字,大约阅读时间需要 52 分钟。

springmvc与springboot没多大区别,springboot一个jar包配置几乎包含了所有springmvc,也不需要繁琐的xml配置,springmvc需要配置多种jar包,需要繁琐的xml配置,当然springmvc也可以使用java类来配置,但这种感觉没有xml配置来的直观。 

下面是springmvc+mybatis动态多数据源完整代码:

数据源配置db.properties

1   2 #datasource.driver=com.mysql.jdbc.Driver 3   4 #datasource.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver 5   6 #datasource.url=jdbc:sqlserver://127.0.0.1:1433;DatabaseName=db1 7   8 #数据源1 9  10 datasource1.driver=com.mysql.jdbc.Driver11  12 datasource1.url=jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false13  14 datasource1.username=root15  16 datasource1.password=root17  18 #数据源219  20 datasource2.driver=com.mysql.jdbc.Driver21  22 datasource2.url=jdbc:mysql://127.0.0.1:3306/db2?useUnicode=true&characterEncoding=utf-8&useSSL=false23  24 datasource2.username=root25  26 datasource2.password=root27  28 #通用配置29  30 jdbc.initialSize=531  32 jdbc.minIdle=533  34 jdbc.maxIdle=2035  36 jdbc.maxActive=10037  38 jdbc.maxWait=10000039  40 jdbc.defaultAutoCommit=false41  42 jdbc.removeAbandoned=true43  44 jdbc.removeAbandonedTimeout=60045  46 jdbc.testWhileIdle=true47  48 jdbc.timeBetweenEvictionRunsMillis=6000049  50 jdbc.numTestsPerEvictionRun=20

springmvc配置

1 
2 3
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
86 87 88 89
90 91
92 93
94 95
96 97 98 99
100 101
102 103
104 105
106 107
108 109
110 111
112 113
114 115
116 117
118 119
120 121
122 123
124 125
126 127
128 129
130 131
132 133
134 135
136 137
138 139
140 141
142 143
144 145
146 147
148 149
150 151
152 153
154 155
156 157
158 159
160 161
162 163
164 165
166 167
168 169
170 171
172 173
174 175 176 177
178 179
180 181
182 183
184 185
187
189
191
193
195
199
203
204 205
206 207
208 209
210 211
212 213
214 215
216 217
218 219
220 221
222 223
224 225
226 227
228 229
230 231
232 233
239
240 241
242 243
244 245
246 247
248 249
253
254 255
256 257
258 259
260 261
262 263
264 265
266 267
268 269
270 271
272 273
274 275
276 277 278 279
280 281
285
286 287
291
292 293
298 299
300 301
314 315
316 317
318 319
320 321
332 333
337

mybatis配置

 

1    2 
3 4 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
67 68
69 70
73 74
75 76
77 78
79 80
81 82
83 84 85 86
87 88
97 98
99 100
101 102
103 104
105 106

数据源切换保存类

1 package com.ss.config; 2   3   4   5 /** 6   7 * Created by pure on 2018-05-06. 8   9 */10  11 public class DataSourceContextHolder {12  13 /**14  15 * 默认数据源16  17 */18  19 public static final String DEFAULT_DS = "datasource1";20  21  22  23 private static final ThreadLocal
contextHolder = new ThreadLocal<>();24 25 26 27 // 设置数据源名28 29 public static void setDB(String dbType) {30 31 System.out.println("切换到{"+dbType+"}数据源");32 33 contextHolder.set(dbType);34 35 }36 37 38 39 // 获取数据源名40 41 public static String getDB() {42 43 //return (contextHolder.get());44 45 if(contextHolder.get()==null){46 47 return DEFAULT_DS;48 49 }else{50 51 return (contextHolder.get());52 53 }54 55 }56 57 58 59 // 清除数据源名60 61 public static void clearDB() {62 63 contextHolder.remove();64 65 }66 67 }

动态数据源类

1 package com.ss.config; 2   3   4   5 import org.springframework.core.annotation.Order; 6   7 import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; 8   9  10  11 /**12  13 * Created by pure on 2018-05-08.14  15 */16  17 @Order(2)18  19 public class DynamicDataSource extends AbstractRoutingDataSource {20  21 @Override22  23 protected Object determineCurrentLookupKey() {24  25 System.out.println("当前数据源为"+DataSourceContextHolder.getDB());26  27 return DataSourceContextHolder.getDB();28  29 }30 31 }

 

自定义注解

1 package com.ss.config; 2   3   4   5 import java.lang.annotation.*; 6   7   8   9 /**10  11 * 自定义注解12  13 */14  15 @Retention(RetentionPolicy.RUNTIME)16  17 @Target({ElementType.METHOD})18  19 @Documented20  21 public @interface DS {22  23 String value() default "datasource1";24  25 }

 

注解Aop切割类

1 package com.ss.config;  2    3    4    5 import org.aspectj.lang.JoinPoint;  6    7 import org.aspectj.lang.annotation.After;  8    9 import org.aspectj.lang.annotation.Aspect; 10   11 import org.aspectj.lang.annotation.Before; 12   13 import org.aspectj.lang.reflect.MethodSignature; 14   15 import org.springframework.core.annotation.Order; 16   17 import org.springframework.stereotype.Component; 18   19   20   21 import java.lang.reflect.Method; 22   23   24   25 /** 26   27 * 自定义注解 + AOP的方式实现数据源动态切换。 28   29 * Created by pure on 2018-05-06. 30   31 */ 32   33 @Order(1) 34   35 @Aspect 36   37 //@Component 38   39 public class DynamicDataSourceAspect { 40   41 /* 42   43 //使用DS注解动作之后清除 44   45 @After("@annotation(DS)") 46   47 public void afterSwitchDS(JoinPoint point){ 48   49 System.out.println("清除当前数据源"+DataSourceContextHolder.getDB()); 50   51 DataSourceContextHolder.clearDB(); 52   53 } 54   55 //*/ 56   57 //使用DS注解动态切换 58   59 @Before("@annotation(DS)") 60   61 public void beforeSwitchDS(JoinPoint point){ 62   63 //获得当前访问的class 64   65 Class
className = point.getTarget().getClass(); 66 67 //获得访问的方法名 68 69 String methodName = point.getSignature().getName(); 70 71 //得到方法的参数的类型 72 73 Class[] argClass = ((MethodSignature)point.getSignature()).getParameterTypes(); 74 75 String dataSource = DataSourceContextHolder.DEFAULT_DS; 76 77 try { 78 79 // 得到访问的方法对象 80 81 Method method = className.getMethod(methodName, argClass); 82 83 // 判断是否存在@DS注解 84 85 if (method.isAnnotationPresent(DS.class)) { 86 87 DS annotation = method.getAnnotation(DS.class); 88 89 // 取出注解中的数据源名 90 91 dataSource = annotation.value(); 92 93 } 94 95 } catch (Exception e) { 96 97 e.printStackTrace(); 98 99 }100 101 // 切换数据源102 103 DataSourceContextHolder.setDB(dataSource);104 105 }106 107 108 109 }

Mapper映射xml

1 
2 3 4 5 6 7
8 9
10 11
16 17

dao层

1 package com.ss.dao; 2   3 import org.apache.ibatis.annotations.Mapper; 4   5 import org.apache.ibatis.annotations.Param; 6   7 import java.util.List; 8   9 import java.util.Map;10  11  12 /**13  14 * dao层15  16 * Created by pure on 2018-05-06.17  18 */19  20 @Mapper21  22 public interface UserDao {23  24 //使用xml配置形式查询25  26 public List getAllUser();27  28 }

service层

1 package com.ss.service; 2  3 import com.ss.config.DS; 4   5 import com.ss.dao.UserDao; 6   7 import org.springframework.beans.factory.annotation.Autowired; 8   9 import org.springframework.stereotype.Service;10 11 import java.util.List;12  13 import java.util.Map;14 15 /**16  17 * service层18  19 * Created by pure on 2018-05-06.20  21 */22  23 @Service24  25 public class UserService {26  27 @Autowired28  29 private UserDao userDao;30 31 //使用数据源1查询32  33 @DS("datasource1")34  35 public List getAllUser1(){36  37 return userDao.getAllUser();38  39 }40  41 //使用数据源2查询42  43 @DS("datasource2")44  45 public List getAllUser2(){46  47 return userDao.getAllUser();48  49 }50 51 }

controller层

1 package com.ss.controller; 2  3 import com.ss.config.DataSourceContextHolder; 4   5 import com.ss.service.UserService; 6   7 import org.springframework.beans.factory.annotation.Autowired; 8   9 import org.springframework.stereotype.Controller;10  11 import org.springframework.web.bind.annotation.RequestMapping;12  13 import org.springframework.web.bind.annotation.ResponseBody;14 15 import java.util.List;16  17 import java.util.Map;18 19 /**20  21 * Created by pure on 2018-05-08.22  23 */24  25 @Controller26  27 @RequestMapping("/user")28  29 public class UserController {30  31 32 @Autowired33  34 private UserService userService;35  36 37 @RequestMapping(value = "/getDb1AllUser")38  39 @ResponseBody40  41 public List getDb1AllUser() {42  43 List list = userService.getAllUser1();44  45 return list;46  47 }48 49  50 @RequestMapping(value = "/getDb2AllUser")51  52 @ResponseBody53  54 public List getDb2AllUser() {55  56 List list = userService.getAllUser2();57  58 return list;59  60 }61  62 }

maven配置pom.xml

1 
2 3
8 9
4.0.0
10 11 12
com.springmvc
13 14
ss1
15 16
1.0-SNAPSHOT
17 18
war
19 20 21
22 23
UTF-8
24 25
26 27
28 29 30 31
32 33
34 35
javax.servlet
36 37
jstl
38 39
1.2
40 41
42 43
44 45
taglibs
46 47
standard
48 49
1.1.2
50 51
52 53
54 55
javax.servlet.jsp
56 57
jsp-api
58 59
2.2
60 61
62 63 64
65 66
67 68
org.springframework
69 70
spring-context
71 72
4.3.9.RELEASE
73 74
75 76
77 78
org.springframework
79 80
spring-core
81 82
4.3.9.RELEASE
83 84
85 86
87 88
org.springframework
89 90
spring-beans
91 92
4.3.9.RELEASE
93 94
95 96
97 98
org.springframework
99 100
spring-web
101 102
4.3.9.RELEASE
103 104
105 106
107 108
org.springframework
109 110
spring-webmvc
111 112
4.3.9.RELEASE
113 114
115 116
117 118
org.springframework
119 120
spring-aop
121 122
4.3.9.RELEASE
123 124
125 126
127 128
org.springframework
129 130
spring-test
131 132
4.3.9.RELEASE
133 134
test
135 136
137 138
139 140
org.springframework
141 142
spring-context-support
143 144
4.3.9.RELEASE
145 146
147 148
149 150
commons-dbcp
151 152
commons-dbcp
153 154
1.4
155 156
157 158
159 160
org.springframework
161 162
spring-jdbc
163 164
4.3.9.RELEASE
165 166
167 168
169 170
org.springframework
171 172
spring-orm
173 174
4.3.9.RELEASE
175 176
177 178 179
180 181
org.aspectj
182 183
aspectjrt
184 185
1.8.10
186 187
188 189
190 191
org.aspectj
192 193
aspectjweaver
194 195
1.8.10
196 197
198 199 200
201 202
203 204
org.mybatis
205 206
mybatis
207 208
3.4.4
209 210
211 212
213 214
215 216
org.mybatis
217 218
mybatis-spring
219 220
1.3.1
221 222
223 224 225
226 227
228 229
mysql
230 231
mysql-connector-java
232 233
5.1.42
234 235
236 237 238
239 240
com.google.code.gson
241 242
gson
243 244
2.6.2
245 246
247 248 249 250
251 252
log4j
253 254
log4j
255 256
1.2.17
257 258
259 260
261 262 263 264
265 266
267 268
269 270
maven-compiler-plugin
271 272
3.1
273 274
275 276
1.7277 278
1.7
279 280
281 282
283 284
285 286
287 288

web.xml

1 
2 3
12 13
14 15
contextConfigLocation
16 17
18 19 classpath:/spring-mvc.xml 20 21
22 23
24 25
26 27
org.springframework.web.context.ContextLoaderListener
28 29
30 31 32 33
34 35
36 37
springServlet
38 39
org.springframework.web.servlet.DispatcherServlet
40 41
42 43
contextConfigLocation
44 45
classpath:/spring-mvc.xml
46 47
48 49
1
50 51
52 53
54 55
springServlet
56 57
/
58 59
60 61 62 63
64 65
encodingFilter
66 67
org.springframework.web.filter.CharacterEncodingFilter
68 69
70 71
encoding
72 73
UTF-8
74 75
76 77
78 79
forceEncoding
80 81
true
82 83
84 85
86 87
88 89
encodingFilter
90 91
/*
92 93
94 95 96 97
98 99
index.jsp
100 101
102 103

 

项目结构

运行结果:

代码下载:

 

转载于:https://www.cnblogs.com/weixupeng/p/9720472.html

你可能感兴趣的文章