mysql tomcat列表增删改查_Tomcat-Database
Tomcat-Database
介紹
根據(jù)Tomcat9源碼二次開發(fā),增加數(shù)據(jù)庫功能,可一行代碼實(shí)現(xiàn)CURD
安裝教程
需要先配置Ant,然后在根目錄下執(zhí)行ant命令,即可編譯,編譯后的目錄在Tomcat-Database/output/build下,其中在bin下可直接執(zhí)行./startup.sh啟動(dòng)
使用說明
其實(shí)這也不算擴(kuò)展吧,只是改了小小小的一部分,先來展示下怎么使用。
eclipse
如果要在Eclipse中使用,務(wù)必添加二次開發(fā)后的Tomcat路徑(文章后有鏈接)。
此處也要換成二次開發(fā)后的,不然后續(xù)用到的類會(huì)找不到。
好,看看怎么一行代碼獲取表中所有記錄。
首先要在init(ServletConfig config)方法中拿到HTomcatPal對象,這是唯一的寫法,你不可能通過new創(chuàng)建。
@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 result = hTomcatPal.findAll(BookEntity.class);
request.setAttribute("data", result);
request.getRequestDispatcher("bookList.jsp").forward(request, response);
}
在看看jsp中通過jstl簡單渲染一下。
pageEncoding="UTF-8"%>
圖書列表書名
作者
搜索
ID 書名
作者 價(jià)格
class="row-item">描述
- ${item.getId()}
class="row-item flex-2">${item.getBookName() }
class="row-item">${item.getBookAuthor() }
class="row-item">${item.getBookMoney() }
class="row-item">${item.getBookDesc() }
效果就出來了,是不是非常簡單?
當(dāng)然,這里最重要的是在實(shí)體上加入注解@TabName,并指明表名,如果是主鍵,則要加上@PrimaryKey注解。如果遵守字段命名規(guī)范,則可以不用加@FieldName標(biāo)明對應(yīng)數(shù)據(jù)庫字段是什么。這里的規(guī)范就是如果Java字段是bookName,則數(shù)據(jù)庫字段是book_name,在比如bookAuthor,對應(yīng)數(shù)據(jù)庫字段就是book_author,如果不是這樣的規(guī)范,則需要加入@FieldName注解標(biāo)明。
另外,一定要留一個(gè)空構(gòu)造方法。
@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要連接的數(shù)據(jù)庫信息呢?當(dāng)然,這是必須的,我們可以直接在web.xml中進(jìn)行配置,無需創(chuàng)建額外文件,如下的幾個(gè)標(biāo)簽,其中core-connect是數(shù)據(jù)庫連接池的大小。
當(dāng)Tomcat啟動(dòng)后,會(huì)打印出這些配置。
DynamicWebDemo-Dababase
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
15
jdbc:mysql://localhost:3306/test
root
xxxxx
另外,源碼中已經(jīng)加入了mysql-connector-java-8.0.19,小伙伴無需在導(dǎo)入mysql驅(qū)動(dòng)jar,這聽起來是不是更方便了呢?
IDEA
同樣在idea中也要先配置二次開發(fā)后的Tomcat,如果在Idea下找不到HTomcatPal,則需要在這個(gè)界面添加database.jar。database則是所寫的簡易數(shù)據(jù)庫框架,原路徑在org.apache.database,配置ant后會(huì)生成database.jar。配置后使用方法同上。
三、API
當(dāng)然少不了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 entities = tomcatPal.findAll(BookEntity.class, new Condition.Builder()
.whereGt("book_price", 50)
.builder());
System.out.println(entities);
此時(shí)列舉這么多,主要的類就是Condition(條件)、HTomcatPal。
總結(jié)
以上是生活随笔為你收集整理的mysql tomcat列表增删改查_Tomcat-Database的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: fstream下的读写操作
- 下一篇: redhat linux 6.5 vnc