package com.qfjy.controller;
import com.qfjy.entity.Medal;
import com.qfjy.service.MedalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
@Controller
public class MedalController {
@Autowired
private MedalService medalService;
@RequestMapping(“index”)
public String index(){
return “index”;
}
@RequestMapping("getMedalNum")
public String getMedalNum(Model model){
//System.out.println("1111111111111");
List<Medal> medal = medalService.getMedal();
// for (Medal m1:medal){
// System.out.println(m1);
// }
model.addAttribute(“medal”,medal);
return “index”;
}
@RequestMapping(“add”)
public String add(String country,String pai){
medalService.add(country,pai);
return "redirect:/getMedalNum";
}
}
package com.qfjy.service;
import com.qfjy.entity.Medal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.*;
@Service
public class MedalService {
@Autowired
private JedisPool jedisPool;
public List getMedal(){
Jedis jedis = jedisPool.getResource();
Set set = jedis.zrevrange(“country”, 0, -1);
List list=new ArrayList<>();
for (String a:set
) {
// System.out.println(a);
Double score = jedis.zscore("country", a);
//System.out.println(score);
//x金牌,z银牌,y银牌
int x= (int) (score/1000);
score=score-1000*x;
int y= (int) (score/1);
score=score-y;
int z= (int) (score/0.01);
//System.out.println(x+" "+y+" "+z);
Medal medal=new Medal();
medal.setGold(x);
medal.setSilver(y);
medal.setCopper(z);
medal.setCountry(a);
list.add(medal);
}
return list;
}
public void add(String Country,String pai){
Jedis jedis = jedisPool.getResource();
Double zscore = jedis.zscore("country", Country);
if(pai.equals("gold")){
zscore=zscore+1000;
}else if(pai.equals("silver")){
zscore=zscore+1;
}else{
zscore=zscore+0.01;
}
jedis.zadd("country",zscore,Country);
}
}
java:
application:
name: redis-test
spring:
redis:
port: 6379
password: 123456
host: 192.168.121.129
jedis:
pool:
max-idle: 6
max-active: 10
min-idle: 2
lettuce:
shutdown-timeout: 2000
database: 0
thymeleaf:
cache: false
mode: HTML
encoding: UTF-8
groovy:
template:
cache: false