最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MyBatis-Plus的基本CRUD操作大全

 更新時間:2026年02月27日 09:21:46   作者:空空kkk  
本文介紹了MyBatis-Plus框架中BaseMapper和IService兩大核心組件的使用,,通過示例代碼演示了如何實現數據插入、刪除、修改、查詢等操作,并展示了條件構造器、分頁查詢、事務處理等實用功能,感興趣的朋友跟隨小編一起看看吧

一、BaseMapper

        MyBatis-Plus中的基本CRUD在內置的BaseMapper中都已得到了實現,我們可以直接使用,接口如

public interface BaseMapper<T> extends Mapper<T> {
    int insert(T entity);
    int deleteById(Serializable id);
    int deleteById(T entity);
    int deleteByMap(@Param("cm") Map<String, Object> columnMap);
    int delete(@Param("ew") Wrapper<T> queryWrapper);
    int deleteBatchIds(@Param("coll") Collection<?> idList);
    int updateById(@Param("et") T entity);
    int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
    T selectById(Serializable id);
    List<T> selectBatchIds(@Param("coll") Collection<? extends Serializable> idList);
    List<T> selectByMap(@Param("cm") Map<String, Object> columnMap);
    default T selectOne(@Param("ew") Wrapper<T> queryWrapper) {
        List<T> ts = this.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(ts)) {
            if (ts.size() != 1) {
                throw ExceptionUtils.mpe("One record is expected, but the query result is multiple records", new Object[0]);
            } else {
                return ts.get(0);
            }
        } else {
            return null;
        }
    }
    default boolean exists(Wrapper<T> queryWrapper) {
        Long count = this.selectCount(queryWrapper);
        return null != count && count > 0L;
    }
    Long selectCount(@Param("ew") Wrapper<T> queryWrapper);
    List<T> selectList(@Param("ew") Wrapper<T> queryWrapper);
    List<Map<String, Object>> selectMaps(@Param("ew") Wrapper<T> queryWrapper);
    List<Object> selectObjs(@Param("ew") Wrapper<T> queryWrapper);
    <P extends IPage<T>> P selectPage(P page, @Param("ew") Wrapper<T> queryWrapper);
    <P extends IPage<Map<String, Object>>> P selectMapsPage(P page, @Param("ew") Wrapper<T> queryWrapper);
}

1. 插入

        注意:MyBatis-Plus在實現插入數據時,會默認基于雪花算法的策略生成id

    /**
     * 用戶插入功能的方法。
     * 該方法創(chuàng)建一個User對象并將其插入數據庫,驗證插入操作是否成功,
     * 并輸出受影響的行數以及自動生成的用戶ID。
     */
    @Test
    public void testInsert(){
        // 創(chuàng)建一個新的User對象,ID為null表示由數據庫自動生成
        User user = new User(null, "張三", 23, "zhangsan@qcby.com",null,null);
        // 執(zhí)行插入操作,將用戶數據保存到數據庫中
        //INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
        int result = userMapper.insert(user);
        // 輸出插入操作影響的行數,正常情況下應為1
        System.out.println("受影響行數:"+result);
        // 輸出數據庫自動生成的用戶ID,MyBatis-Plus在實現插入數據時,會默認基于雪花算法的策略生成id
        //2020478658078167042
        System.out.println("id自動獲?。?+user.getId());
    }

2. 刪除

    /**
     * 通過ID刪除用戶信息的功能。
     * 該方法調用userMapper的deleteById方法,傳入指定的用戶ID進行刪除操作,
     * 并輸出受影響的行數以驗證刪除結果。
     */
    @Test
    public void testDeleteById(){
        //通過id刪除用戶信息
        //DELETE FROM user WHERE id=?
        int result = userMapper.deleteById(2020488576042545170L);
        System.out.println("受影響行數:"+result);
    }
    @Test
    public void testDeleteBatchIds(){
        //通過多個id批量刪除
        //DELETE FROM user WHERE id IN ( ? , ? )
        List<Long> idList = Arrays.asList(1L, 3L);
        int result = userMapper.deleteBatchIds(idList);
        System.out.println("受影響行數:"+result);
    }
    /**
     * 通過Map條件刪除用戶記錄的功能。
     * 該方法構造一個包含刪除條件的Map對象,并調用userMapper的deleteByMap方法執(zhí)行刪除操作,
     * 最后輸出受影響的行數。
     * @param map 包含刪除條件的鍵值對集合,其中key為字段名,value為對應的條件值。。
     * @return 受影響的行數,表示成功刪除的記錄數量。
     */
    @Test
    public void testDeleteByMap(){
        //根據map集合中所設置的條件刪除記錄
        // 構造刪除條件的Map集合
        //DELETE FROM user WHERE name = ? AND age = ?
        Map<String, Object> map = new HashMap<>();
        map.put("age", 23);
        map.put("name", "張三");
        int result = userMapper.deleteByMap(map);
        System.out.println("受影響行數:"+result);
    }

3. 修改

    /**
     * 通過ID更新用戶信息的功能。
     * 該方法創(chuàng)建一個User對象,并調用userMapper的updateById方法來更新數據庫中的對應記錄。
     * 最后輸出受影響的行數。
     */
    @Test
    public void testUpdateById(){
        //創(chuàng)建User對象,并設置要修改的屬性,null不傳參
        User user = new User(4L, "test", null, null, null, null);
        //UPDATE user SET name=?, age=? WHERE id=?
        int result = userMapper.updateById(user);
        System.out.println("受影響行數:"+result);
    }

4. 查詢

    /**
     * 通過ID查詢用戶信息的功能。
     * 該方法調用userMapper的selectById方法,傳入指定的用戶ID進行查詢,
     * 并將查詢結果打印到控制臺。
     */
    @Test
    public void testSelectById(){
        //根據id查詢用戶信息
        //SELECT id,name,age,email FROM user WHERE id=?
        User user = userMapper.selectById(4L);
        System.out.println(user);
    }
    /**
     * 通過多個ID批量查詢用戶信息的功能。
     * 該方法調用userMapper的selectBatchIds方法,傳入指定的用戶ID列表進行批量查詢,
     * 并將查詢結果打印到控制臺。
     */
    @Test
    public void testSelectBatchIds(){
        //根據id批量查詢
        //SELECT id,name,age,email FROM user WHERE id IN ( ? , ? )
        List<User> list = userMapper.selectBatchIds(Arrays.asList(4, 2));
        list.forEach(System.out::println);
    }
    /**
     * 通過Map條件查詢用戶信息列表的功能。
     * 該方法構造一個包含查詢條件的Map對象,并調用userMapper的selectByMap方法執(zhí)行查詢操作,
     * 最后將查詢結果打印到控制臺。
     */
    @Test
    public void testSelectByMap(){
        //根據map查詢用戶信息,此時null傳參
        //SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
        Map<String, Object> map = new HashMap<>();
        map.put("name", "Jack");
        map.put("age", 20);
        List<User> list = userMapper.selectByMap(map);
        list.forEach(System.out::println);
    }
    /**
     * 通過條件查詢用戶信息列表的功能。
     * 該方法創(chuàng)建一個User對象,并調用userMapper的selectOne方法來查詢數據庫中的對應記錄。
     * 最后將查詢結果打印到控制臺。
     */
    @Test
    public void testSelectOne(){
        //查詢用戶信息,根據條件查詢,最多只能查詢一條
        //SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
        User user = new User();
        user.setName("Jack");
        user.setAge(20);
        User one = userMapper.selectOne(new QueryWrapper<>(user));
        System.out.println(one);
    }
    /**
     * 查詢所有數據selectList()功能。
     * 該方法通過調用userMapper的selectList()方法查詢數據,
     * 并將查詢結果打印到控制臺。
     * selectList()方法根據MyBatis-Plus內置的條件構造器查詢一個list集合。
     * 傳入null表示沒有查詢條件,即查詢所有數據。
     */
    @Test
    public void testSelectList(){
        //selectList()根據MP內置的條件構造器查詢一個list集合,null表示沒有條件,即查詢所有
        userMapper.selectList( null).forEach(System.out::println);
    }

5. 其他重要接口

    // selectPage(分頁查詢 + 復雜條件)
    @Test
    public void testSelectPage() {
        // 分頁參數:第2頁,每頁3條
        Page<User> page = new Page<>(2, 3);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.gt("age", 18) // 年齡>18
                .like("email", "@test.com") // 郵箱包含@test.com
                .orderByDesc("age"); // 按年齡降序
        IPage<User> userIPage = userMapper.selectPage(page, queryWrapper);
        System.out.println("selectPage-總條數:" + userIPage.getTotal());
        System.out.println("selectPage-當前頁數據:");
        userIPage.getRecords().forEach(System.out::println);
    }
    // selectMapsPage(分頁查詢,返回Map集合,僅查指定字段)
    @Test
    public void testSelectMapsPage() {
        Page<Map<String, Object>> page = new Page<>(1, 5);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("uid", "name", "age") // 只查3個字段
                .between("age", 18, 30); // 年齡在18-30之間
        IPage<Map<String, Object>> mapIPage = userMapper.selectMapsPage(page, queryWrapper);
        System.out.println("selectMapsPage-總頁數:" + mapIPage.getPages());
        System.out.println("selectMapsPage-當前頁Map數據:");
        mapIPage.getRecords().forEach(System.out::println);
    }
    // selectObjs(僅查詢指定字段,返回Object集合,適合單字段查詢)
    @Test
    public void testSelectObjs() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("name") // 只查name字段
                .eq("age", 20);
        List<Object> nameList = userMapper.selectObjs(queryWrapper);
        System.out.println("selectObjs-僅查詢name字段結果:");
        nameList.forEach(System.out::println);
    }
    // update(UpdateWrapper條件更新,非ID更新)
    @Test
    public void testUpdateWithWrapper() {
        User user = new User();
        user.setEmail("update_wrapper@test.com"); // 要更新的字段
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("name", "張三")
                .gt("age", 18); // 條件:name=張三 且 age>18
        int rows = userMapper.update(user, updateWrapper);
        System.out.println("testUpdateWithWrapper-影響行數:" + rows);
    }
    // update(鏈式UpdateWrapper,直接設置更新字段,無需new User)
    @Test
    public void testUpdateForSet() {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("uid", 1L)
                .set("age", 28) // 直接設置要更新的字段和值
                .set("email", "update_set@test.com");
        int rows = userMapper.update(null, updateWrapper);
        System.out.println("testUpdateForSet-影響行數:" + rows);
    }
    // delete(QueryWrapper條件刪除,支持復雜條件)
    @Test
    public void testDeleteWithWrapper() {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", "測試刪除")
                .lt("age", 18); // 條件:name含測試刪除 且 age<18
        int rows = userMapper.delete(queryWrapper);
        System.out.println("testDeleteWithWrapper-影響行數:" + rows);
    }

二、IServce

        MyBatis-Plus中有一個接口 IService和其實現類 ServiceImpl,封裝了常見的業(yè)務層邏輯。采用get查詢單行、remove刪除、list查詢集合、page分頁前綴命名方式,區(qū)分mapper層,避免混淆。接口如下

public interface IService<T> {
    int DEFAULT_BATCH_SIZE = 1000;
    default boolean save(T entity) {
        return SqlHelper.retBool(this.getBaseMapper().insert(entity));
    }
    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean saveBatch(Collection<T> entityList) {
        return this.saveBatch(entityList, 1000);
    }
    boolean saveBatch(Collection<T> entityList, int batchSize);
    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean saveOrUpdateBatch(Collection<T> entityList) {
        return this.saveOrUpdateBatch(entityList, 1000);
    }
    boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize);
    default boolean removeById(Serializable id) {
        return SqlHelper.retBool(this.getBaseMapper().deleteById(id));
    }
    default boolean removeById(Serializable id, boolean useFill) {
        throw new UnsupportedOperationException("不支持的方法!");
    }
    default boolean removeById(T entity) {
        return SqlHelper.retBool(this.getBaseMapper().deleteById(entity));
    }
    default boolean removeByMap(Map<String, Object> columnMap) {
        Assert.notEmpty(columnMap, "error: columnMap must not be empty", new Object[0]);
        return SqlHelper.retBool(this.getBaseMapper().deleteByMap(columnMap));
    }
    default boolean remove(Wrapper<T> queryWrapper) {
        return SqlHelper.retBool(this.getBaseMapper().delete(queryWrapper));
    }
    default boolean removeByIds(Collection<?> list) {
        return CollectionUtils.isEmpty(list) ? false : SqlHelper.retBool(this.getBaseMapper().deleteBatchIds(list));
    }
    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean removeByIds(Collection<?> list, boolean useFill) {
        if (CollectionUtils.isEmpty(list)) {
            return false;
        } else {
            return useFill ? this.removeBatchByIds(list, true) : SqlHelper.retBool(this.getBaseMapper().deleteBatchIds(list));
        }
    }
    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean removeBatchByIds(Collection<?> list) {
        return this.removeBatchByIds(list, 1000);
    }
    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean removeBatchByIds(Collection<?> list, boolean useFill) {
        return this.removeBatchByIds(list, 1000, useFill);
    }
    default boolean removeBatchByIds(Collection<?> list, int batchSize) {
        throw new UnsupportedOperationException("不支持的方法!");
    }
    default boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFill) {
        throw new UnsupportedOperationException("不支持的方法!");
    }
    default boolean updateById(T entity) {
        return SqlHelper.retBool(this.getBaseMapper().updateById(entity));
    }
    default boolean update(Wrapper<T> updateWrapper) {
        return this.update((Object)null, updateWrapper);
    }
    default boolean update(T entity, Wrapper<T> updateWrapper) {
        return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
    }
    @Transactional(
        rollbackFor = {Exception.class}
    )
    default boolean updateBatchById(Collection<T> entityList) {
        return this.updateBatchById(entityList, 1000);
    }
    boolean updateBatchById(Collection<T> entityList, int batchSize);
    boolean saveOrUpdate(T entity);
    default T getById(Serializable id) {
        return this.getBaseMapper().selectById(id);
    }
    default List<T> listByIds(Collection<? extends Serializable> idList) {
        return this.getBaseMapper().selectBatchIds(idList);
    }
    default List<T> listByMap(Map<String, Object> columnMap) {
        return this.getBaseMapper().selectByMap(columnMap);
    }
    default T getOne(Wrapper<T> queryWrapper) {
        return this.getOne(queryWrapper, true);
    }
    T getOne(Wrapper<T> queryWrapper, boolean throwEx);
    Map<String, Object> getMap(Wrapper<T> queryWrapper);
    <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper);
    default long count() {
        return this.count(Wrappers.emptyWrapper());
    }
    default long count(Wrapper<T> queryWrapper) {
        return SqlHelper.retCount(this.getBaseMapper().selectCount(queryWrapper));
    }
    default List<T> list(Wrapper<T> queryWrapper) {
        return this.getBaseMapper().selectList(queryWrapper);
    }
    default List<T> list() {
        return this.list(Wrappers.emptyWrapper());
    }
    default <E extends IPage<T>> E page(E page, Wrapper<T> queryWrapper) {
        return this.getBaseMapper().selectPage(page, queryWrapper);
    }
    default <E extends IPage<T>> E page(E page) {
        return this.page(page, Wrappers.emptyWrapper());
    }
    default List<Map<String, Object>> listMaps(Wrapper<T> queryWrapper) {
        return this.getBaseMapper().selectMaps(queryWrapper);
    }
    default List<Map<String, Object>> listMaps() {
        return this.listMaps(Wrappers.emptyWrapper());
    }
    default List<Object> listObjs() {
        return this.listObjs(Function.identity());
    }
    default <V> List<V> listObjs(Function<? super Object, V> mapper) {
        return this.listObjs(Wrappers.emptyWrapper(), mapper);
    }
    default List<Object> listObjs(Wrapper<T> queryWrapper) {
        return this.listObjs(queryWrapper, Function.identity());
    }
    default <V> List<V> listObjs(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
        return (List)this.getBaseMapper().selectObjs(queryWrapper).stream().filter(Objects::nonNull).map(mapper).collect(Collectors.toList());
    }
    default <E extends IPage<Map<String, Object>>> E pageMaps(E page, Wrapper<T> queryWrapper) {
        return this.getBaseMapper().selectMapsPage(page, queryWrapper);
    }
    default <E extends IPage<Map<String, Object>>> E pageMaps(E page) {
        return this.pageMaps(page, Wrappers.emptyWrapper());
    }
    BaseMapper<T> getBaseMapper();
    Class<T> getEntityClass();
    default QueryChainWrapper<T> query() {
        return ChainWrappers.queryChain(this.getBaseMapper());
    }
    default LambdaQueryChainWrapper<T> lambdaQuery() {
        return ChainWrappers.lambdaQueryChain(this.getBaseMapper());
    }
    default KtQueryChainWrapper<T> ktQuery() {
        return ChainWrappers.ktQueryChain(this.getBaseMapper(), this.getEntityClass());
    }
    default KtUpdateChainWrapper<T> ktUpdate() {
        return ChainWrappers.ktUpdateChain(this.getBaseMapper(), this.getEntityClass());
    }
    default UpdateChainWrapper<T> update() {
        return ChainWrappers.updateChain(this.getBaseMapper());
    }
    default LambdaUpdateChainWrapper<T> lambdaUpdate() {
        return ChainWrappers.lambdaUpdateChain(this.getBaseMapper());
    }
    default boolean saveOrUpdate(T entity, Wrapper<T> updateWrapper) {
        return this.update(entity, updateWrapper) || this.saveOrUpdate(entity);
    }
}

1. 創(chuàng)建Service接口和實現類

/**
* UserService繼承IService模板提供的基礎功能
*/
public interface UserService extends IService<User> {}
/**
* ServiceImpl實現了IService,提供了IService中基礎功能的實現
* 若ServiceImpl無法滿足業(yè)務需求,則可以使用自定的UserService定義方法,并在實現類中實現 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {}

2. 測試查詢記錄數

    @Autowired
    private UserService userService;
    @Test
    public void testGetCount(){
        long count = userService.count();
        System.out.println("總記錄數:" + count);
    }

3. 測試批量插入

    /**
     * 批量插入
     */
    @Test
    public void testSaveBatch(){
        // SQL長度有限制,海量數據插入單條SQL無法實行,
        // 因此MP將批量插入放在了通用Service中實現,而不是通用Mapper
        ArrayList<User> users = new ArrayList<>();
        // 未傳參的字段默認為null,但是id雪花自增
        for (int i = 0; i < 5; i++) {
            User user = new User();
            // 字符串拼接
            user.setName("kk" + i);
            // 加法運算
            user.setAge(20 + i);
            users.add(user);
        }
        //SQL:INSERT INTO user ( username, age ) VALUES ( ?, ? )
        userService.saveBatch(users);
    }

4. 其他常用接口

    // 測試saveOrUpdate(新增或更新:有ID則更新,無則新增)
    @Test
    public void testSaveOrUpdate() {
        // 場景1:無ID → 新增
        User user1 = new User();
        user1.setName("新增或更新1");
        user1.setAge(23);
        user1.setEmail("saveOrUpdate1@test.com");
        userService.saveOrUpdate(user1);
        // 場景2:有ID → 更新
        User user2 = new User();
        user2.setId(1L);
        user2.setName("新增或更新2");
        user2.setAge(24);
        userService.saveOrUpdate(user2);
    }
    // 測試saveOrUpdateBatch(批量新增或更新)
    @Test
    public void testSaveOrUpdateBatch() {
        User user1 = new User();
        user1.setId(1L); // 有ID → 更新
        user1.setName("批量更新");
        user1.setAge(25);
        User user2 = new User(); // 無ID → 新增
        user2.setName("批量新增");
        user2.setAge(26);
        List<User> userList = Arrays.asList(user1, user2);
        boolean result = userService.saveOrUpdateBatch(userList);
        System.out.println("批量新增/更新是否成功:" + result);
    }
    // 測試updateBatchById(批量更新(按ID))
    @Test
    public void testUpdateBatchById() {
        User user1 = new User();
        user1.setId(1L);
        user1.setAge(30);
        User user2 = new User();
        user2.setId(2L);
        user2.setAge(31);
        List<User> userList = Arrays.asList(user1, user2);
        boolean result = userService.updateBatchById(userList);
        System.out.println("批量更新是否成功:" + result);
    }
    // 測試removeById(根據ID刪除)
    @Test
    public void testRemoveById() {
        boolean result = userService.removeById(1L);
        System.out.println("刪除是否成功:" + result);
    }
    // 測試removeByIds(批量刪除)
    @Test
    public void testRemoveByIds() {
        List<Long> idList = Arrays.asList(7L, 8L);
        boolean result = userService.removeByIds(idList);
        System.out.println("批量刪除是否成功:" + result);
    }
    // 測試remove(按條件刪除)
    @Test
    public void testRemove() {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getName, "測試刪除").eq(User::getAge, 0);
        boolean result = userService.remove(queryWrapper);
        System.out.println("條件刪除是否成功:" + result);
    }
    // 測試getById(根據ID查詢)
    @Test
    public void testGetById() {
        User user = userService.getById(1L);
        System.out.println(user);
    }
    // 測試listByIds(批量查詢)
    @Test
    public void testListByIds() {
        List<Long> idList = Arrays.asList(1L, 2L);
        List<User> userList = userService.listByIds(idList);
        userList.forEach(System.out::println);
    }
    // 測試listPage(分頁查詢)
    @Test
    public void testListPage() {
        Page<User> page = new Page<>(1, 5);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(User::getAge, 18);
        IPage<User> userIPage = userService.page(page, queryWrapper);
        System.out.println("總條數:" + userIPage.getTotal());
        userIPage.getRecords().forEach(System.out::println);
    }
    // 測試count(統(tǒng)計總數)
    @Test
    public void testCount() {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.gt(User::getAge, 20);
        long count = userService.count(queryWrapper);
        System.out.println("年齡大于20的用戶數:" + count);
    }
    // 測試鏈式查詢(LambdaQueryChainWrapper)
    @Test
    public void testLambdaQueryChain() {
        List<User> userList = new LambdaQueryChainWrapper<>(userService.getBaseMapper())
                .eq(User::getAge, 20)
                .like(User::getName, "張三")
                .list();
        userList.forEach(System.out::println);
    }
    // 測試事務性批量操作(@Transactional)
    @Test
    @Transactional // 測試完成后回滾,避免污染數據
    public void testTransactionalBatch() {
        // 批量新增
        List<User> saveList = Arrays.asList(
                new User(null, "事務測試1", 28, "tx1@test.com", SexEnum.MALE, null),
                new User(null, "事務測試2", 29, "tx2@test.com", SexEnum.FEMALE,null)
        );
        userService.saveBatch(saveList);
        // 批量更新
        List<User> updateList = Arrays.asList(
                new User(2026L, "事務更新1", 30, null, SexEnum.MALE, null),
                new User(2027L, "事務更新2", 31, null, SexEnum.FEMALE, null)
        );
        userService.updateBatchById(updateList);
        // 驗證(事務內可查,事務外回滾)
        System.out.println("事務內查詢:" + userService.getById(2026L));
    }

到此這篇關于MyBatis-Plus的基本CRUD的文章就介紹到這了,更多相關mybatisplus crud操作內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • mybatis一對多兩種mapper寫法實例

    mybatis一對多兩種mapper寫法實例

    這篇文章主要介紹了mybatis一對多兩種mapper寫法實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • Springboot集成Quartz實現定時任務代碼實例

    Springboot集成Quartz實現定時任務代碼實例

    這篇文章主要介紹了Springboot集成Quartz實現定時任務代碼實例,任務是有可能并發(fā)執(zhí)行的,若Scheduler直接使用Job,就會存在對同一個Job實例并發(fā)訪問的問題,而JobDetail?&?Job方式,Scheduler都會根據JobDetail創(chuàng)建一個新的Job實例,這樣就可以規(guī)避并發(fā)訪問問題
    2023-09-09
  • Java語言class類用法及泛化(詳解)

    Java語言class類用法及泛化(詳解)

    這篇文章主要介紹了Java語言class類用法及泛化(詳解),大家都知道Java程序在運行過程中,對所有的對象今夕類型標識,也就是RTTI。這項信息記錄了每個對象所屬的類,需要的朋友可以參考下
    2015-07-07
  • mybaits中if條件中怎樣判斷布爾值

    mybaits中if條件中怎樣判斷布爾值

    這篇文章主要介紹了mybaits中if條件中怎樣判斷布爾值問題,具有很好的參考價值,希望對大家有所幫助,
    2023-08-08
  • java利用oss實現下載功能

    java利用oss實現下載功能

    這篇文章主要為大家詳細介紹了java利用oss實現下載功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • Spring與Mybatis相結合實現多數據源切換功能

    Spring與Mybatis相結合實現多數據源切換功能

    這篇文章主要介紹了Spring與Mybatis相結合實現多數據源切換功能的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • java后臺本地文件轉為MultipartFile類型的實現方式

    java后臺本地文件轉為MultipartFile類型的實現方式

    在Java后臺將本地文件轉換為MultipartFile類型,可以通過使用FileItemFactory創(chuàng)建FileItem,然后使用CommonsMultipartFile類構造一個MultipartFile對象,將本地文件流轉換為MultipartFile,getMultipartFiles()和getMultipartFiles()方法
    2025-02-02
  • IDEA的默認快捷鍵設置與Eclipse的常用快捷鍵的設置方法

    IDEA的默認快捷鍵設置與Eclipse的常用快捷鍵的設置方法

    這篇文章主要介紹了IDEA的默認快捷鍵設置與Eclipse的常用快捷鍵的設置方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • maven環(huán)境變量配置講解

    maven環(huán)境變量配置講解

    這篇文章主要介紹了maven環(huán)境變量配置講解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-08-08
  • SpringBoot服務訪問路徑動態(tài)處理方式

    SpringBoot服務訪問路徑動態(tài)處理方式

    這篇文章主要介紹了SpringBoot服務訪問路徑動態(tài)處理方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12

最新評論

封开县| 乌什县| 沁源县| 科技| 定日县| 彝良县| 石家庄市| 吉木乃县| 定结县| 图木舒克市| 丰原市| 弥渡县| 洛宁县| 兴城市| 大埔区| 成安县| 富源县| 开原市| 平南县| 保德县| 宁河县| 昌江| 蕲春县| 兴义市| 新绛县| 渑池县| 申扎县| 喀什市| 仁化县| 五家渠市| 博爱县| 色达县| 大兴区| 文安县| 榆社县| 安龙县| 清徐县| 盐亭县| 噶尔县| 汪清县| 乌拉特后旗|