有个活动要个投票系统,要求每个用户每天只能投5票给用户,还TM的分区还得能搜索
需求上边已经说了 ,这里是在微信里传播投票的,微信公众号对每个用户都有一个唯一的openId号 所以我们利用这个在做为存入redis数据的key,value为一个hashmap对象 map对象储存有num(投票次数)和date(投票日期),
先根据拿到的openId判断这个map为不为空在进行后续操作
1,maven引入包坐标
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.1</version>
</dependency>
2,spring集成redis的 spring-redis.xml配置文件编写
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd ">
<!-- redis连接池的配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxIdle" value="500"/>
<property name="minIdle" value="1"/>
<property name="testOnBorrow" value="true"/>
<property name="testOnReturn" value="true"/>
</bean>
<!-- redis的连接池pool -->
<bean id = "jedisPool" class="redis.clients.jedis.JedisPool">
<constructor-arg index="0" ref="jedisPoolConfig"/>
<constructor-arg index="1" value="127.0.0.1"/> <!-- 地址 -->
<constructor-arg index="2" value="6379" type="int"/>
<constructor-arg index="3" value="15000" type="int"/>
<constructor-arg index="4" value="123456"/> <!-- 密码 -->
</bean>
</beans>
( 此处可以引入properties配置文件,我为了方便测试没有引入直接写在这个里面,建议用properties配置文件 )
注意:jedis 2.4.1版本以上没有maxActive与maxWait
3,web.xml引入spring配置文件,有spring配置文件也可以在spring配置文件里引入
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
4,切入正题真正要使用了(redis可以存储 string map list set 数据类型,具体查看api就好了)
//在controller注入jedis连接池
@Autowired
private JedisPool JedisPool;/**
* 投票方法
*
* @param request
* @param response
*/
@RequestMapping("/clickVote")
public void clickVote(HttpServletRequest request, HttpServletResponse response) {
try {
Jedis jedis = null;
jedis = JedisPool.getResource();
String openId = (String) session.getAttribute("openId");
Map<String, String> redisMap = new HashMap<String, String>();
/********************** 是否第一次投票 ******************************/
if ((jedis.hmget(openId, "date").get(0)) == null) {
redisMap.put("date", DateUtil.getDate());
redisMap.put("num", "0");
jedis.hmset(openId, redisMap);
}
/************************** 是否够五次 *******************************/
List<String> list = jedis.hmget(openId, "date", "num");
int ticketNum = Integer.parseInt(list.get(1)); // 票数
String ticketDate = list.get(0); // 投票日期
String sysDate = DateUtil.getDate();
// 日期不相等清空
if (!(ticketDate.equals(sysDate))) {
jedis.del(openId);
}
// 投票=5并且日期相同弹窗
if ((ticketNum > 4) && (ticketDate.equals(sysDate))) {
// 返回
Map<String, String> messageMap = new HashMap<String, String>();
messageMap.put("message", "a");//随便给
Object json = JSONObject.toJSON(messageMap);
// 设置access-control来解决跨域问题
response.addHeader("Access-Control-Allow-Origin", "*");
PrintWriter writer;
writer = response.getWriter();
writer.print(json);
} else {
ticketNum = ticketNum + 1;
redisMap.put("date", sysDate);
redisMap.put("num", String.valueOf(ticketNum));
jedis.hmset(openId, redisMap);
String stuId = request.getParameter("stuId");
int id = Integer.parseInt(stuId);
studentServlet.updateStuById(id);
Integer ticketnum = studentServlet.selectStuById(id).getTicketnum();
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("ticketnum", ticketnum);
Object json = JSONObject.toJSON(map);
// 设置access-control来解决跨域问题
response.addHeader("Access-Control-Allow-Origin", "*");
PrintWriter writer;
writer = response.getWriter();
writer.print(json);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {jedis.close();}}