zset实现奖牌排行榜案例

实现效果
zset实现奖牌排行榜案例_第1张图片

首先创建需要一个JavaBean来存储数据

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Rank {
     

    private String country;
    private int score;
    private int goldCount;
    private int silverCount;
    private int copperCount;


}

service层

@Service
@Slf4j
public class RankingServiceImpl implements RankingService {
     

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public List<Rank> ranking(String country,Double score) {
     

        if(country==null||country.equals("")){
     
        }else{
     
            redisTemplate.opsForZSet().incrementScore("rank",country,score);
        }


        Set<String> set = redisTemplate.opsForZSet().reverseRange("rank", 0, -1);
        List<Rank> list=new ArrayList<>();

        for (String s : set) {
     

            Rank rank1 = new Rank();
            Double rank = redisTemplate.opsForZSet().score("rank", s);
            int gold= (int) (rank/10000);
            int silver= (int) (rank/100%100);
            int copper= (int) (rank%100);
            int count=gold+silver+copper;
            rank1.setCountry(s);
            rank1.setGoldCount(gold);
            rank1.setSilverCount(silver);
            rank1.setCopperCount(copper);
            rank1.setScore(count);
            list.add(rank1);
        }


        return list;
    }
}

controller层

@Controller
public class RankingController {
     

    @Autowired
    private RankingService rankingService;

    @RequestMapping("index")
    public String index(){
     
        return "index";
    }

    @RequestMapping("ranking")
    public String ranking(@Param("country") String country,@Param("score")  Double score, Model model){
     
        List<Rank> rankList = rankingService.ranking(country, score);
        model.addAttribute("rankList",rankList);

        return "forward:index";
    }

}

前端页面

<!DOCTYPE html>
<html  xmlns:th="http://www.thymeleaf.org">
<head>
    <title>奖牌排行榜</title>
    <style>
        table tr th{
     
            text-align: center;
        }
    </style>
</head>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script type="text/javascript" src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
<body>


<div >
    <table class="table table-hover" style="text-align: center">
        <tr>
            <th>排序</th>
            <th>国家</th>
            <th>金牌</th>
            <th>银牌</th>
            <th>铜牌</th>
            <th>奖牌总数</th>
            <th width="200px">操作</th>
        </tr>
        <tr th:each="r:${rankList}">
            <td th:text="${rStat.count}">序号</td>
            <td th:text="${r.country}">国家</td>
            <td th:text="${r.goldCount}">金牌</td>
            <td th:text="${r.silverCount}">银牌</td>
            <td th:text="${r.copperCount}">铜牌</td>
            <td th:text="${r.score}">奖牌总数</td>
            <td>:<a href="javascript:goldin()" th:href="|javascript:goldin('${r.country}')|" class="glyphicon glyphicon-plus"></a>&ensp;&ensp;:<a href="javascript:silverin()" th:href="|javascript:silverin('${r.country}')|" class="glyphicon glyphicon-plus"></a>&ensp;&ensp;:<a href="javascript:copperin()" th:href="|javascript:copperin('${r.country}')|" class="glyphicon glyphicon-plus"></a></td>
        </tr>


    </table>
</div>



</body>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript">

    function goldin(country) {
     
        var score=10000.0;
        $.ajax({
     
            type:"post",
            url:"ranking",
            data:{
     "country":country,"score":score},
            success:function () {
     
                    location.href="ranking"
            }
        })
    }
    function silverin(country) {
     
        var score=100.0;
        $.ajax({
     
            type:"post",
            url:"ranking",
            data:{
     "country":country,"score":score},
            success:function () {
     
                location.href="ranking"
            }
        })
    }
    function copperin(country) {
     
        var score=1.0;
        $.ajax({
     
            type:"post",
            url:"ranking",
            data:{
     "country":country,"score":score},
            success:function () {
     
                location.href="ranking"
            }
        })
    }

</script>
</html>

你可能感兴趣的:(redis,ajax,jquery,bootstrap)