Tomcat源码解析五部曲终章、扩展源码

一、前言

距离前几篇Tomcat源码文章已经过去两个月了,但迟迟没有写做后一篇,直到今天,中途打算写Tomcat优化或者Cookie、Session原理,最后还是放弃了,原因是这类文章非常多,不想写了。正好前几个星期开学,并且有一门JavaWeb课,所以想的是做一个简单的数据库框架,和Tomcat源码整合一下,也就是扩展Tomcat源码,让他让他轻松实现CRUD。最终可以轻松应付各种毕设、作业。但由于又去学习其他东西,所以这个也耽误了些时间。

二、用法示例

其实这也不算扩展吧,只是改了小小小的一部分,先来展示下怎么使用。

eclipse

如果要在Eclipse中使用,务必添加二次开发后的Tomcat路径(文章后有链接)。
在这里插入图片描述
此处也要换成二次开发后的,不然后续用到的类会找不到。
在这里插入图片描述
好,看看怎么一行代码获取表中所有记录。

首先要在init(ServletConfig config)方法中拿到HTomcatPal对象,这是唯一的写法,你不可能通过new创建。

@WebServlet("/BookServlet")
public class BookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
 	private HTomcatPal hTomcatPal;
	
 
 	@Override
 	public void init(ServletConfig config) throws ServletException {
 		/**
 		 * 获取BookDao,只能通过此方式
 		 */
 		hTomcatPal =	(HTomcatPal)config.getServletContext().getAttribute("AZY");
 	}

    public BookServlet() {
        super();

    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<BookEntity> result = hTomcatPal.findAll(BookEntity.class);
		request.setAttribute("data", result);
		request.getRequestDispatcher("bookList.jsp").forward(request, response);
		
	}

在看看jsp中通过jstl简单渲染一下。

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书列表</title>
</head>
<link href="static/css/books.css" rel="stylesheet" type="text/css" />
<body>
	<div class="container books-list">
		<form action="BookServlet" method="post">
			<input name="content" />
			<select name="type">
				<option>书名</option>
				<option>作者</option>
			</select>
			<button type="submit">搜索</button>
		</form>
		<header>
			<span class="row-item">ID</span> <span class="row-item flex-2">书名</span>
			<span class="row-item">作者</span> <span class="row-item">价格</span> <span
				class="row-item">描述</span>
		</header>

		<ul>
			<c:forEach items="${data}" var="item">

				<li><span class="row-item">${item.getId()} </span> <span
					class="row-item flex-2">${item.getBookName() }</span> <span
					class="row-item">${item.getBookAuthor() }</span> <span
					class="row-item">${item.getBookMoney() }</span> <span
					class="row-item">${item.getBookDesc() }</span></li>


			</c:forEach>
		</ul>
	</div>
</body>
</html>

效果就出来了,是不是非常简单?
在这里插入图片描述
当然,这里最重要的是在实体上加入注解@TabName,并指明表名,如果是主键,则要加上@PrimaryKey注解。如果遵守字段命名规范,则可以不用加@FieldName标明对应数据库字段是什么。这里的规范就是如果Java字段是bookName,则数据库字段是book_name,在比如bookAuthor,对应数据库字段就是book_author,如果不是这样的规范,则需要加入@FieldName注解标明。

另外,一定要留一个空构造方法。

@TabName("tb_books")
public class BookEntity {
	@PrimaryKey
	private int id;
	private String bookName;
	private String bookAuthor;
	private BigDecimal bookPrice;

	@FieldName("book_describ")
	private String bookDesc;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getBookName() {
		return bookName;
	}
	public void setBookName(String bookName) {
		this.bookName = bookName;
	}
	public String getBookAuthor() {
		return bookAuthor;
	}
	public void setBookAuthor(String bookAuthor) {
		this.bookAuthor = bookAuthor;
	}
	public BigDecimal getBookMoney() {
		return bookPrice;
	}
	public void setBookMoney(BigDecimal bookMoney) {
		this.bookPrice = bookMoney;
	}
	public String getBookDesc() {
		return bookDesc;
	}
	public void setBookDesc(String bookDesc) {
		this.bookDesc = bookDesc;
	}

	public BookEntity(int id, String bookName, String bookAuthor, BigDecimal bookPrice, String bookDesc) {
		this.id = id;
		this.bookName = bookName;
		this.bookAuthor = bookAuthor;
		this.bookPrice = bookPrice;
		this.bookDesc = bookDesc;
	}

	public BookEntity() {
	}

	@Override
	public String toString() {
		return "BookEntity{" +
				"id=" + id +
				", bookName='" + bookName + '\'' +
				", bookAuthor='" + bookAuthor + '\'' +
				", bookPrice=" + bookPrice +
				", bookDesc='" + bookDesc + '\'' +
				'}';
	}
}


但是,是不是需要告诉Tomcat要连接的数据库信息呢?当然,这是必须的,我们可以直接在web.xml中进行配置,无需创建额外文件,如下的几个标签,其中core-connect是数据库连接池的大小。

当Tomcat启动后,会打印出这些配置。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>DynamicWebDemo-Dababase</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  
  <core-connect>15</core-connect>
  <connect-url>jdbc:mysql://localhost:3306/test</connect-url>
  <user-name>root</user-name>
  <user-pass>xxxxx</user-pass>
</web-app>

另外,源码中已经加入了mysql-connector-java-8.0.19,小伙伴无需在导入mysql驱动jar,这听起来是不是更方便了呢?

IDEA

同样在idea中也要先配置二次开发后的Tomcat,如果在Idea下找不到HTomcatPal,则需要在这个界面添加database.jar。database则是所写的简易数据库框架,原路径在org.apache.database,配置ant后会生成database.jar。配置后使用方法同上。
在这里插入图片描述

三、API

当然少不了API的使用,这里提供了简单的CRUD。

插入

  BookEntity entity =new BookEntity();
  for (int i = 0; i < 100; i++) {
      entity.setBookAuthor("作者"+i);
      entity.setBookDesc("描述"+i);
      entity.setBookMoney(new BigDecimal(""+i));
      entity.setBookName("书名"+i);
      tomcatPal.insert(entity);
  }

删除

 /**
  * 删除ID为26580,26581,26583
  */
 tomcatPal.deleteIds(BookEntity.class,26580,26581,26583);
 /**
  * 删除作者为’作者35‘的记录
  */
 tomcatPal.deleteByCondition(BookEntity.class,new Condition.Builder()
         .whereEq("book_author","作者35")
         .builder());

更具条件查找

  /**
   *查找金额大于50的记录
   */
  List<BookEntity> entities = tomcatPal.findAll(BookEntity.class, new Condition.Builder()
          .whereGt("book_price", 50)
          .builder());
  System.out.println(entities);

此时列举这么多,主要的类就是Condition(条件)、HTomcatPal。

四、下载地址

https://www.houxinlin.com/static/tomcat-database-9.zip

五、后续

后续会继续更新,因为好多没有完善,但是对于作业这些已经足够,有好想法的小伙伴可以和我一起讨论哦。

本来想的介绍原理,但是实在想打羽毛球去了,就算了,源码本身也比较简单,在后续会给出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值