学无先后,达者为师

网站首页 编程语言 正文

Mybatisplus的增删改查

作者:宣布无人罪 更新时间: 2023-12-18 编程语言

01 Mybatis-plus的CRUD

  • Mybatis-plus实现了单表的增删改查

  • create 添加数据 read读取数据 update 修改数据 delete删除数据

02 简单的mybatis-plus方法

  • 测试类

    public class Category {
        @TableId
        private Long categoryId;
        private String categoryName;
        private String categoryPicture1;
        private String categoryPicture2;
    }
    

1.增加insert

// 向数据库插入一条记录
 	Category category=new Category();
   	category.setCategoryName("电脑");
    Long num=new Long(100);
    category.setCategoryId(num);
    boolean add=iCategoryService.save(category);

2.删除delete

// 根据 ID 删除
	Category category=new Category();
   category.setCategoryName("机器人");
   Long num=new Long(100);
   category.setCategoryId(num);
   boolean delete=iCategoryService.removeById(category);
//或者直接传主键也是可以删除的
   boolean delete=iCategoryService.removeById(new Long(100));

3.修改update

//根据ID修改		
	Category category=new Category();
    category.setCategoryName("机器人");
    Long num=new Long(100);
  	category.setCategoryId(num);
  	 boolean update=iCategoryService.updateById(category);

4.查找select

// 查找对应数据库所有数据
	List<Category> categories=iCategoryService.list();
// 根据表主键查询一条数据
	Category category=iCategoryService.getById(new Long(100));
// 根据数据库批量查询
     LinkedList<Long> list = new LinkedList<>();
     list.add(new Long(1));
     list.add(new Long(2));
     list.add(new Long(3));
     List<Category> categoryList=iCategoryService.listByIds(list);

原文链接:https://blog.csdn.net/2302_77182979/article/details/134471698

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新