介紹
項目採用B/S架構、MVC開發模式,使用java語言,根據Java EE 標準開發,旨在感覺java web項目開發流程、學習與理解java EE基礎技術和原理。javascript
M: Model模型 JavaBean
V:view視圖 Html
C:Controller控制器 Servletcss
其中Html與Servlet之間用Ajax與Json技術互動。html
項目結構
使用maven構建,分爲Dao、Service、Servlet、和前端展現層前端
數據持久化工具
使用C3p0數據庫鏈接池java
C3P0配置文件出c3p0-config.xml:mysql
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/數據庫名?serverTimezone=Asia/Shanghai</property> <property name="user">用戶名</property> <property name="password">密碼</property> <!-- 初始化鏈接的數量 --> <property name="initialPoolSize">10</property> <!-- 最大空閒時間,單位是秒 --> <property name="maxIdleTime">1</property> <!-- 池中最大鏈接的數量 --> <property name="maxPoolSize">100</property> <!-- 池中最小鏈接的數量 --> <property name="minPoolSize">10</property> </default-config> </c3p0-config>
C3P0Util:web
package util; import com.mchange.v2.c3p0.ComboPooledDataSource; import java.sql.Connection; import java.sql.SQLException; /** * C3P0 */ public class C3P0Util { // 獲得一個c3p0的數據源 private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); // 從數據源中獲得一個鏈接對象 // 這個返回的connection其實是c3p0通過裝飾以後的connection public static Connection getConnection() { try { return dataSource.getConnection(); } catch (SQLException e) { throw new RuntimeException("服務器錯誤"); } } //查看鏈接池的狀態 public static void poolStatus() { try { System.out.println("悠閒的:" + dataSource.getNumIdleConnections()); System.out.println("忙碌的:" + dataSource.getNumBusyConnections()); System.out.println("全部的:" + dataSource.getNumConnections()); } catch (SQLException e) { e.printStackTrace(); } } }
實體映射
User:sql
1 package bean; 2 3 import java.util.Date; 4 5 public class User { 6 private int id; 7 private String username; 8 private String password; 9 private String email; 10 private String realname; 11 private String address; 12 private String mobile; 13 private Date regTime; 14 private int role; 15 16 public int getId() { 17 return id; 18 } 19 20 public void setId(int id) { 21 this.id = id; 22 } 23 24 public String getUsername() { 25 return username; 26 } 27 28 public void setUsername(String username) { 29 this.username = username; 30 } 31 32 public String getPassword() { 33 return password; 34 } 35 36 public void setPassword(String password) { 37 this.password = password; 38 } 39 40 public String getEmail() { 41 return email; 42 } 43 44 public void setEmail(String email) { 45 this.email = email; 46 } 47 48 public String getRealname() { 49 return realname; 50 } 51 52 public void setRealname(String realname) { 53 this.realname = realname; 54 } 55 56 public String getAddress() { 57 return address; 58 } 59 60 public void setAddress(String address) { 61 this.address = address; 62 } 63 64 public String getMobile() { 65 return mobile; 66 } 67 68 public void setMobile(String mobile) { 69 this.mobile = mobile; 70 } 71 72 public Date getRegTime() { 73 return regTime; 74 } 75 76 public void setRegTime(Date regTime) { 77 this.regTime = regTime; 78 } 79 80 public int getRole() { 81 return role; 82 } 83 84 public void setRole(int role) { 85 this.role = role; 86 } 87 88 @Override 89 public String toString() { 90 return "User{" + 91 "id=" + id + 92 ", username='" + username + '\'' + 93 ", password='" + password + '\'' + 94 ", email='" + email + '\'' + 95 ", realname='" + realname + '\'' + 96 ", address='" + address + '\'' + 97 ", mobile='" + mobile + '\'' + 98 ", regTime=" + regTime + 99 ", role=" + role + 100 '}'; 101 } 102 }
Book:數據庫
1 package bean; 2 3 import java.math.BigDecimal; 4 5 public class Book { 6 private int id; 7 private String bookname; 8 private int salesVolume; 9 private int inventory; 10 private int categoryId; 11 private String depict; 12 private BigDecimal price; 13 private BigDecimal sellingPrice; 14 private int recommend; 15 private int clickcount; 16 private int onSale; 17 private String imgAddress; 18 19 public int getId() { 20 return id; 21 } 22 23 public void setId(int id) { 24 this.id = id; 25 } 26 27 public String getBookname() { 28 return bookname; 29 } 30 31 public void setBookname(String bookname) { 32 this.bookname = bookname; 33 } 34 35 public int getSalesVolume() { 36 return salesVolume; 37 } 38 39 public void setSalesVolume(int salesVolume) { 40 this.salesVolume = salesVolume; 41 } 42 43 public int getInventory() { 44 return inventory; 45 } 46 47 public void setInventory(int inventory) { 48 this.inventory = inventory; 49 } 50 51 public int getCategoryId() { 52 return categoryId; 53 } 54 55 public void setCategoryId(int categoryId) { 56 this.categoryId = categoryId; 57 } 58 59 public String getDepict() { 60 return depict; 61 } 62 63 public void setDepict(String depict) { 64 this.depict = depict; 65 } 66 67 public BigDecimal getPrice() { 68 return price; 69 } 70 71 public void setPrice(BigDecimal price) { 72 this.price = price; 73 } 74 75 public BigDecimal getSellingPrice() { 76 return sellingPrice; 77 } 78 79 public void setSellingPrice(BigDecimal sellingPrice) { 80 this.sellingPrice = sellingPrice; 81 } 82 83 public int getRecommend() { 84 return recommend; 85 } 86 87 public void setRecommend(int recommend) { 88 this.recommend = recommend; 89 } 90 91 public int getClickcount() { 92 return clickcount; 93 }