import com.example.egmybatis1.mapper.CategoryMapper;
import com.example.egmybatis1.model.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.Test;
@SpringBootTest
public class CategoryTest {
@Autowired
private CategoryMapper categoryMapper;
@Test
public void addCategory() {
Category category = new Category();
category.setName("分类名称");
category.setIntro("分类介绍");
categoryMapper.addCategory(category);
System.out.println("分类添加成功,分类ID为:" + category.getId());
}
@Test
public void getAllCategories() {
List<Category> categories = categoryMapper.getAllCategories();
System.out.println("所有分类信息:");
for (Category category : categories) {
System.out.println(category);
}
}
@Test
public void getCategoryById() {
int categoryId = 1; // 修改为你要查询的分类的ID
Category category = categoryMapper.getCategoryById(categoryId);
if (category != null) {
System.out.println("分类信息:" + category);
} else {
System.out.println("分类不存在");
}
}
@Test
public void updateCategoryById() {
int categoryId = 1; // 修改为你要修改的分类的ID
Category category = categoryMapper.getCategoryById(categoryId);
if (category != null) {
category.setName("修改后的分类名称");
category.setIntro("修改后的分类介绍");
categoryMapper.updateCategory(category);
System.out.println("分类修改成功");
} else {
System.out.println("分类不存在");
}
}
@Test
public void deleteCategoryById() {
int categoryId = 1; // 修改为你要删除的分类的ID
int rowsAffected = categoryMapper.deleteCategory(categoryId);
if (rowsAffected > 0) {
System.out.println("分类删除成功");
} else {
System.out.println("分类不存在");
}
}
}