文章目录
- 一.功能需求
- 二.设计思路
- 三.实现步骤
- (一)创建数据表
- (二)创建Web项目
- (三)创建实体类
- (四)创建数据工具类ConnectionManager
- 1.在web\WEB-INF目录下创建lib子目录,添加MySQL驱动程序jar包
- 2.在src下创建net.ysy.shop.dbutil包,在里面加ConnectionManager类
- 3.运行程序,查看结果
- 建议采用数据库连接池,效率更高,建议使用数据源方式获取数据连接.
- (五)数据访问接口(XXXDao)
- (六)数据访问接口实现类(XXXDaoImpl)
- 1.用户数据访问接口实现类UserDaoImpl
- 2.类别数据访问接口实现类CategoryDaoImpl
- 3.商品数据访问接口实现类ProductDaoImpl
- 4.订单数据访问接口实现类OrderDaoImpl
- (七)数据访问接口实现类(XXXService)
本项目采用MVC模式进行开发,整合JSP+Servlet+DB(DAO).
一.功能需求
1.普通用户
只有注册用户登录成功之后才可查看商品类别,查看商品,选购商品、生成订单、查看订单.
2.管理员用户
只有管理员才有权限进入购物网后台管理,进行用户管理
二.设计思路
(一)采用MVC设计模式
分层架构:展现层(JSP)<——>控制层(Servlet)<——>业务层(Service)<——>模型层(Dao)<——>数据库(DB)
(二)前台购物
1.购物流程
登录-------显示商品类别------显示某类商品信息------查看购物车------生成订单------支付
2.用户注册
系统只有一个管理员,在注册页面注册的都是普通用户.
(三)后台管理
1.用户管理
用户的增删改查
2.类别管理
商品类别的增删改查
3.商品管理
商品的增删改查
4.订单管理
订单的查看与删除
三.实现步骤
(一)创建数据表
创建MySQL数据库shop,包含四张表:用户表(t_user)、类别表(t_category)、商品表(t_product)和订单表(t_order).
(二)创建Web项目
1.创建Web项目shop
2.在项目结构窗口设置Artifacts名词:shop
3.配置服务器(Server)
4.设置部署(Deployment)
(三)创建实体类
1.用户实体类User
package net.ysy.shop.bean;
import java.util.Date;
public class User {
/**
* 用户表示符
*/
private int id;
/**
*用户名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 电话号码
*/
private String telephone;
/**
* 注册时间
*/
private Date registerTime;
/**
* 权限(0:管理员;1:普通用户)
*/
private int popedom;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Date getRegisterTime() {
return registerTime;
}
public void setRegisterTime(Date registerTime) {
this.registerTime = registerTime;
}
public int getPopedom() {
return popedom;
}
public void setPopedom(int popedom) {
this.popedom = popedom;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", telephone='" + telephone + '\'' +
", registerTime=" + registerTime +
", popedom=" + popedom +
'}';
}
}
2.类别实体类Category
package net.ysy.shop.bean;
public class Category {
/**
* 类别标识符
*/
private int id;
/**
* 类别名称
*/
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Category{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
3.商品实体类Product
package net.ysy.shop.bean;
import java.util.Date;
public class Product {
/**
* 商品标识符
*/
private int id;
/**
* 商品名称
*/
private String name;
/**
* 商品上架时间
*/
private Date addTime;
/**
* 商品所属类别标识符
*/
private int categoryId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getAddTime() {
return addTime;
}
public void setAddTime(Date addTime) {
this.addTime = addTime;
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", addTime=" + addTime +
", categoryId=" + categoryId +
'}';
}
}
4.订单实体类Order
package net.ysy.shop.bean;
import javafx.scene.chart.PieChart;
import javax.xml.crypto.Data;
public class Order {
/**
* 订单标识符
*/
private int id;
/**
* 用户名
*/
private String username;
/**
* 联系电话
*/
private String telephone;
/**
* 订单总金额
*/
private String totalPric;
/**
* 送货地址
*/
private String deliveryAddrrss;
/**
* 下单时间
*/
private Data orderTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getTotalPric() {
return totalPric;
}
public void setTotalPric(String totalPric) {
this.totalPric = totalPric;
}
public String getDeliveryAddrrss() {
return deliveryAddrrss;
}
public void setDeliveryAddrrss(String deliveryAddrrss) {
this.deliveryAddrrss = deliveryAddrrss;
}
public Data getOrderTime() {
return orderTime;
}
public void setOrderTime(Data orderTime) {
this.orderTime = orderTime;
}
@Override
public String toString() {
return "Order{" +
"id=" + id +
", username='" + username + '\'' +
", telephone='" + telephone + '\'' +
", totalPric='" + totalPric + '\'' +
", deliveryAddrrss='" + deliveryAddrrss + '\'' +
", orderTime=" + orderTime +
'}';
}
}
(四)创建数据工具类ConnectionManager
1.在web\WEB-INF目录下创建lib子目录,添加MySQL驱动程序jar包
2.在src下创建net.ysy.shop.dbutil包,在里面加ConnectionManager类
package net.ysy.shop.dbutil;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ConnectionManager {
/**
* 数据库驱动程序
*/
private static final String DRIVER="com.mysql.jdbc.Driver";
/**
* 数据库统一标识符
*/
private static final String URL="jdbc:mysql://localhost:3306/simonshop";
/**
* 数据库用户名
*/
private static final String USERNAME="root";
/**
* 数据库密码
*/
private static final String PASSWORD="pASSWORD";
/**
* 私有构造方法,拒绝实例化
*/
private ConnectionManager(){
}
/**
* 获取数据库连接静态方法
* @return 数据库连接对象
*/
public static Connection getconnection(){
//定义数据库连接
Connection conn=null;
try{
//安装驱动程序
Class.forName(DRIVER);
//获取数据库连接
conn= DriverManager.getConnection(URL
+"?useUnicode=true&charaterEncoding=UTF8",USERNAME,PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
//返回数据库连接
return conn;
}
/**
* 关闭数据库连接静态方法
* @param conn
*/
public static void closeConnection(Connection conn){
//判断数据库连接是否为空
if (conn !=null){
//判断数据库连接是否关闭
try{
if (!conn.isClosed()){
//关闭数据库
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 测试数据库连接是否成功
*/
public static void main(String[] args) {
//获取数据库连接
Connection conn=getconnection();
//判断是否连接成功
if (conn !=null){
JOptionPane.showMessageDialog(null,"恭喜连接成功!");
}else {
JOptionPane.showMessageDialog(null,"遗憾,数据库连接失败!");
}
}
}
3.运行程序,查看结果
建议采用数据库连接池,效率更高,建议使用数据源方式获取数据连接.
(五)数据访问接口(XXXDao)
1.用户数据访问接口UserDao
package net.ysy.shop.dao;
import net.ysy.shop.bean.User;
import java.sql.SQLException;
import java.util.List;
public interface UserDao {
//插入用户
int insert(User user) throws SQLException;
//按标识符删除用户
int deleteById(int id);
//更新用户
int update(User user);
//按标识符查询用户
User findById(int id);
//按用户名查询用户
List<User> findByUsername(String username);
//查询全部用户
List<User> findAll();
//用户登录
User login(String username,String password);
}
2.类别数据访问接口CategoryDao
package net.ysy.shop.dao;
import net.ysy.shop.bean.Category;
import java.util.List;
public interface CategoryDao {
//插入类别
int insert(Category category);
//按标识符删除类别
int deletById(int id);
//更新类别
int update(Category category);
//按标识符查询类别
Category findById(int id);
//查询全部类别
List<Category> findAll();
}
3.商品数据访问接口ProductDao
package net.ysy.shop.dao;
import net.ysy.shop.bean.Product;
import java.util.List;
public interface ProductDao {
//插入商品
int insert(Product product);
//按标识符删除商品
int deletById(int id);
//更新商品
int update(Product product);
//按标识符查询商品
Product findById(int id);
//按类别查询商品
List<Product> findByCategoryId(int categoryId);
//查询全部商品
List<Product> findAll();
}
4.订单数据访问接口OrderDao
package net.ysy.shop.dao;
import net.ysy.shop.bean.Order;
import java.util.List;
public interface OrderDao {
//插入订单
int insert(Order order);
//按标识符删除订单
int deleteById(int id);
//更新订单
int update(Order order);
//按标识符查询订单
Order findById(int id);
//查询最后一个订单
Order findLast();
//查询全部订单
List<Order> findAll();
}
(六)数据访问接口实现类(XXXDaoImpl)
1.用户数据访问接口实现类UserDaoImpl
package net.ysy.shop.dao.impl;
import net.ysy.shop.bean.User;
import net.ysy.shop.dao.UserDao;
import net.ysy.shop.dbutil.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class UserDaoImpl implements UserDao {
/**
* 插入用户
*/
@Override
public int insert(User user) throws SQLException {
//定义插入记录数
int count=0;
//获得数据库连接
Connection conn= ConnectionManager.getconnection();
//定义SQL字符串
String strSQL="INSERT INTO t_user(username,password,telephone,register_time,popedom)"+"VALUES(?,?,?,?,?)";
try {
//创建预备语句对象
PreparedStatement pstm=conn.prepareStatement(strSQL);
//设置占位符的值
pstm.setString(1,user.getUsername());
pstm.setString(2,user.getPassword());
pstm.setString(3,user.getTelephone());
pstm.setTimestamp(4,new Timestamp(user.getRegisterTime().getTime()));
pstm.setInt(5,user.getPopedom());
//执行更新操作,插入新记录
count=pstm.executeUpdate();
//关闭预备语句
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
ConnectionManager.closeConnection(conn);
}
//返回插入记录数
return count;
}
@Override
public int deleteById(int id) {
//定义删除距离数
int count=0;
//获得数据库连接
Connection conn=ConnectionManager.getconnection();
//定义SQL字符串
String strSQL="DELETE FROM t_user WHERE id =? ";
try{
//创建预备语句
PreparedStatement pstm=conn.prepareStatement(strSQL);
//设置占位符的值
pstm.setInt(1,id);
//执行更新操作,删除记录
count=pstm.executeUpdate();
//关闭预备语句对象
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
ConnectionManager.closeConnection(conn);
}
//返回删除距离数
return count;
}
/**
* 更新用户
*/
@Override
public int update(User user) {
//定义记录数
int count=0;
//获取数据库连接
Connection conn=ConnectionManager.getconnection();
//定义SQL字符串
String strSQL="UPDATE t_user SET username = ?,password = ?,telephone = ?,"
+"register_time = ? ,popem = ? WHERE id= ?";
try{
//创建预备语句对象
PreparedStatement pstm=conn.prepareStatement(strSQL);
//设置占位符的值
pstm.setString(1,user.getUsername());
pstm.setString(2,user.getPassword());
pstm.setString(3,user.getTelephone());
pstm.setTimestamp(4,new Timestamp(user.getRegisterTime().getTime()));
pstm.setInt(5,user.getPopedom());
pstm.setInt(6,user.getId());
//执行更新操作,更新记录
count=pstm.executeUpdate();
//关闭预备语句对象
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
ConnectionManager.closeConnection(conn);
}
//返回更新记录数
return count;
}
@Override
public User findById(int id) {
//声明用户
User user=null;
//获取数据库连接对象
Connection conn=ConnectionManager.getconnection();
//定义字符串
String strSQL=" SELECT * FROM t_user WHERE id =?";
try{
//创建预备语句对象
PreparedStatement pstm=conn.prepareStatement(strSQL);
//设置占位符
pstm.setInt(1,id);
//执行SQL查询,返回结果集
ResultSet rs=pstm.executeQuery();
//判断结果集是否有记录
if (rs.next()){
//实例化用户
user=new User();
//利用当前记录字段去设置商品类别属性
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
user.setPopedom(rs.getInt("popedom"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
ConnectionManager.closeConnection(conn);
}
return user;
}
@Override
public List<User> findByUsername(String username) {
//声明用户列表
List<User> users=new ArrayList<User>();
//获取数据库连接对象
Connection conn=ConnectionManager.getconnection();
//定义SQL字符串
String strSQL="SELECT * FROM t_user WHERE username =? ";
try{
//创建预备语句
PreparedStatement pstm=conn.prepareStatement(strSQL);
//设置占位符的值
pstm.setString(1,username);
//执行SQl查询,返回结果集
ResultSet rs=pstm.executeQuery();
//遍历结果集
while (rs.next()){
//创建类别实体类
User user=new User();
//设置实体属性
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
user.setPopedom(rs.getInt("popedom"));
//将实体类添加到用户列表
users.add(user);
}
//关闭结果集
rs.close();
//关闭语句对象
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
ConnectionManager.closeConnection(conn);
}
return users;
}
@Override
public List<User> findAll() {
//声明用户列表
List<User> users=new ArrayList<>();
//获取数据库连接对象
Connection conn=ConnectionManager.getconnection();
//定义SQL字符串
String strSQL="SELECT * FROM t_user";
try{
//创建预备语句
Statement stmt=conn.createStatement();
//执行SQL,返回结果集
ResultSet rs=stmt.executeQuery(strSQL);
while (rs.next()){
//设置实体属性
User user=new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
user.setPopedom(rs.getInt("popedom"));
//将实体类添加到用户列表
users.add(user);
}
//关闭结果集
rs.close();
//关闭语句对象
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回用户列表
return users;
}
/**
* 登录方法
* @param username
* @param password
* @return
*/
@Override
public User login(String username, String password) {
//定义用户对象
User user=null;
//获取数据库连接
Connection conn=ConnectionManager.getconnection();
//定义SQL字符
String strSQL="SELECT * FROM t_user WHERE username = ? AND password = ?";
try{
//创建预备语句
PreparedStatement pstm=conn.prepareStatement(strSQL);
//设置占位符的值
pstm.setString(1,username);
pstm.setString(2,password);
//执行查询,返回结果集
ResultSet rs=pstm.executeQuery();
//判断结果集是否有记录
if (rs.next()){
//实例化对象
user=new User();
//用户记录设置用户属性
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getDate("register_time"));
user.setPopedom(rs.getInt("popedom"));
}
//关闭结果集
rs.close();
//关闭预备语句
pstm.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
//关闭数据库连接
ConnectionManager.closeConnection(conn);
}
//返回用户对象
return user;
}
}
我们需要对用户数据访问接口实现类的各个方法进行单元测试,采用JUnit来进行单元测试.
在项目根目录创建一个test文件夹,然后在项目结构窗口里将标记为"Tests",这样文件夹颜色将变为green.
在test文件夹里面创建net.ysy.shop.dao.impl包,在里面创建测试类TestUserDaoImpl
(1)编写测试登录方法testLogin()
将光标定位到@Test注解符,按组合键Alt+Enter:
package net.ysy.shop.dao.impl;
import net.ysy.shop.bean.User;
import net.ysy.shop.dao.UserDao;
import org.junit.Test;
public class TestUserDaoImpl {
@Test
public void testLogin(){
String username,password;
username="admin";
password="12345";
//父类接口指向子类对象
UserDao userDao=new UserDaoImpl();
//调用登录用户数据访问对象的登录方法
User user=userDao.login(username,password);
//判断用户是否登录成功
if (user !=null){
System.out.println("恭喜,登录成功!");
}else {
System.out.println("遗憾,登录失败!");
}
}
}
修改登录密码,在进行测试,看结构如何:
将用户[涂文艳]的密码改为"123456",电话改为"15883723523",涂文艳的用户id是4,进行测试.
实现删除用户方法,寻找用户id,进行删除.