我的任务是按类型对书籍进行计数。 示例:genre1-5;
我不知道该怎么计数?
public List<Integer> task4() {
Session session = this.sessionFactory.getCurrentSession();
String sql = "SELECT book.genre, COUNT(book.genre) AS counter FROM book book GROUP BY genre";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(Book.class);
// List<Integer> task4List = session.createQuery("SELECT book.genre, COUNT(book.genre) AS counter FROM book GROUP BY genre")
// .list();
List<Integer> task4List = query.list();
//Map<Integer,String> task4List = (Map<counter,genre>)
return task4List;
}
第一次,我只是尝试计算没有类型名称的类型。我对此有疑问。 有或没有流派如何正确计算流派?
型号:
@Entity
@Table(name = "book")
public class Book {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "name")
private String name;
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Column(name = "published")
@Temporal(TemporalType.DATE)
private Date published;
@Column(name = "genre")
private String genre;
@Column(name = "rating")
private int rating;
P.S。 每个带有条件或hql的提示都会很好。
答案 0 :(得分:1)
String genre = "SELECT book.genre FROM Book book GROUP BY genre";
SQLQuery query= session.createSQLQuery(genre);
List<String> genreList = query.list();
String count = "SELECT COUNT(genre) AS counter FROM book book GROUP BY genre";
query= session.createSQLQuery(count);
List<Integer> countList = query.list();
Map<String, Integer> task4List = new HashMap<String, Integer>();
for (String genre : genreList) {
task4List.put(genre ,countList.get(genreList.indexOf(genre)));
}
答案 1 :(得分:0)
尝试此代码
public void task4() {
Session session = this.sessionFactory.getCurrentSession();
String sql = "SELECT genre, COUNT(1) FROM Book GROUP BY genre";
Query query=session.createQuery(sql);
List<Object[]> task4List = query.list();
for(Object[] o:task4List) {
System.out.println("genre = "+o[0]+" Count = "+o[1]);
}
}
答案 2 :(得分:0)
此SQL应该可以工作:
从书本中选择计数(区别(类型))
您还可以在Hibernate Criterion API中使用Projections。 Hibernate的投影都是count和distinct,Hibernate将根据实体映射生成正确的SQL。
答案 3 :(得分:0)
<script>
var dayArray= ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
var monthArray= ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
function getTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
var d = dayArray[today.getDay()];
var mo = monthArray[today.getMonth()];
var y = today.getFullYear();
m = correctDigit(m);
s = correctDigit(s);
document.getElementById('time')
.innerHTML = "<br><h1 class='large'>"+h+":"+m+":"+s+"</h1> <span class='dark'>"+d+",</span> <span class='dark'>"+mo+"</span> <span class='dark'>"+y+"</span>";
setTimeout (function() {
getTime();
}, 1000);
function correctDigit(i){
if(i < 10){
i = "0" + i;
return i;
}
};
};
</script>
</head>
<body onload="getTime()" class="color-4">
<section>
<div class='time color-0' id="time"></div>
<div>
<div class="news scroll color-1">
<ul>
<li class="ellipsis"><h4 class="dark"><i class="fa fa-newspaper-o"></i> Title 1</h4><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit,</p></li>
<li class="ellipsis"><h4 class="dark"><i class="fa fa-newspaper-o"></i> Title 2</h4><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit,</p></li>
<li class="ellipsis"><h4 class="dark"><i class="fa fa-newspaper-o"></i> Title 3</h4><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit,</p></li>
<li class="ellipsis"><h4 class="dark"><i class="fa fa-newspaper-o"></i> Title 4</h4><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit,</p></li>
</ul>
</div>