今天实现各出版社出版图书占比、每年图书出版数量、评价人数top10的图书名称的数据可视化效果。具体不详细讲了,可参考(四)https://blog.youkuaiyun.com/qq_45804925/article/details/113117424
1 各出版社出版图书占比
这个的图片太大了,没有截完图。
1.1 在cn.geo.doubanbook.entity包下创建Publisher.java类
package cn.geo.doubanbook.entity;
import java.io.Serializable;
/**
* 各出版社出版图书占比
* @author SGG
*
*/
public class Publisher implements Serializable{
private static final long serialVersionUID = 1L;
private String publisher;
private Integer num;
public Publisher() {
}
/**
* @param publisher
* @param num
*/
public Publisher(String publisher, Integer num) {
super();
this.publisher = publisher;
this.num = num;
}
@Override
public String toString() {
return "Publisher [publisher=" + publisher + ", num=" + num + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((num == null) ? 0 : num.hashCode());
result = prime * result + ((publisher == null) ? 0 : publisher.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;
Publisher other = (Publisher) obj;
if (num == null) {
if (other.num != null)
return false;
} else if (!num.equals(other.num))
return false;
if (publisher == null) {
if (other.publisher != null)
return false;
} else if (!publisher.equals(other.publisher))
return false;
return true;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public Integer getNum() {
return num;
}
public void setNum(Integer num) {
this.num = num;
}
}
1.2 在cn.geo.doubanbook.dao包下创建PublisherDAO.java类
package cn.geo.doubanbook.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import cn.geo.doubanbook.entity.Publisher;
import cn.geo.doubanbook.util.DBUtils;
/**
* 各出版社出版图书占比的持久层类
* @author SGG
*
*/
public class PublisherDAO {
/**
* 查询各出版社出版图书占比
* @return
* @throws SQLException
*/
public List<Publisher> listPublisher() throws SQLException {
List<Publisher> list = new ArrayList<Publisher>(248);
// 从数据库连接池获取连接
Connection conn = DBUtils.getConn();
// 声明SQL的执行器
Statement st = conn.createStatement();
// 执行SQL语句
String sql = "select * from book_publisher_num";
ResultSet rs = st.executeQuery(sql);
// 对结果集进行操作
while(rs.next()) {
// 获取该行数据中的指定字段
String publisher = rs.getString("publisher");
int num = rs.getInt("num");
// 创建Publisher对象,封装一行数据
Publisher pn= new Publisher(publisher, num);
// 将Publisher对象 保存到集合中
list.add(pn);
}
// 关闭连接释放资源
st.close();
conn.close();
return list;
}
}
1.3 持久层测试用例开发
package cn.geo.doubanbook.dao;
import java.sql.SQLException;
import java.util.List;
import org.junit.Test;
import cn.geo.doubanbook.dao.PublisherDAO;
import cn.geo.doubanbook.entity.Publisher;
public class PublisherDAOTest {
PublisherDAO dao = new PublisherDAO();
/**
* 测试PublisherDAO中的listPublisher方法中的方法
* @throws SQLException
*/
@Test
public void listPublisher() throws SQLException{
List<Publisher> list = dao.listPublisher();
list.forEach(item->System.out.println(item));
}
}
1.4 在cn.geo.doubanbook.entity包下创建PublisherVO.java类
package cn.geo.doubanbook.entity;
import java.io.Serializable;
import java.util.List;
public class PublisherVO implements Serializable{
private static final long serialVersionUID = -6643160296906955532L;
private List<String> xData;
private List<Integer> yData;
private List<String> pieData;
public PublisherVO() {
}
/**
* @param xData
* @param yData
* @param pieData
*/
public PublisherVO(List<String> xData, List<Integer> yData, List<String> pieData) {
super();
this.xData = xData;
this.yData = yData;
this.pieData = pieData;
}
@Override
public String toString() {
return "PublisherVO [xData=" + xData + ", yData=" + yData + ", pieData=" + pieData + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((pieData == null) ? 0 : pieData.hashCode());
result = prime * result + ((xData == null) ? 0 : xData.hashCode());
result = prime * result + ((yData == null) ? 0 : yData.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;
PublisherVO other = (PublisherVO) obj;
if (pieData == null) {
if (other.pieData != null)
return false;
} else if (!pieData.equals(other.pieData))
return false;
if (xData == null) {
if (other.xData != null)
return false;
} else if (!xData.equals(other.xData))
return false;
if (yData == null) {
if (other.yData != null)
return false;
} else if (!yData.equals(other.yData))
return false;
return true;
}
public List<String> getxData() {
return xData;
}
public void setxData(List<String> xData) {
this.xData = xData;
}
public List<Integer> getyData() {
return yData;
}
public void setyData(List<Integer> yData) {
this.yData = yData;
}
public List<String> getPieData() {
return pieData;
}
public void setPieData(List<String> pieData) {
this.pieData = pieData;
}
}
1.5 在cn.geo.doubanbook.service包下创建PublisherService.java类
package cn.geo.doubanbook.service;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.geo.doubanbook.dao.PublisherDAO;
import cn.geo.doubanbook.entity.Publisher;
import cn.geo.doubanbook.entity.PublisherVO;
public class PublisherService {
private PublisherDAO dao = new PublisherDAO();
public PublisherVO findPublisher() {
// 调用持久层方法,查询所需数据
List<Publisher> list = null;
try {
list = dao.listPublisher();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
// 创建xData,保存x轴数据
List<String> xData