4. service层
MyBatisPlus封装了service层,只需要继承IService接口即可。并且MyBatisPlus实现了提供了接口的实现类。
package pers.beiluo.yunshangoffice.service;
import com.baomidou.mybatisplus.extension.service.IService;
import pers.beiluo.yunshangoffice.model.system.SysRole;
public interface SysRoleService extends IService<SysRole> {
}
package pers.beiluo.yunshangoffice.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import pers.beiluo.yunshangoffice.mapper.SysRoleMapper;
import pers.beiluo.yunshangoffice.model.system.SysRole;
import pers.beiluo.yunshangoffice.service.SysRoleService;
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService{
//在ServiceImpl中已经完成了mapper的注入
// @Autowired
// protected M baseMapper;
}
//测试类
package pers.beiluo.yunshangoffice;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import pers.beiluo.yunshangoffice.model.system.SysRole;
import pers.beiluo.yunshangoffice.service.SysRoleService;
import java.util.List;
@SpringBootTest
public class SysRoleServiceTest {
@Autowired
private SysRoleService sysRoleService;
}
4.1 查询测试
//这里要注意,@MapperScan注解扫描位置要精确到mapper所在的包,不能写上一层或更上层的包,否则会报错
@Test
public void getAllTest(){
List<SysRole> list = sysRoleService.list();
list.forEach(System.out::println);
}
4.2 添加测试
@Test
public void insertTest(){
SysRole sysRole = new SysRole();
sysRole.setRoleName("角色管理员2");
sysRole.setRoleCode("role");
sysRole.setDescription("角色管理员2");
boolean save = sysRoleService.save(sysRole);
System.out.println(save);
System.out.println(sysRole);
}
4.3 修改测试
@Test
public void updateTest(){
SysRole sysRole = new SysRole();
sysRole.setId(10L);
sysRole.setRoleCode("roleee");
boolean b = sysRoleService.updateById(sysRole);
System.out.println(b);
}
4.4 删除测试
//根据id删除
@Test
public void deleteByIdTest(){
boolean b = sysRoleService.removeById(10);
System.out.println(b);
}
//批量删除
@Test
public void deleteBatchTest(){
boolean b = sysRoleService.removeByIds(Arrays.asList(2, 9));
System.out.println(b);
}
4.5 条件查询
@Test
public void selectTest(){
// QueryWrapper<SysRole> sysRoleQueryWrapper = new QueryWrapper<>();
// sysRoleQueryWrapper.eq("role_code", "roleee");
LambdaQueryWrapper<SysRole> sysRoleQueryWrapper = new LambdaQueryWrapper<>();
sysRoleQueryWrapper.eq(SysRole::getRoleCode,"roleee");
List<SysRole> list = sysRoleService.list(sysRoleQueryWrapper);
list.forEach(System.out::println);
}