File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 5656 <version >1.1.9</version >
5757 </dependency >
5858 <dependency >
59- <groupId >org.aspectj</groupId >
60- <artifactId >aspectjweaver</artifactId >
61- <version >1.9.0</version >
59+ <groupId >org.springframework.boot</groupId >
60+ <artifactId >spring-boot-starter-aop</artifactId >
61+ </dependency >
62+ <dependency >
63+ <groupId >org.springframework.boot</groupId >
64+ <artifactId >spring-boot-starter-data-redis</artifactId >
6265 </dependency >
6366 <dependency >
64- <groupId >aopalliance </groupId >
65- <artifactId >aopalliance </artifactId >
66- <version >1.0 </version >
67+ <groupId >org.apache.commons </groupId >
68+ <artifactId >commons-lang3 </artifactId >
69+ <version >3.5 </version >
6770 </dependency >
71+ <dependency >
72+ <groupId >junit</groupId >
73+ <artifactId >junit</artifactId >
74+ <version >4.12</version >
75+ <scope >test</scope >
76+ </dependency >
77+
6878
6979 </dependencies >
7080
Original file line number Diff line number Diff line change @@ -38,15 +38,15 @@ public void setData(Object data) {
3838 this .data = data ;
3939 }
4040
41- private ResponseBean rtnSuccess (Object obj ){
41+ public ResponseBean rtnSuccess (Object obj ){
4242 ResponseBean responseBean = new ResponseBean ();
43- responseBean .setCode (200 );
43+ responseBean .setCode (0 );
4444 responseBean .setMsg ("success" );
4545 responseBean .setData (obj );
4646 return responseBean ;
4747 }
4848
49- private ResponseBean rtnError (Integer code , String msg ){
49+ public ResponseBean rtnError (Integer code , String msg ){
5050 ResponseBean responseBean = new ResponseBean ();
5151 responseBean .setCode (code );
5252 responseBean .setMsg (msg );
Original file line number Diff line number Diff line change 11package com .xh .basic .controller ;
22
33import com .xh .basic .bean .ResponseBean ;
4- import com .xh .basic .model .UserInfo ;
4+ import com .xh .basic .redis .annotation .MCache ;
5+ import com .xh .basic .redis .service .RedisService ;
56import com .xh .basic .service .UserInfoService ;
67import org .springframework .web .bind .annotation .PostMapping ;
78import org .springframework .web .bind .annotation .RequestMapping ;
@@ -24,9 +25,10 @@ public ResponseBean hello(){
2425 }
2526
2627 @ PostMapping ("/selectById" )
28+ @ MCache (keyOrIdx = "0" , cacheGroup = "userInfo" )
2729 public ResponseBean selectById (Integer id ){
2830 ResponseBean responseBean = new ResponseBean ();
29- responseBean . setData (userInfoService .selectById (id ));
31+ responseBean = responseBean . rtnSuccess (userInfoService .selectById (id ));
3032 return responseBean ;
3133 }
3234}
Original file line number Diff line number Diff line change 1- package com .xh .basic .aop ;
1+ package com .xh .basic .datasource . annotation ;
22
33import java .lang .annotation .ElementType ;
44import java .lang .annotation .Retention ;
Original file line number Diff line number Diff line change 1- package com .xh .basic .aop ;
1+ package com .xh .basic .datasource . aop ;
22
3+ import com .xh .basic .datasource .annotation .DataSourceTarget ;
34import com .xh .basic .datasource .DynamicDataSourceContextHolder ;
45import org .aspectj .lang .JoinPoint ;
56import org .aspectj .lang .annotation .After ;
2223@ Component
2324public class DynamicDataSourceAspect {
2425
25- @ Before ("@annotation(DataSourceTarget)" )
26+ @ Before ("@annotation(com.xh.basic.datasource.annotation. DataSourceTarget)" )
2627 public void beforeSwitchDataSource (JoinPoint joinpoint ){
2728 //获取当前访问的class
2829 Class <?> className = joinpoint .getTarget ().getClass ();
@@ -49,7 +50,7 @@ public void beforeSwitchDataSource(JoinPoint joinpoint){
4950 DynamicDataSourceContextHolder .setDataSource (dataSource );
5051 }
5152
52- @ After ("@annotation(DataSourceTarget)" )
53+ @ After ("@annotation(com.xh.basic.datasource.annotation. DataSourceTarget)" )
5354 public void afterSwitchDataSource (JoinPoint point ){
5455 DynamicDataSourceContextHolder .clearDataSource ();
5556 }
Original file line number Diff line number Diff line change 1- package com .xh .basic .config ;
1+ package com .xh .basic .datasource . config ;
22
33import com .alibaba .druid .pool .DruidDataSource ;
44import com .alibaba .druid .spring .boot .autoconfigure .DruidDataSourceBuilder ;
Original file line number Diff line number Diff line change 1- package com .xh .basic .config ;
1+ package com .xh .basic .datasource . config ;
22
33import org .apache .ibatis .session .SqlSessionFactory ;
44import org .mybatis .spring .SqlSessionFactoryBean ;
Original file line number Diff line number Diff line change 1+ package com .xh .basic .redis ;
2+
3+ import org .springframework .context .annotation .Bean ;
4+ import org .springframework .context .annotation .Configuration ;
5+ import org .springframework .data .redis .connection .RedisConnectionFactory ;
6+ import org .springframework .data .redis .connection .jedis .JedisConnectionFactory ;
7+ import org .springframework .data .redis .core .RedisTemplate ;
8+
9+ /**
10+ * @author szq
11+ * @Package com.xh.basic.redis
12+ * @Description: to do ...
13+ * @date 2018/4/2015:37
14+ */
15+ @ Configuration
16+ public class RedisConfig {
17+
18+ @ Bean ("redisTemplate" )
19+ public RedisTemplate <String , Object > redisTemplate (RedisConnectionFactory factory ){
20+ RedisTemplate <String , Object > template = new RedisTemplate <>();
21+ template .setConnectionFactory (factory );
22+ template .setKeySerializer (new RedisObjectSerializer ());
23+ template .setValueSerializer (new RedisObjectSerializer ());
24+ return template ;
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package com .xh .basic .redis ;
2+
3+ import org .springframework .core .convert .converter .Converter ;
4+ import org .springframework .core .serializer .support .DeserializingConverter ;
5+ import org .springframework .core .serializer .support .SerializingConverter ;
6+ import org .springframework .data .redis .serializer .RedisSerializer ;
7+ import org .springframework .data .redis .serializer .SerializationException ;
8+
9+ /**
10+ * @author szq
11+ * @Package com.xh.basic.redis
12+ * @Description: 对象序列化
13+ * @date 2018/4/2015:26
14+ */
15+ public class RedisObjectSerializer implements RedisSerializer <Object > {
16+
17+ private Converter <Object , byte []> serializer = new SerializingConverter ();
18+ private Converter <byte [], Object > deserializer = new DeserializingConverter ();
19+
20+ static final byte [] EMPTY_ARRAY = new byte [0 ];
21+
22+ @ Override
23+ public byte [] serialize (Object o ) throws SerializationException {
24+ if (o == null ){
25+ return EMPTY_ARRAY ;
26+ }
27+ try {
28+ return serializer .convert (o );
29+ }catch (Exception e ){
30+ return EMPTY_ARRAY ;
31+ }
32+ }
33+
34+ @ Override
35+ public Object deserialize (byte [] bytes ) throws SerializationException {
36+ if (isEmpty (bytes )){
37+ return null ;
38+ }
39+ try {
40+ return deserializer .convert (bytes );
41+ }catch (Exception e ){
42+ throw new SerializationException ("Cannot deserialize" , e );
43+ }
44+ }
45+
46+ private boolean isEmpty (byte [] data ){
47+ return (data == null || data .length == 0 );
48+ }
49+ }
Original file line number Diff line number Diff line change 1+ package com .xh .basic .redis .annotation ;
2+
3+ import java .lang .annotation .ElementType ;
4+ import java .lang .annotation .Retention ;
5+ import java .lang .annotation .RetentionPolicy ;
6+ import java .lang .annotation .Target ;
7+
8+ /**
9+ * @author szq
10+ * @Package com.xh.basic.redis.annotation
11+ * @Description: redis缓存标签
12+ * @date 2018/4/2016:04
13+ */
14+ @ Target (ElementType .METHOD )
15+ @ Retention (RetentionPolicy .RUNTIME )
16+ public @interface MCache {
17+ MCacheType cacheType () default MCacheType .QUERY ;
18+ String keyOrIdx ();
19+ String cacheGroup () default "" ;
20+ int cacheTime () default 180 ;
21+
22+ public enum MCacheType {
23+ QUERY ,NOT_QUERY ;
24+ }
25+ }
You can’t perform that action at this time.
0 commit comments