import
java.io.Serializable;
public
abstract
class
BaseModel
implements
Serializable{
}
|
1
|
import
java.util.Map;
import
org.springframework.web.servlet.ModelAndView;
public
class
BaseMultiController {
protected
ModelAndView toView(
final
String url,
final
Map
{
ModelAndView view =
new
ModelAndView(url);
return
view;
}
}
|
1
|
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
@Autowired
protected
RedisTemplate
/**
* 设置redisTemplate
* @param redisTemplate the redisTemplate to set
*/
public
void
setRedisTemplate(RedisTemplate
this
.redisTemplate = redisTemplate;
}
/**
* 获取 RedisSerializer
*
*/
protected
RedisSerializer
return
redisTemplate.getStringSerializer();
}
}
|
1
|
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
new
HashMap
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
new
HashMap
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
new
HashMap
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
new
HashMap
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);
}
}
|
import
java.util.List;
import
com.pudp.bae.model.Member;
public
interface
MemberDao {
boolean
add(Member member);
abstract
boolean
add(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
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
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
|
package
com.pudp.bae.dao.redis;
import
java.util.ArrayList;
import
java.util.List;
import
org.springframework.dao.DataAccessException;
import
org.springframework.data.redis.connection.RedisConnection;
import
org.springframework.data.redis.core.RedisCallback;
import
org.springframework.data.redis.serializer.RedisSerializer;
import
org.springframework.stereotype.Repository;
import
org.springframework.util.Assert;
import
com.pudp.bae.base.RedisGeneratorDao;
import
com.pudp.bae.model.Member;
@Repository
(value=
"memberDao"
)
public
class
MemberDaoImpl
extends
RedisGeneratorDao
implements
MemberDao{
/**
* 添加对象
*/
@Override
public
boolean
add(
final
Member member) {
boolean
result = redisTemplate.execute(
new
RedisCallback
public
Boolean doInRedis(RedisConnection connection)
throws
DataAccessException {
RedisSerializer
byte
[] key = serializer.serialize(member.getId());
byte
[] name = serializer.serialize(member.getNickname());
return
connection.setNX(key, name);
}
});
return
result;
}
/**
* 添加集合
*/
@Override
public
boolean
add(
final
List
Assert.notEmpty(list);
boolean
result = redisTemplate.execute(
new
RedisCallback
public
Boolean doInRedis(RedisConnection connection)
throws
DataAccessException {
RedisSerializer
for
(Member member : list) {
byte
[] key = serializer.serialize(member.getId());
byte
[] name = serializer.serialize(member.getNickname());
connection.setNX(key, name);
}
return
true
;
}
},
false
,
true
);
return
result;
}
/**
* 删除对象 ,依赖key
*/
@Override
public
void
delete(String key) {
List
new
ArrayList
list.add(key);
delete(list);
}
/**
* 删除集合 ,依赖key集合
*/
public
void
delete(List
redisTemplate.delete(keys);
}
/**
* 修改对象
*/
public
boolean
update(
final
Member member) {
String key = member.getId();
if
(get(key) ==
null
) {
throw
new
NullPointerException(
"数据行不存在, key = "
+ key);
}
boolean
result = redisTemplate.execute(
new
RedisCallback
public
Boolean doInRedis(RedisConnection connection)
throws
DataAccessException {
RedisSerializer
byte
[] key = serializer.serialize(member.getId());
byte
[] name = serializer.serialize(member.getNickname());
connection.set(key, name);
return
true
;
}
});
return
result;
}
/**
* 根据key获取对象
*/
@Override
public
Member get(
final
String keyId) {
Member result = redisTemplate.execute(
new
RedisCallback
public
Member doInRedis(RedisConnection connection)
throws
DataAccessException {
RedisSerializer
byte
[] key = serializer.serialize(keyId);
byte
[] value = connection.get(key);
if
(value ==
null
) {
return
null
;
}
String nickname = serializer.deserialize(value);
return
new
Member(keyId, nickname);
}
});
return
result;
}
}
|
|
import
com.pudp.bae.base.BaseModel;
public
class
Member
extends
BaseModel{
private
static
final
long
serialVersionUID = -1959528436584592183L;
private
String id;
private
String nickname;
public
Member(){}
public
Member(String id, String nickname){
this
.setId(id);
this
.setNickname(nickname);
}
public
String getId() {
return
id;
}
public
void
setId(String id) {
this
.id = id;
}
public
String getNickname() {
return
nickname;
}
public
void
setNickname(String nickname) {
this
.nickname = nickname;
}
}
|
import
javax.annotation.Resource;
import
com.pudp.bae.dao.redis.MemberDao;
import
com.pudp.bae.model.Member;
public
class
MemberService {
@Resource
(name=
"memberDao"
)
private
MemberDao memberDao;
public
void
setMemberDao(MemberDao memberDao)
{
this
.memberDao = memberDao;
}
public
void
add(Member member){
memberDao.add(member);
}
public
void
delete(String id){
memberDao.delete(id);
}
public
Member get(String id)
{
return
memberDao.get(id);
}
}
|
redis-context.xml
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
|
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:p=
"http://www.springframework.org/schema/p"
xmlns:tx=
"http://www.springframework.org/schema/tx"
xmlns:context=
"http://www.springframework.org/schema/context"
xsi:schemaLocation="
http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//www.springframework.org/schema/tx
http:
//www.springframework.org/schema/tx/spring-tx-3.0.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-3.0.xsd
">
p:host-name=
"${redis.host}"
p:port=
"${redis.port}"
p:password=
"${redis.pass}"
p:pool-config-ref=
"poolConfig"
/>
|
spring-context.xml
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
|
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:p=
"http://www.springframework.org/schema/p"
xmlns:mvc=
"http://www.springframework.org/schema/mvc"
xmlns:context=
"http://www.springframework.org/schema/context"
xmlns:util=
"http://www.springframework.org/schema/util"
xsi:schemaLocation="http:
//www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http:
//www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http:
//www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<
import
resource=
"redis-context.xml"
/>
|
spring-mvc.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:context=
"http://www.springframework.org/schema/context"
xmlns:mvc=
"http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http:
//www.springframework.org/schema/beans
http:
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
http:
//www.springframework.org/schema/context
http:
//www.springframework.org/schema/context/spring-context-3.0.xsd
http:
//www.springframework.org/schema/mvc
http:
//www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
|
redis.properties
1
2
3
4
5
6
7
8
9
10
|