增加和更新以save为前缀

 

 

 

 

 

 UserService接口:

public interface UserService extends IService<User> {
}

UserServiceImpl:

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

 

其中User为实体类


 

 

package com.zhy.mybatisplus_project;

import com.zhy.service.UserService;
import com.zhy.mapper.UserMapper;
import com.zhy.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@SpringBootTest
public class MyBatisPlusServiceTest {

    @Autowired
    UserService userService;

    //批量添加
    @Test
    public void test1(){
        List<User> list = new ArrayList<>();
        for(int i=0;i<10;i++){
            User user = new User();
            user.setAge(20+i);
            user.setName("z"+i);
            list.add(user);
        }
        boolean b = userService.saveBatch(list);
        System.out.println(b);
    }

    //查询总记录数
    @Test
    public void test2(){
        long count = userService.count();
        System.out.println("总记录数:"+count);
    }
}

 

 

 

posted on 2022-05-23 18:26  西凉#  阅读(220)  评论(0编辑  收藏  举报