介绍
项目采用B/S架构、MVC开发模式,使用java语言,根据Java EE 标准开发,旨在感受java web项目开发流程、学习与理解java EE基础技术和原理。
M: Model模型 JavaBean
V:view视图 Html
C:Controller控制器 Servlet
其中Html与Servlet之间用Ajax与Json技术互动。
项目结构
使用maven构建,分为Dao、Service、Servlet、和前端展示层
数据持久化工具
使用C3p0数据库连接池
C3P0配置文件出c3p0-config.xml:
<?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:
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:
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 }
94
95 public