MP中有一个接口 IService和其实现类 ServiceImpl,封装了常见的业务层逻辑
创建 service 包,创建 UserService,继承 IService
package com.atguigu.mybatisplus.service; public interface UserService extends IService<User> { }
创建 impl 包,创建 UserServiceImpl,继承 ServiceImpl,实现 UserService
package com.atguigu.mybatisplus.service.impl; @Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService { }
创建ServiceTests
package com.atguigu.mybatisplus; @SpringBootTest public class ServiceTests { @Resource private UserService userService; }
@Test public void testCount(){ int count = userService.count(); System.out.println("总记录数:" + count); }
@Test public void testSaveBatch(){ // SQL长度有限制,海量数据插入单条SQL无法实行, // 因此MP将批量插入放在了通用Service中实现,而不是通用Mapper ArrayList<User> users = new ArrayList<>(); for (int i = 0; i < 5; i++) { User user = new User(); user.setName("Helen" + i); user.setAge(10 + i); users.add(user); } userService.saveBatch(users); }