sql—如何使用jdbctemplate在java代码中按降序显示分级

bqf10yzr  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(271)

我正试图编写一个代码来按降序显示评分,但我希望例如:
我有1star到5star的评分,如果1star评分是2,3star评分是4,5star评分是1,2,4star评分是0。
现在我想按降序显示5star count、4star count、3star count、2star count和1star count。

List responseList = new ArrayList();
String lables = "54321";
String present = "";
List<Map<String, Object>> dataList = new ArrayList<Map<String,Object>>();

String sql = "select distinct count(name) name, technical from data where name =? group by Technical order by technical desc";

Map dataMap = null;
try {
    dataList= jdbcTemplate.queryForList(sql,name);

    for (Map<String, Object> row : dataList) {
        dataMap= new HashMap();

        String technicals = null;
        technicals =  row.get("technical")+ " Star";
        present +=  row.get("technical");
        dataMap.put("count", row.get("name"));
        dataMap.put("technical", technicals);

        responseList.add(dataMap);

        HashSet<Character> u = unique(present,lables);

        for (Character h : u) {
            System.out.println(h);
            dataMap= new HashMap();
            dataMap.put("count", 0);
            dataMap.put("technical", h + " Star");
            responseList.add(dataMap);
        }
    }
}
oxalkeyp

oxalkeyp1#

要按等级排序,请改为使用pojo Map ,这将很容易定制排序。您的pojo应该实现可比较的接口,并且应该提供 hashcode 以及 equals 方法 technical 现场。下面是示例代码。以及分类 TreeSet 反而会更好 ArrayList .

public class RankCount implements Comparable<RankCount>{

    private String technical;
    private int count;

    public RankCount(String technical, int count) {
        this.technical = technical;
        this.count = count;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((technical == null) ? 0 : technical.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        RankCount other = (RankCount) obj;
        if (technical == null) {
            if (other.technical != null)
                return false;
        } else if (!technical.equals(other.technical))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "RankCount [technical=" + technical + ", count=" + count + "]";
    }

    @Override
    public int compareTo(RankCount o) {
        return o.technical.compareToIgnoreCase(this.technical) ;
    }

}

例子:

Set<RankCount> dataList = new TreeSet<RankCount>();

    RankCount rc1 = new RankCount("3 Star", 4);
    RankCount rc2 = new RankCount("1 Star", 2);
    RankCount rc3 = new RankCount("5 Star", 1);
    RankCount rc4 = new RankCount("2 Star", 0);
    RankCount rc5 = new RankCount("4 Star", 0);

    dataList.add(rc1);
    dataList.add(rc2);
    dataList.add(rc3);
    dataList.add(rc4);
    dataList.add(rc5);

    System.out.println(dataList);

输出:

[
RankCount [technical=5 Star, count=1], 
RankCount [technical=4 Star, count=0], 
RankCount [technical=3 Star, count=4], 
RankCount [technical=2 Star, count=0],
RankCount [technical=1 Star, count=2]
]

相关问题