SpringMVC+redis整合

 

 

 

在網絡上有一個很多人轉載的springmvc+redis整合的案例,不過一直不完整,也是被各種人裝來轉去,現在基本將該框架搭建起來。

1
2
3
4
5
6
7
package  com.pudp.bae.base;
 
import  java.io.Serializable;
 
public  abstract  class  BaseModel  implements  Serializable{
 
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
package  com.pudp.bae.base;
 
import  java.util.Map;
 
import  org.springframework.web.servlet.ModelAndView;
 
public  class  BaseMultiController {
     protected  ModelAndView toView( final  String url, final  Map<String,Object> map)
     {   
         ModelAndView  view =  new  ModelAndView(url);
         return  view;
     }
}

 

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
package  com.pudp.bae.base;
 
import  java.io.Serializable;
 
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.data.redis.core.RedisTemplate;
import  org.springframework.data.redis.serializer.RedisSerializer;
 
public  abstract  class  RedisGeneratorDao<K  extends  Serializable, V  extends  Serializable>  {
   
   @Autowired
   protected  RedisTemplate<K,V> redisTemplate ;
 
   /** 
    * 設置redisTemplate 
    * @param redisTemplate the redisTemplate to set 
    */ 
   public  void  setRedisTemplate(RedisTemplate<K, V> redisTemplate) {  
     this .redisTemplate = redisTemplate;  
   }  
     
   /** 
    * 獲取 RedisSerializer 
    * <br>------------------------------<br> 
    */ 
   protected  RedisSerializer<String> getRedisSerializer() {  
     return  redisTemplate.getStringSerializer();  
   }
 
}

 

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package  com.pudp.bae.controller;
 
import  java.util.HashMap;
import  java.util.Map;
 
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;
 
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.stereotype.Controller;
import  org.springframework.web.bind.annotation.ModelAttribute;
import  org.springframework.web.bind.annotation.PathVariable;
import  org.springframework.web.bind.annotation.RequestMapping;
import  org.springframework.web.bind.annotation.RequestMethod;
import  org.springframework.web.servlet.ModelAndView;
 
import  com.pudp.bae.base.BaseMultiController;
import  com.pudp.bae.model.Member;
import  com.pudp.bae.service.MemberService;
 
@Controller
@RequestMapping (value =  "/member" )
public  class  MemberController  extends  BaseMultiController {
 
     @Autowired
     private  MemberService memberService;
 
     public  void  setMemberService(MemberService memberService) {
         this .memberService = memberService;
     }
 
     @RequestMapping (value = {  "/add" "/add.html"  }, method = { RequestMethod.GET })
     public  ModelAndView add(HttpServletRequest request,
             HttpServletResponse response) {
         Map<String, Object> map =  new  HashMap<String, Object>();
         Member member =  new  Member();
         member.setId( "1" );
         member.setNickname( "guoxiaoming" );
         this .memberService.add(member);
         
         return  toView( "add" , map);
     }
 
     @RequestMapping (value = {  "/add" "/add.html"  }, method = { RequestMethod.POST })
     public  ModelAndView addMember(HttpServletRequest request,
             HttpServletResponse response,
             @ModelAttribute ( "member" ) Member member) {
         Map<String, Object> map =  new  HashMap<String, Object>();
         System.out.println(member);
         map.put( "message" "成功添加數據到庫,"  + member);
         this .memberService.add(member);
         return  toView( "message" , map);
     }
 
     @RequestMapping (value = {  "/{id:\\d+}/query" "/{id:\\d+}/query.html"  }, method = {
             RequestMethod.GET, RequestMethod.POST })
     public  ModelAndView queryMember(HttpServletRequest request,
             HttpServletResponse response,  @PathVariable ( "id" ) String id) {
         Map<String, Object> map =  new  HashMap<String, Object>();
         System.out.println(id);
         Member member =  this .memberService.get(id);
         if  ( null  != member) {
             map.put( "message" "查詢Id="  + id +  "的用戶名爲:"  + member.getNickname());
         else  {
             map.put( "message" "沒有查詢到與Id="  + id +  "相關的數據" );
         }
         return  toView( "message" , map);
     }
 
     @RequestMapping (value = {  "/{id:\\d+}/delete" "/{id:\\d+}/delete.html"  }, method = {
             RequestMethod.GET, RequestMethod.POST })
     public  ModelAndView deleteMember(HttpServletRequest request,
             HttpServletResponse response,  @PathVariable ( "id" ) String id) {
         Map<String, Object> map =  new  HashMap<String, Object>();
         try  {
             this .memberService.delete(id);
             map.put( "message" "刪除Id爲"  + id +  "的用戶成功." );
         catch  (Exception e) {
             e.printStackTrace();
             map.put( "message" "刪除Id爲"  + id +  "的用戶失敗, "  + e.getMessage());
         }
         return  toView( "message" , map);
     }
 
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package  com.pudp.bae.dao.redis;
 
import  java.util.List;
 
import  com.pudp.bae.model.Member;
 
public  interface  MemberDao {
     boolean  add(Member member);
 
     abstract  boolean  add(List<Member> list);
 
     void  delete(String key);
 
     Member get(String keyId);
     
}

 

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
37
38
39
40
41
42
43
44
45
46
47
48
37
相關文章
相關標籤/搜索