mysql数据库操作的测试类
基础,bean,dao,dbutil都写好了,测试dao.impl
测试类
TestUserDaoImpl
package net.lmq.shop.dao.impl;
import net.lmq.bean.User;
import net.lmq.dao.UserDao;
import org.junit.Test;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
public class TestUserDaoImpl {
//登录
@Test
public void testLogin(){
String username,password;
username = "admin";
password = "123456";
//创建数据访问对象
UserDao userDao=new net.lmq.dao.impl.UserDaoImpl();
User user = userDao.login(username,password);
//判断
if(user != null){
System.out.println("恭喜,登录成功");
}else {
System.out.println("抱歉,登录失败");
}
}
//更新
@Test
public void testUpdate(){
//创建用户数据访问对象
UserDao userDao = new net.lmq.dao.impl.UserDaoImpl();
//寻找用户
User user = userDao.findById(4);
//修改密码与电话
user.setPassword("09876");
user.setTelephone("13928374654");
int count = userDao.update(user);
if(count > 0){
System.out.println("恭喜,用户数据修改成功");
}else {
System.out.println("抱歉,用户数据修改失败");
}
System.out.println(user);
}
//插入
@Test
public void testiInsert(){
//创建实例
UserDao userDao = new net.lmq.dao.impl.UserDaoImpl();
User user = new User();
String username = "lmq";
user.setId(4);
user.setUsername(username);
user.setPassword("123");
user.setTelephone("19282737485");
user.setRegisterTime(Timestamp.valueOf(LocalDateTime.now()));
user.setPopedom(1);
List<User> coun = userDao.findByUsername(username);
//判断用户是否已存在
if(coun.size() > 0 ){
System.out.println("已经有此用户"+coun);
}else {
int count = userDao.insert(user);
//判断插入是否成功
if(count >0){
System.out.println("恭喜,用户数据插入成功");
}else {
System.out.println("抱歉,用户数据插入失败");
}
}
}
//通过id删除
@Test
public void testDeleteByid(){
//创建数据访问对象
UserDao userDao = new net.lmq.dao.impl.UserDaoImpl();
int id = 1;
//调用
int count = userDao.deleteById(id);
if(count > 0){
System.out.println("恭喜,用户数据删除成功");
}else {
System.out.println("抱歉,用户数据删除失败");
}
}
//通过id查询用户
@Test
public void testFindByid(){
//创建数据访问对象
UserDao userDao = new net.lmq.dao.impl.UserDaoImpl();
int id = 1;
//调用
User count = userDao.findById(id);
if(count != null){
System.out.println("用户信息:"+count);
}else {
System.out.println("抱歉,没有该用户");
}
}
//通过名字查询用户
@Test
public void testFindByname(){
//创建访问对象
UserDao userDao = new net.lmq.dao.impl.UserDaoImpl();
String username = "赖明庆";
//调用
List<User> users = userDao.findByUsername(username);
if(users.size() >0 ){
System.out.println("用户信息:"+ users);
}else {
System.out.println("抱歉,没有该用户");
}
}
//查询全部用户
@Test
public void testFindAll(){
//创建访问对象
UserDao userDao = new net.lmq.dao.impl.UserDaoImpl();
//调用
List<User> users = userDao.findAll();
if(users.size() > 0){
System.out.println("用户列表:");
for(User user:users){
System.out.println(user);
}
}else {
System.out.println("抱歉,程序错误");
}
}
}
类别:Category
package net.lmq.shop.dao.impl;
import net.lmq.bean.Category;
import net.lmq.dao.CategoryDao;
import net.lmq.dao.impl.CategoryDaoImpl;
import org.junit.Test;
import java.util.List;
public class TestCategoryDaoImpl {
@Test
public void testFindAll(){
//创建类别数据访问对象
CategoryDao categoryDao = new CategoryDaoImpl();
List<Category> categories = categoryDao.findAll();
//判断是否有类别
if(!categories.isEmpty() ){
for(Category category:categories){
System.out.println(category);
}
}else {
System.out.println("没有商品类别");
}
}
//插入类别
@Test
public void testCategoryInsert(){
//创建数据访问对象
CategoryDao categoryDao = new CategoryDaoImpl();
//创建类别信息
Category category = new Category();
category.setId(5);
category.setName("服装");
int count = categoryDao.insert(category);
if(count > 0){
System.out.println("恭喜,插入数据成功");
}else {
System.out.println("抱歉,插入数据失败");
}
}
//删除类别
@Test
public void testCategoryDeleteByid(){
//创建类别数据访问对象
CategoryDao categoryDao = new CategoryDaoImpl();
//id
int id = 6;
//调用
int count = categoryDao.deleteById(id);
//判断删除是否成功
if(count > 0){
System.out.println("删除成功");
}else {
System.out.println("抱歉,删除失败");
}
}
//更新类别
@Test
public void testCategoryUpdate(){
//创建类别数据访问对象
CategoryDao categoryDao = new CategoryDaoImpl();
//创建类别对象
Category category = new Category();
category.setName("电子产品");
category.setId(7);
//调用
int count = categoryDao.update(category);
if(count > 0){
System.out.println("恭喜,更新数据成功");
}else {
System.out.println("抱歉,更新数据失败");
}
}
//按标识符查类别
@Test
public void testCategoryFindByid(){
//创建类别访问对象
CategoryDao categoryDao = new CategoryDaoImpl();
int id = 1;
Category category = categoryDao.findById(id);
if(category !=null){
System.out.println(category);
}else {
System.out.println("抱歉,没有该类别");
}
}
}
商品:Produce
package net.lmq.shop.dao.impl;
import net.lmq.bean.Category;
import net.lmq.bean.Product;
import net.lmq.dao.CategoryDao;
import net.lmq.dao.ProductDao;
import net.lmq.dao.impl.CategoryDaoImpl;
import net.lmq.dao.impl.ProductDaoImpl;
import org.junit.Test;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
public class TestProductDaoImpl {
@Test
public void testFindByCategoryId(){
//创建商品数据访问对象
ProductDao productDao = new ProductDaoImpl();
//定义商品类别编号
int categoryId = 3;
//创建类别数据访问对象
CategoryDao categoryDao = new CategoryDaoImpl();
//判断类别是否存在
if(categoryDao.findById(categoryId) != null){
//获取该类别全部商品
List<Product> products = productDao.findByCategoryId(categoryId);
//判断商品是否存在
if(!products.isEmpty()){
for(Product product:products){
System.out.println(product);
}
}else {
String cattegoryName = categoryDao.findById(categoryId).getName();
System.out.println(cattegoryName+"类别没有商品");
}
}else {
System.out.println("类别编号"+categoryId +"不存在");
}
}
//插入商品
@Test
public void testProduceInsert(){
//创建商品数据访问对象
ProductDao productDao = new ProductDaoImpl();
//创建商品对象
Product product = new Product();
product.setName("计算机");
product.setPrice(112);
product.setAddTime(Timestamp.valueOf(LocalDateTime.now()));
product.setCategoryId(7);
//调用
int count = productDao.insert(product);
if(count >0){
System.out.println("恭喜,插入商品数据成功");
}else {
System.out.println("抱歉,插入商品数据失败");
}
}
//通过id删除商品
@Test
public void testProduceDeleteByid(){
//创建商品数据访问对象
ProductDao productDao = new ProductDaoImpl();
//调用
int id = 1;
int count = productDao.deleteById(id);
if(count >0){
System.out.println("删除商品数据成功");
}else {
System.out.println("错误,删除商品数据失败");
}
}
//更新商品数据
@Test
public void testUpdate(){
//创建商品数据访问对象
ProductDao productDao = new ProductDaoImpl();
//寻找商品
Product product = productDao.findById(3);
product.setPrice(4567);
int count = productDao.update(product);
if(count >0){
System.out.println("更新商品数据成功");
}else {
System.out.println("错误,更新商品数据失败");
}
}
//通过id查找商品
@Test
public void testFindByid(){
//创建商品数据访问对象
ProductDao productDao = new ProductDaoImpl();
//调用
int id = 4;
Product product = productDao.findById(id);
if(product != null){
System.out.println(product);
}else {
System.out.println("没有该商品");
}
}
//查询全部商品
@Test
public void testFindAll(){
//创建商品数据访问对象
ProductDao productDao = new ProductDaoImpl();
//调用
int id = 4;
List<Product> products = productDao.findAll();
if(!products.isEmpty()){
for(Product product:products){
System.out.println(product);
}
}else {
System.out.println("抱歉,没有商品");
}
}
}
订单:Order
package net.lmq.shop.dao.impl;
import com.sun.org.apache.xpath.internal.operations.Or;
import net.lmq.bean.Order;
import net.lmq.bean.Product;
import net.lmq.dao.OrderDao;
import net.lmq.dao.ProductDao;
import net.lmq.dao.impl.OrderDaoImpl;
import net.lmq.dao.impl.ProductDaoImpl;
import org.junit.Test;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
public class TestOrderDaoImpl {
@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("没有订单");
}
}
//插入订单
@Test
public void testInsert(){
//创建订单数据访问对象
OrderDao orderDao = new OrderDaoImpl();
//创建订单对象
Order order = new Order();
order.setUsername("lmq");
order.setTotalPrice(2000);
order.setTelephone("123123122");
order.setOrderTime(Timestamp.valueOf(LocalDateTime.now()));
order.setDeliveryAddress("泸职院");
//调用
int count = orderDao.insert(order);
if(count >0){
System.out.println("订单数据插入成功");
}else{
System.out.println("抱歉,订单数据插入失败");
}
}
//删除
@Test
public void testDeleteByid() {
//创建商品数据访问对象
OrderDao order = new OrderDaoImpl();
//调用
int id = 1;
int count = order.deleteById(id);
if (count > 0) {
System.out.println("删除订单数据成功");
} else {
System.out.println("错误,删除订单数据失败");
}
}
//更新商品数据
@Test
public void testUpdate(){
//创建商品数据访问对象
OrderDao orderDao = new OrderDaoImpl();
//寻找商品
Order order = orderDao.findById(3);
order.setTelephone("12345672132");
order.setTotalPrice(122);
int count = orderDao.update(order);
if(count >0){
System.out.println("更新商品数据成功");
}else {
System.out.println("错误,更新商品数据失败");
}
}
//查询最后一个订单
@Test
public void testListOrder(){
//创建商品数据访问对象
OrderDao orderDao = new OrderDaoImpl();
//调用
List<Order> orders = orderDao.findAll();
int i = orders.size()-1;
Order order = orders.get(i);
System.out.println(order);
}
}
这些 测试类都很相似,先创建数据访问对象,然后调用相应的方法返回值,之后判断是否有值:有输出恭喜,没有输出抱歉