MybatisPlus-CRUD,不带条件构造器的常用方法

mapper层
@Repository
public interface UserMapper extends BaseMapper 

BaseMapper中封装好了增删改查的方法

后面直接调用就好了

测试类

@SpringBootTest
public class CrudTest {
    @Autowired
    private UserMapper userMapper;
    //新增
    @Test
    public void insert(){//没有返回值,不需要调用
        User user =new User();
        user.setAge(23);
        user.setName("llx");
        user.setEmail("[email protected]");
        int result = userMapper.insert(user);//新增一条数据,需要创建对象,赋值
        System.out.println(result);

    }
    //根据id删除
    @Test
    public void deleteById(){
        userMapper.deleteById("1687729477728641025");
    }
    //根据Map类型删除
    @Test
    public void deleteByMap(){
        Map map = new HashMap<>();
        map.put("name","Billie");//设置根据哪些值删除
        map.put("age",24);
        userMapper.deleteByMap(map);//需要map作为参数
    }
    //批量删除
    @Test
    public void deletByCatchId(){
        List list = Arrays.asList(1L, 2L);//数据库是long类型的
        //Arrays.asList(1L, 2L)将数据转换为List集合
        int result = userMapper.deleteBatchIds(list);
        System.out.println(result);
    }
    //通过id修改
    @Test
    public void UpdateById(){
        User user = new User();
        user.setId(3L);//需要设置id
        user.setAge(30);
        int result = userMapper.updateById(user);
        System.out.println(result);
    }
    //通过对id进行查询
    @Test
    public void testSelectById(){
        User user = userMapper.selectById(3L);
        System.out.println(user);
    }
    //批量查询,注意Arrays.asList(3L, 4L),这个是把要查询的id放到数组里
    @Test
    public void testSelectByBatchIds(){
        List list = Arrays.asList(3L, 4L);
        List users = userMapper.selectBatchIds(list);
        System.out.println(users);
    }
    //通过map进行查询
    @Test
    public void testSelectBymaps(){
        Map map = new HashMap<>();
        map.put("name","Sandy");
        map.put("age",21);
        List users = userMapper.selectByMap(map);//需要创建map对象
    }
    //查询全部,有条件构造器,查询全部可以用null
    @Test
    public void testSelectByAll(){
        List users = userMapper.selectList(null);//条件构造器,没有条件的时候可以使用null
        users.forEach(System.out::println);
    }
}

-----------------------------------------------------------------

public interface UserService extends IService {
}
@Service//标识为一个组件
public class UserServiceImpl extends ServiceImpl implements UserService{
}

ServiceImpl中封装了方法(特别注意批量添加只有serviceImpl中有)

测试类

@SpringBootTest
public class MybatisPlusServiceTest {
    @Autowired
    private UserService userService;
    @Test
    public void testCount(){
        long count = userService.count();
        System.out.println("总记录数为:"+count);
    }
    //测试批量添加,只有service层中有批量添加
    //userService.saveOrUpdate();有id修改,无id添加
    @Test
    public void testInsertMore(){
        List list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setName("abc"+i);
            user.setAge(20+i);
            list.add(user);
        }
        boolean b = userService.saveBatch(list);
        System.out.println(b);
    }
}

你可能感兴趣的:(MybatisPlus,java,开发语言)