MyBatis-Plus_通用service

通用service

文章目录

1. 接口类继承 IService<T>

public interface UserService extends IService<User> {
}

2. 接口实现类中继承ServiceImpl<M,T>

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

3. 测试

@Component
public class UserServiceTest extends MybatisPlusSamplesChapter1ApplicationTests {

    @Resource
    private UserService userService;

    @Test
    public void getOne(){
        User one = userService.getOne(Wrappers.<User>lambdaQuery().gt(User::getAge, 25),false);
        System.out.println(one);

    }

    /**
     * 当调用saveBatch方法会执行插入操作
     * 当调用saveOrUpdateBatch方法会不存在执行插入存在执行更新操作
     */
    @Test
    public void bath(){
        User user1 = new User();
        user1.setName("yuxin");
        user1.setAge(2);

        User user2 = new User();
        user2.setId(11L);
        user2.setName("ly");
        user2.setAge(25);

        List<User> userList = Arrays.asList(user1, user2);
//        boolean saveBatch = userService.saveBatch(userList);
        boolean saveBatch = userService.saveOrUpdateBatch(userList);
        System.out.println(saveBatch);
    }

    @Test
    public void chain(){
        List<User> userList = userService.lambdaQuery().gt(User::getAge, 25).like(User::getName, "雨").list();
        userList.forEach(System.out::println);
    }

    @Test
    public void chain2(){
        boolean update = userService.lambdaUpdate().eq(User::getAge, 25).set(User::getAge, 30).update();
        System.out.println(update);
    }

    @Test
    public void chain3(){
        boolean remove = userService.lambdaUpdate().eq(User::getAge, 25).remove();
        System.out.println(remove);
    }
}

想学习更多微服务、分布式、中间件、数据库、项目快速构建等系列技术
请访问http://gblfy.com
让我们一起进步!!!

上一篇:cglib 代理实现


下一篇:springboot测试类注解