购物小网站
结构
package 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 + "]";
}
}
package bean;
import java.util.Date;
public class Order {
/**
* 订单标识符
*/
private int id;
/**
* 用户名
*/
private String username;
/**
* 联系电话
*/
private String telephone;
/**
* 订单总金额
*/
private double totalPrice;
/**
* 送货地址
*/
private String deliveryAddress;
/**
* 下单时间
*/
private Date 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 double getTotalPrice() {
return totalPrice;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
public String getDeliveryAddress() {
return deliveryAddress;
}
public void setDeliveryAddress(String deliveryAddress) {
this.deliveryAddress = deliveryAddress;
}
public Date getOrderTime() {
return orderTime;
}
public void setOrderTime(Date orderTime) {
this.orderTime = orderTime;
}
@Override
public String toString() {
return "Order [id=" + id + ", username=" + username + ", telephone=" + telephone + ", totalPrice=" + totalPrice
+ ", deliveryAddress=" + deliveryAddress + ", orderTime=" + orderTime + "]";
}
}
package bean;
import java.util.Date;
public class Product {
/**
* 商品标识符
*/
private int id;
/**
* 商品名称
*/
private String name;
/**
* 商品单价
*/
private double price;
/**
* 商品上架时间
*/
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 double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
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 + ", price=" + price + ", addTime=" + addTime
+ ", categoryId=" + categoryId + "]";
}
}
package 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(String yanghua) {
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 + "]";
}
}
package dao.impl;
import bean.Category;
import dao.CategoryDao;
import dbutil.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class CategoryDaoImpl implements CategoryDao {
/**
* 插入类别
*/
@Override
public int insert(Category category) {
// 定义插入记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "INSERT INTO t_category (name) VALUES (?)";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, category.getName());
// 执行更新操作,插入新录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.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_category WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, id);
// 执行更新操作,删除记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回删除记录数
return count;
}
/**
* 更新类别
*/
@Override
public int update(Category category) {
// 定义更新记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "UPDATE t_category SET name = ? WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, category.getName());
pstmt.setInt(2, category.getId());
// 执行更新操作,更新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
/**
* 按标识符查询类别
*/
@Override
public Category findById(int id) {
// 声明商品类别
Category category = null;
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_category WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, id);
// 执行SQL查询,返回结果集
ResultSet rs = pstmt.executeQuery();
// 判断结果集是否有记录
if (rs.next()) {
// 实例化商品类别
category = new Category();
// 利用当前记录字段值去设置商品类别的属性
category.setId(rs.getInt("id"));
category.setName(rs.getString("name"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回商品类别
return category;
}
/**
* 查询全部类别
*/
@Override
public List<Category> findAll() {
// 声明类别列表
List<Category> categories = new ArrayList<Category>();
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_category";
try {
// 创建语句对象
Statement stmt = conn.createStatement();
// 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 遍历结果集
while (rs.next()) {
// 创建类别实体
Category category = new Category();
// 设置实体属性
category.setId(rs.getInt("id"));
category.setName(rs.getString("name"));
// 将实体添加到类别列表
categories.add(category);
}
// 关闭结果集
rs.close();
// 关闭语句对象
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回类别列表
return categories;
}
}
package dao.impl;
import bean.Order;
import dao.OrderDao;
import dbutil.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class OrderDaoImpl implements OrderDao {
/**
* 插入订单
*/
@Override
public int insert(Order order) {
// 定义插入记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "INSERT INTO t_order (username, telephone, total_price, delivery_address, order_time)"
+ " VALUES (?, ?, ?, ?, ?)";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, order.getUsername());
pstmt.setString(2, order.getTelephone());
pstmt.setDouble(3, order.getTotalPrice());
pstmt.setString(4, order.getDeliveryAddress());
pstmt.setTimestamp(5, new Timestamp(order.getOrderTime().getTime()));
// 执行更新操作,插入记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.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_order WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, id);
// 执行更新操作,删除记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回删除记录数
return count;
}
/**
* 更新订单
*/
@Override
public int update(Order order) {
// 定义更新记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "UPDATE t_order SET username = ?, telephone = ?, total_price = ?,"
+ " delivery_address = ?, order_time = ? WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, order.getUsername());
pstmt.setString(2, order.getTelephone());
pstmt.setDouble(3, order.getTotalPrice());
pstmt.setString(4, order.getDeliveryAddress());
pstmt.setTimestamp(5, new Timestamp(order.getOrderTime().getTime()));
pstmt.setInt(6, order.getId());
// 执行更新操作,更新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
/**
* 查询最后一个订单
*/
@Override
public Order findLast() {
// 声明订单
Order order = null;
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_order";
try {
// 创建语句对象
Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
// 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 定位到最后一条记录
if (rs.last()) {
// 创建订单实体
order = new Order();
// 设置实体属性
order.setId(rs.getInt("id"));
order.setUsername(rs.getString("username"));
order.setTelephone(rs.getString("telephone"));
order.setTotalPrice(rs.getDouble("total_price"));
order.setDeliveryAddress(rs.getString("delivery_address"));
order.setOrderTime(rs.getTimestamp("order_time"));
}
// 关闭结果集
rs.close();
// 关闭语句对象
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回订单对象
return order;
}
/**
* 按标识符查询订单
*/
@Override
public Order findById(int id) {
// 声明订单
Order order = null;
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_order WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, id);
// 执行SQL查询,返回结果集
ResultSet rs = pstmt.executeQuery();
// 判断结果集是否有记录
if (rs.next()) {
// 实例化订单
order = new Order();
// 利用当前记录字段值去设置订单的属性
order.setId(rs.getInt("id"));
order.setUsername(rs.getString("username"));
order.setTelephone(rs.getString("telephone"));
order.setDeliveryAddress(rs.getString("delivery_address"));
order.setOrderTime(rs.getTimestamp("order_time"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回订单
return order;
}
/**
* 查询全部订单
*/
@Override
public List<Order> findAll() {
// 声明订单列表
List<Order> orders = new ArrayList<Order>();
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_order";
try {
// 创建语句对象
Statement stmt = conn.createStatement();
// 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 遍历结果集
while (rs.next()) {
// 创建订单实体
Order order = new Order();
// 设置实体属性
order.setId(rs.getInt("id"));
order.setUsername(rs.getString("username"));
order.setTelephone(rs.getString("telephone"));
order.setDeliveryAddress(rs.getString("delivery_address"));
order.setOrderTime(rs.getTimestamp("order_time"));
// 将实体添加到订单列表
orders.add(order);
}
// 关闭结果集
rs.close();
// 关闭语句对象
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回用户列表
return orders;
}
}
package dao.impl;
import bean.Product;
import dao.ProductDao;
import dbutil.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class ProductDaoimpl implements ProductDao {
/**
* 插入商品
*/
@Override
public int insert(Product product) {
// 定义插入记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "INSERT INTO t_product (name, price, add_time, category_id)" + " VALUES (?, ?, ?, ?)";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, product.getName());
pstmt.setDouble(2, product.getPrice());
pstmt.setTimestamp(3, new Timestamp(product.getAddTime().getTime()));
pstmt.setInt(4, product.getCategoryId());
// 执行更新操作,插入新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.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_product WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, id);
// 执行更新操作,删除记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回删除记录数
return count;
}
/**
* 更新商品
*/
@Override
public int update(Product product) {
// 定义更新记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "UPDATE t_product SET name = ?, price = ?, add_time = ?,"
+ " category_id = ? WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, product.getName());
pstmt.setDouble(2, product.getPrice());
pstmt.setTimestamp(3, new Timestamp(product.getAddTime().getTime()));
pstmt.setInt(4, product.getCategoryId());
pstmt.setInt(5, product.getId());
// 执行更新操作,更新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
/**
* 按标识符查找商品
*/
@Override
public Product findById(int id) {
// 声明商品
Product product = null;
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_product WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, id);
// 执行SQL查询,返回结果集
ResultSet rs = pstmt.executeQuery();
// 判断结果集是否有记录
if (rs.next()) {
// 实例化商品
product = new Product();
// 利用当前记录字段值去设置商品类别的属性
product.setId(rs.getInt("id"));
product.setName(rs.getString("name"));
product.setPrice(rs.getDouble("price"));
product.setAddTime(rs.getTimestamp("add_time"));
product.setCategoryId(rs.getInt("category_id"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回商品
return product;
}
/**
* 按类别查询商品
*/
@Override
public List<Product> findByCategoryId(int categoryId) {
// 定义商品列表
List<Product> products = new ArrayList<Product>();
// 获取数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_product WHERE category_id = ?";
try {
// 创建预备语句
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, categoryId);
// 执行SQL语句,返回结果集
ResultSet rs = pstmt.executeQuery();
// 遍历结果集,将其中的每条记录生成商品对象,添加到商品列表
while (rs.next()) {
// 实例化商品对象
Product product = new Product();
// 利用当前记录字段值设置实体对应属性
product.setId(rs.getInt("id"));
product.setName(rs.getString("name"));
product.setPrice(rs.getDouble("price"));
product.setAddTime(rs.getTimestamp("add_time"));
product.setCategoryId(rs.getInt("category_id"));
// 将商品添加到商品列表
products.add(product);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回商品列表
return products;
}
/**
* 查询全部商品
*/
@Override
public List<Product> findAll() {
// 声明商品列表
List<Product> products = new ArrayList<Product>();
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_product";
try {
// 创建语句对象
Statement stmt = conn.createStatement();
// 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 遍历结果集
while (rs.next()) {
// 创建商品实体
Product product = new Product();
// 设置实体属性
product.setId(rs.getInt("id"));
product.setName(rs.getString("name"));
product.setPrice(rs.getDouble("price"));
product.setAddTime(rs.getTimestamp("add_time"));
product.setCategoryId(rs.getInt("category_id"));
// 将实体添加到商品列表
products.add(product);
}
// 关闭结果集
rs.close();
// 关闭语句对象
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回商品列表
return products;
}
}
package dao.impl;
import bean.User;
import dao.UserDao;
import dbutil.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class UserDaoImpl implements UserDao {
/**
* 插入用户
*/
@Override
public int insert(User user) {
// 定义插入记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "INSERT INTO t_user (username, password, telephone, register_time, popedom)"
+ " VALUES (?, ?, ?, ?, ?)";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, user.getUsername("yanghua"));
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getTelephone());
pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
pstmt.setInt(5, user.getPopedom());
// 执行更新操作,插入新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.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 pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, id);
// 执行更新操作,删除记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.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 = ?, popedom = ? WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, user.getUsername("yanghua"));
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getTelephone());
pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
pstmt.setInt(5, user.getPopedom());
pstmt.setInt(6, user.getId());
// 执行更新操作,更新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.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();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_user WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, id);
// 执行SQL查询,返回结果集
ResultSet rs = pstmt.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.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 pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, username);
// 执行SQL查询,返回结果集
ResultSet rs = pstmt.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();
// 关闭语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回用户列表
return users;
}
@Override
public List<User> findAll() {
// 声明用户列表
List<User> users = new ArrayList<User>();
// 获取数据库连接对象
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;
}
/**
* 登录方法
*/
@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 psmt = conn.prepareStatement(strSQL);
// 设置占位符的值
psmt.setString(1, username);
psmt.setString(2, password);
// 执行查询,返回结果集
ResultSet rs = psmt.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();
// 关闭预备语句
psmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回用户对象
return user;
}
}
package dao;
import bean.Category;
import java.util.List;
public interface CategoryDao {
// 插入类别
int insert(Category category);
// 按标识符删除类别
int deleteById(int id);
// 更新类别
int update(Category category);
// 按标识符查询类别
Category findById(int id);
// 查询全部类别
List<Category> findAll();}
package dao;
import 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();
}
package dao;
import bean.Product;
import java.util.List;
public interface ProductDao {
// 插入商品
int insert(Product product);
// 按标识符删除商品
int deleteById(int id);
// 更新商品
int update(Product product);
// 按标识符查询商品
Product findById(int id);
// 按类别查询商品
List<Product> findByCategoryId(int categoryId);
// 查询全部商品
List<Product> findAll();}
package dao;
import bean.User;
import java.util.List;
public interface UserDao {
// 插入用户
int insert(User user);
// 按标识符删除用户
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);
}
package 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 = "1";
/**
* 私有化构造方法,拒绝实例化
*/
private ConnectionManager() {
}
/**
* 获得数据库连接
*
* @return 数据库连接对象
*/
public static Connection getConnection() {
// 定义数据库连接
Connection conn = null;
try {
// 安装数据库驱动程序
Class.forName(DRIVER);
// 获得数据库连接
conn = DriverManager.getConnection(URL
+ "?useUnicode=true&characterEncoding=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();
}
}
}
/**
* 测试数据库连接是否成功
*
* @param args
*/
public static void main(String[] args) {
// 获得数据库连接
Connection conn = getConnection();
// 判断是否连接成功
if (conn != null) {
JOptionPane.showMessageDialog(null, "恭喜,数据库连接成功!");
} else {
JOptionPane.showMessageDialog(null, "遗憾,数据库连接失败!");
}
// 关闭数据库连接
closeConnection(conn);
}}
package Service.ServiceImpl;
import Service.CategoryService;
import bean.Category;
import dao.CategoryDao;
import dao.impl.CategoryDaoImpl;
import java.util.List;
public class CategoryServiceImpl implements CategoryService {
/**
* 声明类别数据访问对象
*/
private CategoryDao categoryDao = (CategoryDao) new CategoryDaoImpl();
@Override
public int addCategory(Category category) {
return categoryDao.insert(category);
}
@Override
public int deleteCategoryById(int id) {
return categoryDao.deleteById(id);
}
@Override
public int updateCategory(Category category) {
return categoryDao.update(category);
}
@Override
public Category findCategoryById(int id) {
return categoryDao.findById(id);
}
@Override
public List<Category> findAllCategories() {
return categoryDao.findAll();
}
}
package Service.ServiceImpl;
import Service.OrderService;
import bean.Order;
import dao.OrderDao;
import dao.impl.OrderDaoImpl;
import java.util.List;
public class OrderServiceImpl implements OrderService {
/**
* 声明订单数据访问对象
*/
OrderDao orderDao = new OrderDaoImpl();
@Override
public int addOrder(Order order) {
return orderDao.insert(order);
}
@Override
public int deleteOrderById(int id) {
return orderDao.deleteById(id);
}
@Override
public int updateOrder(Order order) {
return orderDao.update(order);
}
@Override
public Order findOrderById(int id) {
return orderDao.findById(id);
}
@Override
public Order findLastOrder() {
return orderDao.findLast();
}
@Override
public List<Order> findAllOrders() {
return orderDao.findAll();
}
}
package Service.ServiceImpl;
import Service.ProductService;
import bean.Product;
import dao.ProductDao;
import dao.impl.ProductDaoimpl;
import java.util.List;
public class ProductServiceImpl implements ProductService {
/**
* 声明商品数据访问对象
*/
private ProductDao productDao = new ProductDaoimpl();
@Override
public int addProduct(Product product) {
return productDao.insert(product);
}
@Override
public int deleteProductById(int id) {
return productDao.deleteById(id);
}
@Override
public int updateProduct(Product product) {
return productDao.update(product);
}
@Override
public Product findProductById(int id) {
return productDao.findById(id);
}
@Override
public List<Product> findProductsByCategoryId(int categoryId) {
return productDao.findByCategoryId(categoryId);
}
@Override
public List<Product> findAllProducts() {
return productDao.findAll();
}
}
package Service.ServiceImpl;
import Service.UserService;
import bean.User;
import dao.UserDao;
import dao.impl.UserDaoImpl;
import java.util.List;
public class UserServiceImpl implements UserService {
/**
* 声明用户访问对象
*/
private UserDao userDao = new UserDaoImpl();
@Override
public int addUser(User user) {
return userDao.insert(user);
}
@Override
public int deleteUserById(int id) {
return userDao.deleteById(id);
}
@Override
public int updateUser(User user) {
return userDao.update(user);
}
@Override
public User findUserById(int id) {
return userDao.findById(id);
}
@Override
public List<User> findUsersByUsername(String username) {
return userDao.findByUsername(username);
}
@Override
public List<User> findAllUsers() {
return userDao.findAll();
}
@Override
public User login(String username, String password) {
return userDao.login(username, password);
}
}
package 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 + "]";
}
}
package Service;
import bean.Order;
import java.util.List;
public interface OrderService {
// 添加订单
int addOrder(Order order);
// 按标识符删除订单
int deleteOrderById(int id);
// 更新订单
int updateOrder(Order order);
// 查询最后一个订单
Order findLastOrder();
// 按标识符查询订单
Order findOrderById(int id);
// 查询全部订单
List<Order> findAllOrders();
}
package Service;
import bean.Product;
import java.util.List;
public interface ProductService {
// 添加商品
int addProduct(Product product);
// 按标识符删除商品
int deleteProductById(int id);
// 更新商品
int updateProduct(Product product);
// 按标识符查询商品
Product findProductById(int id);
// 按类别查询商品
List<Product> findProductsByCategoryId(int categoryId);
// 查询全部商品
List<Product> findAllProducts();
}
package Service;
import bean.User;
import java.util.List;
public interface UserService {
// 添加用户
int addUser(User user);
// 按标识符删除用户
int deleteUserById(int id);
// 更新用户
int updateUser(User user);
// 按标识符查询用户
User findUserById(int id);
// 按用户名查询用户
List<User> findUsersByUsername(String username);
// 查询全部用户
List<User> findAllUsers();
// 用户登录
User login(String username, String password);
}
测试
package test.dao;
import bean.Category;
import dao.CategoryDao;
import dao.impl.CategoryDaoImpl;
import org.junit.Test;
import java.util.List;
public class TestCategoryDaoImpl {
@Test
public void testdeleteById(){
CategoryDao dao=new CategoryDaoImpl();
int user=dao.deleteById(1);
System.out.println(user);
}
@Test
public void testFindById(){
CategoryDao dao =new CategoryDaoImpl();
Category category=dao.findById(2);
System.out.println(category);
}
@Test
public void testFindAll(){
CategoryDao dao=new CategoryDaoImpl();
List<Category>category=dao.findAll();
System.out.println(category);
}
@Test
public void testInsert(){
Category category=new Category();
CategoryDao categoryDao=new CategoryDaoImpl();
category.setId(1);
category.setName("jslfjls");
int count=categoryDao.insert(category);
if (count>0){
System.out.println("插入成功");
}else {
System.out.println("插入失败");
}
}
@Test
public void testUpdate(){
CategoryDao dao=new CategoryDaoImpl();
Category category=dao.findById(3);
category.setName("wew");
int count=dao.update(category);
if(count>0){
System.out.println("更新成功");
}else {
System.out.println("更新失败");
}
}
}
package test.dao;
import bean.Order;
import dao.OrderDao;
import dao.impl.OrderDaoImpl;
import org.junit.Test;
import java.util.Date;
import java.util.List;
public class TestOrderImpl {
@Test
public void testInsert(){
Order order=new Order();
OrderDao orderDao=new OrderDaoImpl();
order.setUsername("aaa");
order.setTotalPrice(123);
order.setTelephone("2312312312");
order.setOrderTime(new Date());
order.setDeliveryAddress("home");
int count=orderDao.insert(order);
if (count>0){
System.out.println("插入成功");
}else {
System.out.println("插入失败");
}
}
@Test
public void testdeletByid(){
OrderDao dao=new OrderDaoImpl();
int order=dao.deleteById(1);
System.out.println(order);
}
@Test
public void testUpdate(){
OrderDao dao=new OrderDaoImpl();
Order order=dao.findById(2);
order.setUsername("bbbb");
order.setTelephone("3021830");
order.setTotalPrice(123);
order.setOrderTime(new Date());
order.setDeliveryAddress("live");
order.setTotalPrice(333);
int count=dao.update(order);
if(count>0){
System.out.println("更新成功");
}else {
System.out.println("更新失败");
}
}
@Test
public void testFindLast(){
OrderDao dao = new OrderDaoImpl();
Order order=dao.findLast();
System.out.println(order);
}
@Test
public void testFindById(){
OrderDao dao =new OrderDaoImpl();
Order order=dao.findById(3);
System.out.println(order);
}
@Test
public void testFindAll(){
OrderDao dao =new OrderDaoImpl();
List<Order>orders=dao.findAll();
System.out.println(orders);
}
}
package test.dao;
import bean.Product;
import dao.ProductDao;
import dao.impl.ProductDaoimpl;
import org.junit.Test;
import java.util.Date;
import java.util.List;
public class TestProductDaoimpl {
@Test
public void testDeleteById(){
ProductDao dao=new ProductDaoimpl();
int user=dao.deleteById(1);
System.out.println(user);
}
@Test
public void testFindByname(){
ProductDao dao=new ProductDaoimpl();
List<Product>product=dao.findAll();
System.out.println(product);
}
@Test
public void testFindByid(){
ProductDao dao =new ProductDaoimpl();
Product product=dao.findById(2);
System.out.println(product);
}
@Test
public void testFindAll(){
ProductDao dao =new ProductDaoimpl();
List<Product>products=dao.findAll();
System.out.println(products);
}
@Test
public void testFindByCategoryId(){
ProductDao dao=new ProductDaoimpl();
List<Product>products=dao.findByCategoryId(4);
System.out.println(products);
}
@Test
public void testUpdate(){
ProductDao dao=new ProductDaoimpl();
Product product=dao.findById(2);
product.setName("小王");
product.setPrice(12345);
product.setCategoryId(3);
int count=dao.update(product);
if(count>0){
System.out.println("更新成功");
}else {
System.out.println("更新失败");
}
}
@Test
public void testInsert(){
Product product=new Product();
ProductDao productDao=new ProductDaoimpl();
product.setName("我也不知道");
product.setCategoryId(1);
product.setPrice(333);
product.setAddTime(new Date());
product.setId(1);
int count=productDao.insert(product);
if (count>0){
System.out.println("插入成功");
}else {
System.out.println("插入失败");
}
}
}
package test.dao;
import bean.User;
import dao.UserDao;
import dao.impl.UserDaoImpl;
import org.junit.Test;
import java.util.Date;
import java.util.List;
public class TestUserDaoImpl {
@Test
public void testLogin(){
UserDao dao=new UserDaoImpl();
String username, password;
username = "admin";
password = "12345";
User user = dao.login(username, password);
if (user != null) {
System.out.println("恭喜,用户名与密码正确,登录成功");
} else {
System.out.println("遗憾,用户名或密码错误,登录失败");
}
}
@Test
public void testFindById() {
UserDao dao = new UserDaoImpl();
User user = dao.findById(1);
System.out.println(user);
}
@Test
public void testDeletById() {
UserDao dao = new UserDaoImpl();
int user = dao.deleteById(4);
System.out.println(user);
}
@Test
public void testFindByName(){
UserDao dao=new UserDaoImpl();
List<User> user=dao.findByUsername("温志军");
System.out.println(user);
}
@Test
public void testFindAll(){
UserDao dao=new UserDaoImpl();
List<User> user=dao.findAll();
System.out.println(user);
}
@Test
public void testInsert(){
//定义用户对象
User user = new User();
//定义用户数据访问对象
UserDao userDao = new UserDaoImpl();
//设置用户属性
user.setUsername("李晓东");
user.setPassword("124578");
user.setTelephone("15478951235");
user.setRegisterTime(new Date());
user.setPopedom(1);
//调用用户数据访问对象的插入方式
int count = userDao.insert(user);
//判断用户记录是否插入成功
if (count>0){
System.out.println("恭喜用户插入成功");
System.out.println(userDao.findById(userDao.findAll().size()));
System.out.println(user);
}else {
System.out.println("用户插入失败");
}
}
@Test
public void testUpdate(){
//定义用户数据访问对象
UserDao userDao = new UserDaoImpl();
//找到涂文艳用户,其id是4
User user = userDao.findById(2);
//修改其密码与电话
user.setPassword("903213");
user.setTelephone("13945457890");
//更新涂文艳用户
int count = userDao.update(user);
//判断更新用户是否成功
if(count >0){
System.out.println("恭喜,用户更新成功");
}else {
System.out.println("遗憾,用户更新失败");
}
//再次查询涂文艳用户
user = userDao.findById(4);
//查看涂文艳用户信息
System.out.println(user);
}
}
package test.service;
import Service.CategoryService;
import Service.ServiceImpl.CategoryServiceImpl;
import bean.Category;
import org.junit.Test;
import java.util.List;
public class TestCategoryServiceImpl {
@Test
public void testfindAllCategories(){
CategoryService service = new CategoryServiceImpl();
List<Category> categories = service.findAllCategories();
if (((List) categories).size()>0){
for (Category category: categories){
System.out.println(category);
}
}else {
System.out.println("没有商品类别!");
}
}
@Test
public void testfindCategoryById(){
CategoryService service = new CategoryServiceImpl();
Category categories = service.findCategoryById(2);
System.out.println(categories);
}
@Test
public void testupdateCategory(){
CategoryService service = new CategoryServiceImpl();
Category categories = service.findCategoryById(2);
categories.setName("好吃美食");
int count = service.updateCategory(categories);
if(count >0){
System.out.println("恭喜,商品类别更新成功");
}else {
System.out.println("遗憾,商品类别更新失败");
}
//再次查询好吃美食商品类别
categories = service.findCategoryById(2);
//查看好吃美食商品类别
System.out.println(categories);
}
@Test
public void testdeleteCategoryById(){
// 定义商品类别数据访问对象
CategoryService service = new CategoryServiceImpl();
int count = service.deleteCategoryById(3);
if (count>0){
System.out.println("恭喜,商品类别删除成功");
}else {
System.out.println("商品类别记录删除失败");
}
}
@Test
public void testaddCategory(){
// 定义商品类别对象
Category category = new Category();
//定义商品类别数据访问对象
CategoryService service = new CategoryServiceImpl();
//设置商品类别属性
category.setId(1);
category.setName("经典美食");
//调用商品类别数据访问对象的插入方式
int count = service.addCategory(category);
//判断商品类别记录是否插入成功
if (count>0){
System.out.println("恭喜商品类别插入成功");
System.out.println(category);
}else {
System.out.println("商品类别插入失败");
}
}
}
package test.service;
import Service.OrderService;
import Service.ServiceImpl.OrderServiceImpl;
import bean.Order;
import org.junit.Test;
import java.util.Date;
import java.util.List;
public class TestOrderServiceImpl {
@Test
public void testaddOrder(){
OrderService service=new OrderServiceImpl();
Order order=new Order();
order.setUsername("jjjjjj");
order.setTelephone("48304823408");
order.setTotalPrice(840239);
order.setDeliveryAddress("sjflsd");
order.setOrderTime(new Date());
int count=service.addOrder(order);
if(count>0){
System.out.println("插入成功");
}else {
System.out.println("插入失败");
}
}
@Test
public void testeleteOrderById(){
OrderService service=new OrderServiceImpl();
int count=service.deleteOrderById(2);
if (count>0){
System.out.println("恭喜,用户记录删除成功");
}else {
System.out.println("用户记录删除失败");
}
}
@Test
public void testupdateOrder(){
OrderService service=new OrderServiceImpl();
Order order=new Order();
order.setOrderTime(new Date());
order.setUsername("dlkfjl");
order.setTotalPrice(4324);
order.setDeliveryAddress("erue");
int count=service.updateOrder(order);
if(count >0){
System.out.println("恭喜,用户更新成功");
}else {
System.out.println("遗憾,用户更新失败");
}
}
@Test
public void testfindOrderById(){
OrderService service=new OrderServiceImpl();
Order orders=service.findOrderById(3);
System.out.println(orders);
}
@Test
public void testfindAll(){
OrderService service=new OrderServiceImpl();
List<Order>orders=service.findAllOrders();
System.out.println(orders);
}
}
package test.service;
import Service.ProductService;
import Service.ServiceImpl.ProductServiceImpl;
import bean.Product;
import org.junit.Test;
import java.util.Date;
import java.util.List;
public class TestProductServiceImpl {
@Test
public void testaddProduct(){
Product product=new Product();
ProductService service=new ProductServiceImpl();
product.setName("ssss");
product.setCategoryId(12);
product.setAddTime(new Date());
int count=service.addProduct(product);
if(count>0){
System.out.println("插入成功");
}else {
System.out.println("插入失败");
}
}
@Test
public void testDeleteProductById(){
ProductService service=new ProductServiceImpl();
int count=service.deleteProductById(2);
if(count>0){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
@Test
public void testupdateProduct(){
ProductService service=new ProductServiceImpl();
Product product=service.findProductById(4);
product.setName("sjfl");
product.setAddTime(new Date());
product.setCategoryId(3);
int count=service.updateProduct(product);
if(count>0){
System.out.println("更新成功");
}else {
System.out.println("更新失败");
}
}
@Test
public void testfindProductById(){
ProductService service=new ProductServiceImpl();
Product product=service.findProductById(3);
System.out.println(product);
}
@Test
public void testfindProductsByCategoryId(){
ProductService service=new ProductServiceImpl();
List<Product>products=service.findProductsByCategoryId(2);
System.out.println(products);
}
@Test
public void testfindAllProducts(){
ProductService service=new ProductServiceImpl();
List<Product>products=service.findAllProducts();
System.out.println(products);
}
}
package test.service;
import Service.ServiceImpl.UserServiceImpl;
import Service.UserService;
import bean.User;
import org.junit.Test;
import java.util.Date;
import java.util.List;
public class TestUserServiceImpl {
@Test
public void testLogin(){
String username, password;
username = "admin";
password = "12345";
// 父接口指向子类对象
UserService service = new UserServiceImpl() {
};
User user = service.login(username, password);
// 判断用户登录是否成功
if (user != null) {
System.out.println("恭喜,登录成功!");
} else {
System.out.println("遗憾,登录失败!");
}
}
@Test
public void testUpdate(){
//定义用户数据访问对象
UserService service = new UserServiceImpl();
//找到涂文艳用户,其id是4
User user = service.findUserById(4);
//修改其密码与电话
user.setPassword("903214");
user.setTelephone("13945457890");
//更新涂文艳用户
int count = service.updateUser(user);
//判断更新用户是否成功
if(count >0){
System.out.println("恭喜,用户更新成功");
}else {
System.out.println("遗憾,用户更新失败");
}
//再次查询涂文艳用户
user = service.findUserById(4);
//查看涂文艳用户信息
System.out.println(user);
}
@Test
public void testAddUser(){
//定义用户对象
User user = new User();
//定义用户数据访问对象
UserService service = new UserServiceImpl();
//设置用户属性
user.setUsername("李晓东");
user.setPassword("124578");
user.setTelephone("15478951235");
user.setRegisterTime(new Date());
user.setPopedom(1);
//调用用户数据访问对象的插入方式
int count = service.addUser(user);
//判断用户记录是否插入成功
if (count>0){
System.out.println("恭喜用户插入成功");
System.out.println(user);
}else {
System.out.println("用户插入失败");
}
}
@Test
public void findAllUsers(){
//定义用户数据访问对象
UserService service = new UserServiceImpl();
List<User> users = service.findAllUsers();
//通过增强for循环遍历列表
for (User user : users){
System.out.println(user);
}
}
@Test
public void testFindUserById(){
//定义用户数据访问对象
UserService service = new UserServiceImpl();
//调用用户数据访问对象的操作方法
User user = service.findUserById(1);
//输出用户对象
System.out.println(user);
}
@Test
public void testDeleteUserById(){
//定义用户数据访问对象
UserService service = new UserServiceImpl();
int count = service.deleteUserById(12);
if (count>0){
System.out.println("恭喜,用户记录删除成功");
}else {
System.out.println("用户记录删除失败");
}
}
@Test
public void findUsersByUsername(){
// 定义用户数据访问对象
UserService service = new UserServiceImpl();
// 调用用户数据访问对象方法
List<User> user = service.findUsersByUsername("涂文艳");
System.out.println(user);
}
}
**
## 百度网盘提取
链接:https://pan.baidu.com/s/1i-Yv3nAi8_1Ao-VlPDTAjA
提取码:l0n3
**