python爬虫豆瓣读书top250+数据清洗+数据库+Java后端开发+Echarts数据可视化(六)

今天实现各出版社出版图书占比、每年图书出版数量、评价人数top10的图书名称的数据可视化效果。具体不详细讲了,可参考(四)https://blog.csdn.net/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 = new ArrayList<String>(list.size());
		// 创建yData,保存y轴数据
		List<Integer> yData = new ArrayList<Integer>(list.size());
		// 创建pieData
		List<String> pieData = new ArrayList<String>(list.size());
		// 遍历持久层查询到的数据
		for(Publisher pn: list) {
			xData.add(pn.getPublisher());
			yData.add(pn.getNum());
			pieData.add(pn.getPublisher() + pn.getNum());
		}
		// 创建PublisherVO对象,封装xData和yData
		PublisherVO vo = new PublisherVO(xData, yData, pieData);
		return vo;
	}
}

1.6 业务层测试用例开发

package cn.geo.doubanbook.service;

import org.junit.Test;

import cn.geo.doubanbook.entity.PublisherVO;

public class PublisherServiceTest {

	PublisherService service = new PublisherService();
	
	@Test
	public void findPresstime() {
		PublisherVO vo = service.findPublisher();
		System.out.println(vo);
	}
}

1.7 Web层开发

package cn.geo.doubanbook.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.geo.doubanbook.entity.PublisherVO;
import cn.geo.doubanbook.service.PublisherService;

public class PublisherServlet extends HttpServlet {

	private static final long serialVersionUID = -1209762678671446112L;
	
	private PublisherService service = new PublisherService();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 调动业务层方法,获取PresstimeVO
		PublisherVO vo = service.findPublisher();
		// 判断PresstimeVO是否不为null
		if (vo != null) {
			// 将vo对象转变成JSON字符串-基于JSON插件实现
			String jsonStr = JSON.toJSONString(vo);
			// 通知浏览器,本次返回的数据是JSON格式
			resp.setContentType("application/json;charset=utf-8");
			// 将JSON字符串添加到response对象中
			resp.getWriter().write(jsonStr);
		} else {
			// 返回空的json字符串
			// 通知浏览器,本次返回的数据是JSON格式
			resp.setContentType("application/json;charset=utf-8");
			// 将JSON字符串添加到response对象中
			resp.getWriter().write("{}");
		}
	}
}

1.8 在webapp/WEB-INF/web.xml文件中对Servlet进行配置

<servlet>
		<servlet-name>PublisherServlet</servlet-name>
		<servlet-class>cn.geo.doubanbook.web.PublisherServlet</servlet-class>	
</servlet>
<servlet-mapping>
		<servlet-name>PublisherServlet</servlet-name>
		<url-pattern>/publisher</url-pattern>
</servlet-mapping>

1.9 前端页面开发

在webapp根目录下,创建publisherNum.html文件。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>各出版社出版图书占比</title>
<script src="js/echarts.min.js"></script>
<script src="js/jquery-1.11.0.min.js"></script>
</head>
<body>

	<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
    <div id="publisher" style="width: 1300px;height:1700px;"></div>
    <script type="text/javascript">
    
 		// 声明服务器数据的url
    	var url = "http://localhost:8080/doubanbook/publisher";
 		
    	
    	// 发送Ajax请求,从服务器获取数据
		$.get(url, function(result) {
			// x轴数据: 出版社
			var xData = result.xData;
			// y轴数据: 数量
			var yData = result.yData;
			
			var pieData = [];	
			for(var index in xData){
				// 从xData和yData中获取元素,生成
			    // pieData中的一个数据
			    var obj={
			    	value : yData[index],
			        name : xData[index]
			    };
			    // 将生成的数据添加到pieData中
			    pieData.push(obj);
			}
	    	
			// 基于准备好的dom,初始化echarts实例
	        var myChart = echarts.init(document.getElementById('publisher'));
			
	     	// 指定图表的配置项和数据
	        var option = {
	        	// 图表标题
	            title: {
	                text: '各出版社出版图书占比'
	            },
	            // 提示框
	            tooltip : {},
	            // 图例
	            legend: {
	                data:['出版量']
	            },
	            //工具栏组件
	            toolbox: {   
	                show: true,
	                feature:{  //需要的功能
	                    saveAsImage: {
	                        show: true	//保存为图片
	                    }, 
	                    dataView: {
	                        show: true	//数据视图         
	                    },
	                    dataZoom: {
	                        show: true	//区域缩放与区域缩放还原            
	                    },
	                    magicType: {
	                        type: ['line', 'bar']	//动态类型转换       
	                    }
	                }
	            },
	            // x轴
	            //xAxis: {
	            //	data : xData
	            //},
	            // y轴
	            //yAxis: {},
	            // 系列列表
	            series: [{
	            	name: '各出版社出版图书占比',
	                type: 'pie',
	                radius: '45%',
	                center: ['50%', '50%'],// 水平位置,垂直位置
	                roseType: 'angle',
	                data: pieData.sort(function (a, b) { 
	                	return a.value - b.value; 
	                }),
	                itemStyle:{ 
		               	normal:{ 
		                   	label:{ 
		                    	 show: true, 
		                     	 formatter: '{b} : {c} ({d}%)' 
		                   	}, 
		                   	labelLine :{show:true}
		               	} 
		            } 
	            }]
	        };
	     	// 使用刚指定的配置项和数据显示图表
	        myChart.setOption(option);
		});
	</script>
	
</body>
</html>

2 每年图书出版数量

在这里插入图片描述

2.1 在cn.geo.doubanbook.entity包下创建Presstime.java类

package cn.geo.doubanbook.entity;

import java.io.Serializable;

public class Presstime implements Serializable{

	private static final long serialVersionUID = -6791541892812500199L;
	
	private Integer presstime;
	private Integer num;
	
	public Presstime() {	}
	/**
	 * @param presstime
	 * @param num
	 */
	public Presstime(Integer presstime, Integer num) {
		super();
		this.presstime = presstime;
		this.num = num;
	}
	@Override
	public String toString() {
		return "Presstime [presstime=" + presstime + ", num=" + num + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((num == null) ? 0 : num.hashCode());
		result = prime * result + ((presstime == null) ? 0 : presstime.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;
		Presstime other = (Presstime) obj;
		if (num == null) {
			if (other.num != null)
				return false;
		} else if (!num.equals(other.num))
			return false;
		if (presstime == null) {
			if (other.presstime != null)
				return false;
		} else if (!presstime.equals(other.presstime))
			return false;
		return true;
	}
	public Integer getPresstime() {
		return presstime;
	}
	public void setPresstime(Integer presstime) {
		this.presstime = presstime;
	}
	public Integer getNum() {
		return num;
	}
	public void setNum(Integer num) {
		this.num = num;
	}
}

2.2 在cn.geo.doubanbook.dao包下创建PresstimeDAO.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.Presstime;
import cn.geo.doubanbook.util.DBUtils;
/**
 * 每年图书上榜数量的持久层类
 * @author SGG
 *
 */
public class PresstimeDAO {

	/**
	 * 查询每年图书上榜数量
	 * @return
	 * @throws SQLException
	 */
	public List<Presstime> listPresstime() throws SQLException {
		List<Presstime> list = new ArrayList<Presstime>(248);
		
		// 从数据库连接池获取连接
		Connection conn = DBUtils.getConn();
		// 声明SQL的执行器
		Statement st = conn.createStatement();
		// 执行SQL语句
		String sql = "select * from book_presstime_num";
		ResultSet rs = st.executeQuery(sql);
		// 对结果集进行操作
		while(rs.next()) {
			// 获取该行数据中的指定字段
			int presstime = rs.getInt("press_time");
			int num = rs.getInt("num");
			// 创建Presstime对象,封装一行数据
			Presstime pn= new Presstime(presstime, num);
			// 将Presstime对象 保存到集合中
			list.add(pn);
		}
		// 关闭连接释放资源
		st.close();
		conn.close();
		
		return list;	
	}
}

2.3 持久层测试用例开发

package cn.geo.doubanbook.dao;

import java.sql.SQLException;
import java.util.List;

import org.junit.Test;

import cn.geo.doubanbook.dao.PresstimeDAO;
import cn.geo.doubanbook.entity.Presstime;

public class PresstimeDAOTest {

	PresstimeDAO dao = new PresstimeDAO();
	/**
	 * 测试PresstimeDAO中的listPresstime方法中的方法
	 * @throws SQLException
	 */
	@Test
	public void listPresstime() throws SQLException{
		List<Presstime> list = dao.listPresstime();
		list.forEach(item->System.out.println(item));
	}
}

2.4 在cn.geo.doubanbook.entity包下创建PresstimeVO.java类

package cn.geo.doubanbook.entity;

import java.io.Serializable;
import java.util.List;

public class PresstimeVO implements Serializable{

	private static final long serialVersionUID = 969617548407625473L;
	
	private List<Integer> xData;	
	private List<Integer> yData;
	
	public PresstimeVO() { }
	/**
	 * @param xData
	 * @param yData
	 */
	public PresstimeVO(List<Integer> xData, List<Integer> yData) {
		super();
		this.xData = xData;
		this.yData = yData;
	}
	@Override
	public String toString() {
		return "PresstimeVO [xData=" + xData + ", yData=" + yData + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		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;
		PresstimeVO other = (PresstimeVO) obj;
		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<Integer> getxData() {
		return xData;
	}
	public void setxData(List<Integer> xData) {
		this.xData = xData;
	}
	public List<Integer> getyData() {
		return yData;
	}
	public void setyData(List<Integer> yData) {
		this.yData = yData;
	}
}

2.5 在cn.geo.doubanbook.service包下创建PresstimeService.java类

package cn.geo.doubanbook.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.geo.doubanbook.dao.PresstimeDAO;
import cn.geo.doubanbook.entity.Presstime;
import cn.geo.doubanbook.entity.PresstimeVO;

public class PresstimeService {

	private PresstimeDAO dao = new PresstimeDAO();
	public PresstimeVO findPresstime() {
		// 调用持久层方法,查询所需数据
		List<Presstime> list = null;
		try {
			list = dao.listPresstime();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		// 创建xData,保存x轴数据
		List<Integer> xData = new ArrayList<Integer>(list.size());
		// 创建yData,保存y轴数据
		List<Integer> yData = new ArrayList<Integer>(list.size());
		// 遍历持久层查询到的数据
		for(Presstime pn: list) {
			xData.add(pn.getPresstime());
			yData.add(pn.getNum());
		}
		// 创建PresstimeVO对象,封装xData和yData
		PresstimeVO vo = new PresstimeVO(xData, yData);
		return vo;
	}
}

2.6 业务层测试用例开发

package cn.geo.doubanbook.service;

import org.junit.Test;

import cn.geo.doubanbook.entity.PresstimeVO;

public class PresstimeServiceTest {

	PresstimeService service = new PresstimeService();
	
	@Test
	public void findPresstime() {
		PresstimeVO vo = service.findPresstime();
		System.out.println(vo);
	}
}

2.7 Web层开发

package cn.geo.doubanbook.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.geo.doubanbook.entity.PresstimeVO;
import cn.geo.doubanbook.service.PresstimeService;

public class PresstimeServlet extends HttpServlet {

	private static final long serialVersionUID = -8563191745832048206L;

	private PresstimeService service = new PresstimeService();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 调动业务层方法,获取PresstimeVO
		PresstimeVO vo = service.findPresstime();
		// 判断PresstimeVO是否不为null
		if (vo != null) {
			// 将vo对象转变成JSON字符串-基于JSON插件实现
			String jsonStr = JSON.toJSONString(vo);
			// 通知浏览器,本次返回的数据是JSON格式
			resp.setContentType("application/json;charset=utf-8");
			// 将JSON字符串添加到response对象中
			resp.getWriter().write(jsonStr);
		} else {
			// 返回空的json字符串
			// 通知浏览器,本次返回的数据是JSON格式
			resp.setContentType("application/json;charset=utf-8");
			// 将JSON字符串添加到response对象中
			resp.getWriter().write("{}");
		}
	}
}

2.8 在webapp/WEB-INF/web.xml文件中对Servlet进行配置

<servlet>
		<servlet-name>PresstimeServlet</servlet-name>
		<servlet-class>cn.geo.doubanbook.web.PresstimeServlet</servlet-class>	
</servlet>
<servlet-mapping>
		<servlet-name>PresstimeServlet</servlet-name>
		<url-pattern>/presstime</url-pattern>
</servlet-mapping>

2.9 前端页面开发

在webapp根目录下,创建presstimeNum.html文件。

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>每年图书出版数量</title>
<script src="js/echarts.min.js"></script>
<script src="js/jquery-1.11.0.min.js"></script>
</head>
<body>

	<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
	<div id="presstime" style="width: 1300px;height:500px;"></div>
	<script type="text/javascript">
		// 声明服务器数据的url
		var url = "http://localhost:8080/doubanbook/presstime";
		// 发送Ajax请求,从服务器获取数据
		$.get(url, function(result) {
			// x轴数据: 出版年份
			var xData = result.xData;
			// y轴数据: 数量
			var yData = result.yData;

			// 基于准备好的dom,初始化echarts实例
			var myChart = echarts.init(document.getElementById('presstime'));

			// 指定图表的配置项和数据
			var option = {
				// 图表标题
				title : {
					text : '每年图书出版数量'
				},
				// 提示框
				tooltip : {
					trigger : 'axis',
					axisPointer : {
						type : 'cross',
						label : {
							backgroundColor : '#6a7985'
						}
					}
				},
				// 图例
				legend : {
					data : [ '出版量' ]
				},
				//工具栏组件
				toolbox : {
					show : true,
					feature : { //需要的功能
						saveAsImage : {
							show : true
						//保存为图片
						},
						dataView : {
							show : true
						//数据视图         
						},
						dataZoom : {
							show : true
						//区域缩放与区域缩放还原            
						},
						magicType : {
							type : [ 'line', 'bar' ]
						//动态类型转换       
						}
					}
				},
				// x轴
				xAxis : {
					data : xData,
				},
				// y轴
				yAxis : {},
				// 系列列表
				series : [ {
					name : '出版量',
					type : 'line',
					data : yData,
					smooth : true,
					symbol : 'none',
					stack : 'a',
					// 曲线图区域颜色设置
					areaStyle : {
						normal : {
							color : 'purple' //改变区域颜色
						}
					},
					// 曲线图线条颜色设置
					itemStyle : {
						normal : {
							color : 'red', //改变折线点的颜色
							lineStyle : {
								color : '#FCCE10' //改变折线颜色
							}
						}
					},
					// 最大值、最小值标注
					markPoint : {
						data : [ {
							type : 'max',
							name : '最大值'
						}, {
							type : 'min',
							name : '最小值'
						} ]
					},
					// 平均值标注
					markLine : {
						data : [ {
							type : 'average',
							name : '平均值',
							color : 'green'
						} ]
					}
				} ]
			};
			// 使用刚指定的配置项和数据显示图表
			myChart.setOption(option);
		});
	</script>
</body>
</html>

3 评价人数top10的图书名称

在这里插入图片描述

3.1 在cn.geo.doubanbook.entity包下创建People10.java类

package cn.geo.doubanbook.entity;

import java.io.Serializable;

/**
 * 评价人数top10的图书名称
 * @author SGG
 *
 */
public class People10 implements Serializable {

	private static final long serialVersionUID = -376413041133275545L;
	
	private String title;
	private Integer people;

	public People10() { }
	/**
	 * @param title
	 * @param people
	 */
	public People10(String title, Integer people) {
		super();
		this.title = title;
		this.people = people;
	}
	@Override
	public String toString() {
		return "People10 [title=" + title + ", people=" + people + "]";
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((people == null) ? 0 : people.hashCode());
		result = prime * result + ((title == null) ? 0 : title.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;
		People10 other = (People10) obj;
		if (people == null) {
			if (other.people != null)
				return false;
		} else if (!people.equals(other.people))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Integer getPeople() {
		return people;
	}
	public void setPeople(Integer people) {
		this.people = people;
	}
}

3.2 在cn.geo.doubanbook.dao包下创建People10DAO.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.People10;
import cn.geo.doubanbook.util.DBUtils;

/**
 * 评价人数top10的图书名称的持久层类
 * @author SGG
 *
 */
public class People10DAO {

	/**
	 * 查询评价人数top10的图书名称
	 * @return
	 * @throws SQLException
	 */
	public List<People10> listPeople10() throws SQLException {
		List<People10> list = new ArrayList<People10>(248);
		
		// 从数据库连接池获取连接
		Connection conn = DBUtils.getConn();
		// 声明SQL的执行器
		Statement st = conn.createStatement();
		// 执行SQL语句
		String sql = "select * from book_people_title";
		ResultSet rs = st.executeQuery(sql);
		// 对结果集进行操作
		while(rs.next()) {
			// 获取该行数据中的指定字段
			String title = rs.getString("title");
			int people = rs.getInt("people");
			// 创建People10对象,封装一行数据
			People10 pt = new People10(title, people);
			// 将People10对象 保存到集合中
			list.add(pt);
		}
		// 关闭连接释放资源
		st.close();
		conn.close();
		
		return list;	
	}
}

3.3 持久层测试用例开发

package cn.geo.doubanbook.dao;

import java.sql.SQLException;
import java.util.List;

import org.junit.Test;

import cn.geo.doubanbook.dao.People10DAO;
import cn.geo.doubanbook.entity.People10;

public class People10DAOTest {

	People10DAO dao = new People10DAO();
	/**
	 * 测试PresstimeDAO中的listPresstime方法中的方法
	 * @throws SQLException
	 */
	@Test
	public void listPeople10() throws SQLException{
		List<People10> list = dao.listPeople10();
		list.forEach(item->System.out.println(item));
	}
}

3.4 在cn.geo.doubanbook.entity包下创建People10VO.java类

package cn.geo.doubanbook.entity;

import java.io.Serializable;
import java.util.List;

public class People10VO implements Serializable{

	private static final long serialVersionUID = 2219034749411653039L;
	

	private List<String> xData;
	private List<Integer> yData;
	private List<String> pieData;
	public People10VO() {}
	/**
	 * @param xData
	 * @param yData
	 * @param pieData
	 */
	public People10VO(List<String> xData, List<Integer> yData, List<String> pieData) {
		super();
		this.xData = xData;
		this.yData = yData;
		this.pieData = pieData;
	}
	@Override
	public String toString() {
		return "People10VO [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;
		People10VO other = (People10VO) 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;
	}
}

3.5 在cn.geo.doubanbook.service包下创建People10Service.java类

package cn.geo.doubanbook.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.geo.doubanbook.dao.People10DAO;
import cn.geo.doubanbook.entity.People10;
import cn.geo.doubanbook.entity.People10VO;

public class People10Service {

	private People10DAO dao = new People10DAO();

	public People10VO findPeople10() {
		// 调用持久层方法,查询所需数据
		List<People10> list = null;
		try {
			list = dao.listPeople10();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
		// 创建xData,保存x轴数据
		List<String> xData = new ArrayList<String>(list.size());
		// 创建yData,保存y轴数据
		List<Integer> yData = new ArrayList<Integer>(list.size());
		// 创建pieData
		List<String> pieData = new ArrayList<String>(list.size());
		// 遍历持久层查询到的数据
		for (People10 p : list) {
			xData.add(p.getTitle());
			yData.add(p.getPeople());
			pieData.add(p.getTitle() + p.getPeople());
		}
		// 创建People10对象,封装xData和yData
		People10VO vo = new People10VO(xData, yData, pieData);
		return vo;
	}
}

3.6 业务层测试用例开发

package cn.geo.doubanbook.service;

import org.junit.Test;

import cn.geo.doubanbook.entity.People10VO;

public class People10ServiceTest {

	People10Service service = new People10Service();
	
	@Test
	public void findPeople10() {
		People10VO vo = service.findPeople10();
		System.out.println(vo);
	}
}

3.7 Web层开发

package cn.geo.doubanbook.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.geo.doubanbook.entity.People10VO;
import cn.geo.doubanbook.service.People10Service;

public class People10Servlet extends HttpServlet {

	private static final long serialVersionUID = 3593041906617605523L;

	private People10Service service = new People10Service();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 调动业务层方法,获取People10VO
		People10VO vo = service.findPeople10();
		// 判断People10VO是否不为null
		if (vo != null) {
			// 将vo对象转变成JSON字符串-基于JSON插件实现
			String jsonStr = JSON.toJSONString(vo);
			// 通知浏览器,本次返回的数据是JSON格式
			resp.setContentType("application/json;charset=utf-8");
			// 将JSON字符串添加到response对象中
			resp.getWriter().write(jsonStr);
		} else {
			// 返回空的json字符串
			// 通知浏览器,本次返回的数据是JSON格式
			resp.setContentType("application/json;charset=utf-8");
			// 将JSON字符串添加到response对象中
			resp.getWriter().write("{}");
		}
	}
}

3.8 在webapp/WEB-INF/web.xml文件中对Servlet进行配置:

<servlet>
		<servlet-name>People10Servlet</servlet-name>
		<servlet-class>cn.geo.doubanbook.web.People10Servlet</servlet-class>	
</servlet>
<servlet-mapping>
		<servlet-name>People10Servlet</servlet-name>
		<url-pattern>/people10</url-pattern>
</servlet-mapping>

3.9 前端页面开发

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>评价人数top10的图书名称</title>
<script src="js/echarts.min.js"></script>
<script src="js/jquery-1.11.0.min.js"></script>
</head>
<body>
	
	<!-- 为ECharts准备一个具备大小(宽高)的Dom -->
    <div id="people10" style="width: 1300px;height:500px;"></div>
    <script type="text/javascript">
    
 		// 声明服务器数据的url
    	var url = "http://localhost:8080/doubanbook/people10";
 		    	
    	// 发送Ajax请求,从服务器获取数据
		$.get(url, function(result) {
			// x轴数据: 出版社
			var xData = result.xData;
			// y轴数据: 数量
			var yData = result.yData;
			
			var pieData = [];	
			for(var index in xData){
				// 从xData和yData中获取元素,生成
			    // pieData中的一个数据
			    var obj={
			    	value : yData[index],
			        name : xData[index]
			    };
			    // 将生成的数据添加到pieData中
			    pieData.push(obj);
			}
	    	
			// 基于准备好的dom,初始化echarts实例
	        var myChart = echarts.init(document.getElementById('people10'));
			
	     	// 指定图表的配置项和数据
	        var option = {
	        	// 图表标题
	            title: {
	                text: '评价人数top10的图书名称'
	            },
	            // 提示框
	            tooltip : {},
	            // 图例
	            legend: {
	                data:['评价人数top10']
	            },
	            //工具栏组件
	            toolbox: {   
	                show: true,
	                feature:{  //需要的功能
	                    saveAsImage: {
	                        show: true	//保存为图片
	                    }, 
	                    dataView: {
	                        show: true	//数据视图         
	                    },
	                    dataZoom: {
	                        show: true	//区域缩放与区域缩放还原            
	                    },
	                    magicType: {
	                        type: ['line', 'bar']	//动态类型转换       
	                    }
	                }
	            },	         
	            // 系列列表
	            series: [{
	            	name: '各出版社出版图书占比',
	                type: 'pie',
	                radius: '45%',
	                center: ['50%', '50%'],// 水平位置,垂直位置
	                roseType: 'angle',
	                data: pieData.sort(function (a, b) { 
	                	return a.value - b.value; 
	                }),
	                itemStyle:{ 
		               	normal:{ 
		                   	label:{ 
		                    	 show: true, 
		                     	 formatter: '{b} : {c} ({d}%)' 
		                   	}, 
		                   	labelLine :{show:true}
		               	} 
		            } 
	            }]
	        };
	     	// 使用刚指定的配置项和数据显示图表
	        myChart.setOption(option);
		});
	</script>
</body>
</html>

4 pom.xml

截图看一下pom.xml文件中都有哪些内容。
在这里插入图片描述

5 web.xml

截图看一下web.xml文件中都有哪些内容。
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值