用SQL語句查詢User表中newsh屬性為“政治類新聞”和“娛樂類新聞”的人數放入MAP中
問題一:整個方法該如何實現
問題二:SQL語句怎麼寫
參考用例
類似這樣的方法 新手不知道該怎麼改動成自己需要的方法
求助各位大神
public Map<String, Long> totalInventory(int year){
//獲取采購訂單(已入庫status=2)中指定產品的數量
String sql = "select month(p.pur_date) m, sum(pi.num) n "
+ " from t_purchase_item pi "
+ " join t_purchase p on pi.purc_id=p.id "
+ " where p.status=2 and year(p.pur_date) = ? "
+ " group by month(p.pur_date)";
Map<String, Long> map = getSession().doReturningWork(new ReturningWork<Map<String, Long>>() {
@Override
public Map<String, Long> execute(Connection conn)
throws SQLException {
Map<String, Long> map = new LinkedHashMap<String, Long>();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, year);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
String month = rs.getString(1);
Long num = Long.valueOf(rs.getLong(2));
map.put(month, num);
}
return map;
}
});
return map;
}
public Map<String, Long> sumPolEntNews(){
//查詢政治類新聞和娛樂類新聞的總數
String sql = "select count(*) from USER where newsh ='政治類新聞' or newsh ='娛樂類新聞'";
Map<String, Long> map = getSession().doReturningWork(new ReturningWork<Map<String, Long>>() {
@Override
public Map<String, Long> execute(Connection conn)
throws SQLException {
Map<String, Long> map = new LinkedHashMap<String, Long>();
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
Long num = Long.valueOf(rs.getLong(1));
map.put("政治類新聞和娛樂類新聞的總數", num);
}
return map;
}
});
return map;
}