Java web 实训项目报告: Simonshop 西蒙购物网
创建数据库:
创建simonshop项目
创建实体类:
创建数据库工具类ConnectionManager:
连接成功:
创建数据访问接口:
创建数据访问接口实现类:
创建test文件夹 采用Junit来进行单元测试:
在test文件夹里创建net.hw.shop.dao.impl包,在里面创建测试类TestUserDaoImpl:
测试:
TestUserDaoImpl:
testInsert:
@Test
public void testInsert(){
UserDao userDao = new UserDaoImpl();
User user = new User();
user.setId(5);
user.setUsername("望舒");
user.setPassword("99999");
user.setTelephone("10983777061");
user.setRegisterTime(new Date());
user.setPopedom(0);
int count= userDao.insert(user);
//判断添加用户是否成功
if(count>0){
System.out.println("恭喜,用户添加成功!");
}else {
System.out.println("遗憾。用户添加失败!");
}
System.out.println(user);
}
成功:
testLogin:
package net.typ.shop.dao.impl;
import net.typ.shop.bean.User;
import net.typ.shop.dao.UserDao;
import org.junit.Test;
/**
* 功能:测试用户数据访问接口实现类
* 作者:涂意培
* 日期:2019年12月5日
*/
public class TestUserDaoImpl {
@Test
public void testLogin(){
String username,password;
username="admin";
password="123456";
//创建用户数据访问对象
UserDao userDao = new UserDaoImpl();
//调用用户数据访问对象的登录方法
User user = userDao.login(username,password);
//判断用户是否登录成功
if(user != null){
System.out.println("恭喜,登陆成功!");
}else{
System.out.println("遗憾,登录失败!");
}
}
}
开始测试:
修改一下登录密码:
testUpdate:
@Test
public void testUpdata(){
//定义用户数据访问对象
UserDao userDao = new UserDaoImpl();
//找到id为4的用户【涂文艳】
User user = userDao.findById(4);
//修改用户密码和电话号码
user.setPassword("903213");
user.setTelephone("10983777061");
//利用用户数据访问对象的更新方法更新用户
int count = userDao.update(user);
//判断更新用户是否成功
if(count>0){
System.out.println("恭喜,用户更新成功!");
}else {
System.out.println("遗憾。用户更新失败!");
}
//再次查询Id为4的用户
user = userDao.findById(4);
//输出该用户的信息
System.out.println(user);
}
测试结果:
成功:
修改测试代码:
失败:
testDeleteById:
@Test
public void testDeleteById(){
//定义用户数据访问对象
UserDao userDao = new UserDaoImpl();
int count = userDao.deleteById(4);
if(count>0){
System.out.println("恭喜,用户删除成功!");
}else {
System.out.println("遗憾。用户删除失败!");
}
}
测试:
删除成功:
修改代码:
删除失败
testFindById:
@Test
public void testFindById(){
//定义用户数据访问对象
UserDao userDao = new UserDaoImpl();
User user = userDao.findById(3);
if(user != null){
System.out.println("成功找到");
System.out.println(user);
}else{
System.out.println("没找到");
}
}
测试:
修改代码再次测试:
testFindAll:
@Test
public void testFindAll(){
UserDao userDao = new UserDaoImpl();
List<User> users = userDao.findAll();
if(users.size() > 0){
for(User user:users){
System.out.println(user);
}
}else{
System.out.println("没有用户信息");
}
}
测试:
清空表记录
testFindByUsername:
@Test
public void testFindByUsername() {
//定义用户数据访问对象
List<User> user = new ArrayList<User>();
UserDao userDao = new UserDaoImpl();
user = userDao.findByUsername("温志军");
System.out.println(user);
}
测试:
TestCategroyDaoImpl:
testFindAll:
@Test
public void testFindAll(){
CategoryDao categoryDao = new CategoryDaoImpl();
List<Category> categories = categoryDao.findAll();
if(categories.size() > 0){
for(Category category:categories){
System.out.println(category);
}
}else{
System.out.println("没有商品类别");
}
}
测试:
删除所有类别表记录:
再次进行测试:
testInsert:
@Test
public void testInsert(){
CategoryDao categoryDao = new CategoryDaoImpl();
Category category = new Category();
category.setName("游戏产品");
int count= categoryDao.insert(category);
//判断添加用户是否成功
if(count>0){
System.out.println("恭喜,添加成功!");
}else {
System.out.println("遗憾,添加失败!");
}
System.out.println(category);
}
数据库中:
testDeleteById:
@Test
public void testDeleteById(){
//定义用户数据访问对象
CategoryDao categoryDao = new CategoryDaoImpl();
int count = categoryDao.deleteById(5);
if(count>0){
System.out.println("恭喜,用户删除成功!");
}else {
System.out.println("遗憾。用户删除失败!");
}
}
数据库中:
修改代码:
失败:
testUpdate:
@Test
public void testUpdata(){
CategoryDao categoryDao = new CategoryDaoImpl();
Category category = categoryDao.findById(1);
category.setName("数码电器");
//category.setId(6);
int count = categoryDao.update(category);
//判断更新用户是否成功
if(count>0){
System.out.println("恭喜,更新成功!");
}else {
System.out.println("遗憾,更新失败!");
}
category = categoryDao.findById(1);
System.out.println(category);
}
修改代码:
testFindById
@Test
public void testFindById(){
//定义用户数据访问对象
CategoryDao categoryDao = new CategoryDaoImpl();
Category category = categoryDao.findById(1);
if(category != null){
System.out.println("成功找到");
System.out.println(category);
}else{
System.out.println("没找到");
}
}
修改代码再次测试:
TestProductDaoImpl
testFindByCategoryId
@Test
public void testFindByCategoryId(){
ProductDao productDao = new ProductDaoImpl();
int catagoryId = 1;
CategoryDao categoryDao = new CategoryDaoImpl();
if(categoryDao.findById(catagoryId) != null){
String categoryName = categoryDao.findById(catagoryId).getName();
List<Product> products = productDao.findByCategoryId(catagoryId);
if(!products.isEmpty()){
for (Product product:products){
System.out.println(product);
}
}else{
System.out.println("[" + categoryName + "]类别没有商品!");
}
}else{
System.out.println("类别编号{" + catagoryId + "]不存在");
}
}
测试:
修改代码:
再次测试:
testInsert
@Test
public void testInsert(){
ProductDao productDao = new ProductDaoImpl();
Product product = new Product();
product.setName("Apple手机");
product.setPrice(5499);
product.setAddTime(new Date());
int count= productDao.insert(product);
//判断添加用户是否成功
if(count>0){
System.out.println("恭喜,添加成功!");
}else {
System.out.println("遗憾,添加失败!");
}
System.out.println(product);
}
测试:
修改代码:
testDeleteById
@Test
public void testDeleteById(){
//定义用户数据访问对象
ProductDao productDao = new ProductDaoImpl();
int count = productDao.deleteById(2);
if(count>0){
System.out.println("恭喜,删除成功!");
}else {
System.out.println("遗憾,删除失败!");
}
}
测试:
修改代码 再次测试
testUpdate:
恢复数据库内容:
@Test
public void testUpdata(){
ProductDao productDao = new ProductDaoImpl();
Product product = productDao.findById(1);
product.setName("数码电器");
//product.setId(6);
int count = productDao.update(product);
//判断更新用户是否成功
if(count>0){
System.out.println("恭喜,更新成功!");
}else {
System.out.println("遗憾,更新失败!");
}
product = productDao.findById(1);
System.out.println(product);
}
修改代码:
testFindById
@Test
public void testFindById(){
//定义用户数据访问对象
ProductDao productDao = new ProductDaoImpl();
Product product = productDao.findById(2);
if(product != null){
System.out.println("成功找到");
System.out.println(product);
}else{
System.out.println("没找到");
}
}
修改代码再次测试:
testFindAll
@Test
public void testFindAll(){
ProductDao productDao = new ProductDaoImpl();
List<Product> products = productDao.findAll();
if(products.size() > 0){
for(Product product:products){
System.out.println(product);
}
}else{
System.out.println("没有商品类别");
}
}
测试:
删除表的所有内容:
再次测试:
TestOrderDaoImpl
testFindAll
@Test
public void testFindAll(){
OrderDao orderDao = new OrderDaoImpl();
List<Order> orders = orderDao.findAll();
if(orders.size() > 0){
for(Order order:orders){
System.out.println(order);
}
}else{
System.out.println("没有订单");
}
}
测试:
删除全部订单记录:
再次测试:
testInsert
@Test
public void testInsert(){
OrderDao orderDao = new OrderDaoImpl();
Order order = new Order();
order.setUsername("张晓刚");
order.setTelephone("12345678978");
order.setTotalPrice(5800);
order.setDeliveryAddress("泸职院信息工程系");
order.setOrderTime(new Date());
int count= orderDao.insert(order);
if(count>0){
System.out.println("恭喜,添加成功!");
}else {
System.out.println("遗憾,添加失败!");
}
System.out.println(order);
}
测试:
testDeleteById:
@Test
public void testDeleteById(){
//定义用户数据访问对象
OrderDao orderDao = new OrderDaoImpl();
int count = orderDao.deleteById(1);
if(count>0){
System.out.println("恭喜,删除成功!");
}else {
System.out.println("遗憾,删除失败!");
}
}
修改代码:
再次测试 删除失败:
testUpdate
@Test
public void testUpdate(){
OrderDao orderDao = new OrderDaoImpl();
Order order= orderDao.findById(1);
order.setUsername("韩梅梅");
order.setTelephone("98765432123");
int count = orderDao.update(order);
//判断更新用户是否成功
if(count>0){
System.out.println("恭喜,更新成功!");
}else {
System.out.println("遗憾,更新失败!");
}
order = orderDao.findById(1);
System.out.println(order);
}
数据库中:
修改代码再次测试
testFindById
@Test
public void testFindById(){
//定义用户数据访问对象
OrderDao orderDao = new OrderDaoImpl();
Order order = orderDao.findById(1);
if(order != null){
System.out.println("成功找到");
System.out.println(order);
}else{
System.out.println("没找到");
}
}
修改代码再次测试:
Service
TestUserService:
testLogin:
@Test
public void testLogin(){
String username,password;
username = "admin";
password="12345";
UserService userService = new UserService();
User user = userService.login(username,password);
if (user != null){
System.out.println("登陆成功");
}else {
System.out.println("登录失败!");
}
}
测试:
修改代码:
testAddUser
@Test
public void testAddUser(){
UserService userService = new UserService();
User user = new User();
user.setId(5);
user.setUsername("王二狗");
user.setPassword("1234567");
user.setTelephone("12345678978");
user.setRegisterTime(new Date());
user.setPopedom(1);
List<User> users = userService.findAllUsers();
int flay = 0;
for (User user1:users) {
if (user1.getUsername().equals(user.getUsername())) {
flay = 1;
break;
}
}
if (flay == 0){
int count = userService.addUser(user);
if (count > 0) {
System.out.println("插入成功!");
} else {
System.out.println("插入失败!");
}
}else {
System.out.println("有相同的名字插入失败!");
}
}
测试:
修改代码:
testDeleteUserById:
@Test
public void testDeleteUserById(){
UserService userService = new UserService();
int index = 5;
int count = userService.deleteUserById(index);
if (count > 0) {
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
}
测试:
修改代码:
testUpdateUser:
@Test
public void testUpdateUser(){
UserService userService = new UserService();
User user = userService.findUserById(4);
user.setPassword("987654");
user.setTelephone("98745632136");
int count = userService.updateUser(user);
if (count > 0) {
System.out.println("更新成功!");
}else {
System.out.println("更新失败!");
}
}
测试:
修改代码:
testFindUsersByUsername:
@Test
public void testFindUsersByUsername(){
UserService userService = new UserService();
String name = "admin";
List<User> users = userService.findUsersByUsername(name);
if (users.size() > 0){
for (User user:users){
System.out.println(user);
}
}else {
System.out.println("查询失败");
}
}
测试:
修改代码:
testFindAllUsers:
@Test
public void testFindAllUsers(){
UserService userService = new UserService();
List<User> users = userService.findAllUsers();
if (users.size()>0){
for (User user:users){
System.out.println(user);
}
}else {
System.out.println("查询失败");
}
}
测试:
修改代码:
清空表内容:
testFindUserById
@Test
public void testFindUserById(){
UserService userService = new UserService();
User user = userService.findUserById(4);
if(user != null){
System.out.println("成功找到");
System.out.println(user);
}else{
System.out.println("没找到");
}
}
测试:
修改代码再次测试:
TestCategoryService:
testAddCategory:
@Test
public void testAddCategory(){
Category category = new Category();
category.setId(5);
category.setName("AirPods Pro");
CategoryService categoryService = new CategoryService();
int flay = 0;
List<Category> categories = categoryService.findAllCategories();
for (Category category1:categories){
if(category1.getName().equals(category.getName())){
flay =1;
break;
}
}
if (flay == 0){
int count = categoryService.addCategory(category);
if (count > 0) {
System.out.println("插入成功!");
} else {
System.out.println("插入失败!");
}
}else {
System.out.println("插入失败!有相同的名称的");
}
}
测试:
修改代码:
testDeleteCategoryById:
@Test
public void testDeleteCategoryById(){
int Id = 5;
CategoryService categoryService = new CategoryService();
int count = categoryService.deleteCategoryById(Id);
if (count > 0) {
System.out.println("删除成功!");
} else {
System.out.println("删除失败!");
}
}
测试:
修改代码:
testUpdateCategory:
@Test
public void testUpdateCategory(){
CategoryService categoryService = new CategoryService();
Category category = categoryService.findCategoryById(2);
category.setName("AirPods");
int count = categoryService.updateCategory(category);
if (count > 0) {
System.out.println("更新成功!");
} else {
System.out.println("更新失败!");
}
}
修改代码:
再次测试:
testFindAllCategories:
@Test
public void testFindAllCategories(){
CategoryService categoryService = new CategoryService();
List<Category> categorys = categoryService.findAllCategories();
if (categorys.size() > 0){
for (Category category:categorys){
System.out.println(category);
}
}else {
System.out.println("没有订单");
}
}
测试:
删除表内容:
再次测试
testFindCategoryById
@Test
public void testFindCategoryById(){
CategoryService categoryService = new CategoryService();
Category category = categoryService.findCategoryById(2);
if(category != null){
System.out.println("成功找到");
System.out.println(category);
}else{
System.out.println("未找到");
}
}
测试:
修改代码再次测试:
TestProductService:
testAddProduct
@Test
public void testAddProduct(){
ProductService productService = new ProductService();
Product product = new Product();
product.setId(16);
product.setName("毛笔");
product.setPrice(15);
product.setAddTime(new Date());
product.setCategoryId(3);
List<Product> products = productService.findAllProducts();
int flay = 0;
for (Product product1:products) {
if (product1.getName().equals(product.getName())) {
flay = 1;
break;
}
}
if (flay == 0){
int count = productService.addProduct(product);
if (count > 0) {
System.out.println("插入成功!");
} else {
System.out.println("插入失败!");
}
}else {
System.out.println("有相同的名字插入失败!");
}
}
测试:
修改代码:
testDeleteProductById:
@Test
public void testDeleteProductById(){
int Id = 16;
ProductService productService = new ProductService();
int count = productService.deleteProductById(Id);
if (count > 0) {
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
}
测试:
修改代码:
再次测试:
testUpdateProduct:
@Test
public void testUpdateProduct(){
ProductService productService = new ProductService();
Product product = productService.findProductById(3);
product.setName("墨水");
int count = productService.updateProduct(product);
if (count > 0) {
System.out.println("更新成功!");
}else {
System.out.println("更新失败!");
}
}
测试:
修改代码再次测试:
testFindById:
@Test
public void testFindById(){
//定义用户数据访问对象
ProductService productService = new ProductService();
Product product = productService.findProductById(2);
if(product != null){
System.out.println("成功找到");
System.out.println(product);
}else{
System.out.println("没找到");
}
修改代码再次测试
testFindProductsByCategoryId
@Test
public void testFindProductsByCategoryId(){
//创建商品数据访问对象
ProductService productService = new ProductService();
int categoryId = 1;
CategoryService categoryService = new CategoryService();
if (categoryService.findCategoryById(categoryId)!= null){
String categoryName = categoryService.findCategoryById(categoryId).getName();
List<Product> products = productService.findProductsByCategoryId(categoryId);
if (!products.isEmpty()){
for (Product product:products){
System.out.println(product);
}
}else {
System.out.println("["+categoryName+"]类别没有商品!");
}
}
}
修改代码:
清空表内容
testFindAllProducts
@Test
public void testFindAllProducts(){
ProductService productService = new ProductService();
List<Product> productDaos = productService.findAllProducts();
if (productDaos.size() > 0){
for (Product product:productDaos){
System.out.println(product);
}
}else {
System.out.println("查询失败!");
}
}
删除表内容再次测试:
TestOrderService
@Test
public void testAddOrder(){
Order order = new Order();
order.setId(3);
order.setUsername("程佳佳");
order.setTotalPrice(5000);
order.setTelephone("15898745632");
order.setDeliveryAddress("泸职院信工院");
order.setOrderTime(new Date());
OrderService orderService = new OrderService();
int flay = 0;
List<Order> orders = orderService.findAllOrders();
for (Order order1:orders){
if (order1.getUsername().equals(order.getUsername())){
flay =1;
break;
}
}
if (flay == 0){
int count = orderService.addOrder(order);
if (count > 0) {
System.out.println("插入成功!");
} else {
System.out.println("插入失败!");
}
}else {
System.out.println("有相同的名字插入失败!");
}
}
testAddOrder
修改代码:
testDeleteOrderById
@Test
public void testDeleteOrderById(){
int id = 3;
OrderService orderService = new OrderService();
int count = orderService.deleteOrderById(id);
if (count > 0) {
System.out.println("删除成功!");
} else {
System.out.println("删除失败!");
}
}
修改代码;
testUpdateOrder
@Test
public void testUpdateOrder(){
OrderService orderService = new OrderService();
Order order = orderService.findOrderById(1);
order.setUsername("张甜甜");
int count = orderService.updateOrder(order);
if (count > 0) {
System.out.println("更新成功!");
} else {
System.out.println("更新失败!");
}
}
测试:
修改代码再次测试
testFindOrderById:
@Test
public void testFindOrderById() {
OrderService orderService = new OrderService();
Order order = orderService.findOrderById(1);
if(order != null){
System.out.println("成功找到");
System.out.println(order);
}else{
System.out.println("没找到");
}
}
修改代码再次测试
testFindLast
@Test
public void testFindLast(){
OrderService orderService = new OrderService();
Order order = orderService.findLastOrder();
System.out.println(order);
}
测试:
testFindAllOrders:
@Test
public void testFindAllOrders(){
OrderService orderService = new OrderService();
List<Order> orders = orderService.findAllOrders();
if (orders.size() > 0){
for (Order order:orders){
System.out.println(order);
}
}else {
System.out.println("没有订单");
}
}
测试:
删除表内容再次测试:
上一篇: Java Web实训项目:西蒙购物网(Simonshop)
下一篇: 京东移动端页面布局(二)