Mybatis-Plus BaseMapper接口方法示例

@SpringBootTest
class MybatisPlusParentApplicationTests {

    @Autowired
    private DataSource dataSource;

    @Autowired
    private UserDao userDao;

    @Test
    void contextLoads() throws SQLException {
        Connection connection = dataSource.getConnection();
        System.out.println("connection = " + connection);
    }

    // selectOne selectMapsPage selectPage selectBatchIds selectById selectList selectCount selectMaps selectByMap

    @Test
    void selectById() {
        User user = userDao.selectById(1);
        System.out.println(user);
    }

    @Test
    void selectList() {
        List<User> users = userDao.selectList(null);
        users.forEach(System.out::println);
    }

    @Test
    void selectCount() {
        Long aLong = userDao.selectCount(null);
        System.out.println(aLong);
    }

    /**
     * 根据 Map 作为条件删除,多个 key 的话需要全部满足
     */
    @Test
    void selectByMap() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", 100);
        map.put("age", 100);
        List<User> userList = userDao.selectByMap(map);
        userList.forEach(System.out::println);
    }

    @Test
    void selectBatchIds() {
        List<Integer> asList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        List<User> userList = userDao.selectBatchIds(asList);
        userList.forEach(System.out::println);
    }

    @Test
    void selectPage() {
        IPage<User> page = Page.of(2, 5);
        page = userDao.selectPage(page, null);

        long total = page.getTotal();
        System.out.println("total = " + total);
    }

    @Test
    void selectMapsPage() {
        Page<Map<String, Object>> mapPage = userDao.selectMapsPage(new Page<>(2, 5), null);
        List<Map<String, Object>> records = mapPage.getRecords();
        records.forEach(System.out::println);
    }

    @Test
    void selectOne() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", 4);
        User user = userDao.selectOne(queryWrapper);
        System.out.println(user);
    }

    @Test
    void selectMaps() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("id", 50);
        List<Map<String, Object>> maps = userDao.selectMaps(queryWrapper);
        maps.forEach(System.out::println);
    }

    // update

    @Test
    void update() {
        User user = new User("username", "password", 30);
        int update = userDao.update(user, null);
        System.out.println(update);
    }

    @Test
    void updateById() {
        User user = new User();
//        user.setId("1439416803823353858");
//        user.setName("年少有为的帐号");
//        user.setPass("年少有为的密码");
//        int update = userDao.updateById(user);
//        System.out.println(update);
    }


    // insert

    @Test
    void insert() {
        User user = new User(UUID.randomUUID().toString(),UUID.randomUUID().toString(), 18);
        int insert = userDao.insert(user);
        System.out.println("insert = " + insert);
    }


    // delete

    @Test
    void delete() {
        int delete = userDao.delete(null);
        System.out.println(delete);
    }

    @Test
    void deleteById() {
//        String id = "1439470937482104834";
//        int i = userDao.deleteById(id);
//        System.out.println(i);

//        User user = new User();
//        user.setId("67c6a27001b2ddf398cc831edd768944");
//        int i = userDao.deleteById(user);
//        System.out.println(i);
    }

    @Test
    void deleteMaps() {
        Map<String, Object> map = new HashMap<>();
        map.put("id", 1);
        int i = userDao.deleteByMap(map);
        System.out.println(i);
    }

    @Test
    void deleteBatchIds() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        int i = userDao.deleteBatchIds(list);
        System.out.println("i = " + i);
    }

}

发表回复

相关

浙ICP备2021031744号-3