与BaseMapper相同,MyBatis-Plus同样也为SpringBoot的Service层提供了一些基础的方法,使用前我们在Service层继承IService
import com.baomidou.mybatisplus.extension.service.IService;public interface StudentService extends IService<Student>//其中<>内的为对应的实体类{}
然后再创建对应的ServiceImpl,继承MyBatis-Plus提供的ServiceImpl
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
}
然后就可以在测试内使用一些基础的方法了。首先创建测试项目,其基本内容形式如下
@SpringBootTest
public class Test2 {@Autowiredprivate StudentService studentService;//其他测试方法}
第一部分是查询内容,首先是查询数据库内有多少条数据:
@Testpublic void GetCount() {// 查询数据库中的总记录数long count = studentService.count();System.out.println("总记录数:" + count);}
可以看到其自动的实现了在Mysql内查询数据的数量,不需要再手动的写SQL语句,其运行结果如下:
然后是查询用户的数据,例如:
@Testpublic void TestToGetUserById() {// 根据ID查询用户记录Student s = studentService.getById(1L);//调用查询id的方法assertNotNull(s);//检查是否为null,为null则抛出异常,非必要方法,在没有需求的情况下可以删除System.out.println("查询结果:" + s);}
其运行结果和自动实现的SQl语句如下:
其中的is_deleted为逻辑删除的标志,即删除时在数据库内只修改改属性以实现可恢复的删除,需要手动配置,默认下是没有这个属性。
然后是根据多个用户id,批量的查询用户的信息:
@Testpublic void testListByIds() {// 根据ID列表批量查询用户记录List<Long> ids =Arrays.asList(1L, 2L);//要查询的用户id列表List<Student> ss = studentService.listByIds(ids);System.out.println("用户列表:" + ss);}
其运行结果和自动实现的SQl语句如下:
而根据其他条件如姓名啊,性别啊,有多种方式可以实现,这里学习几个常用的,首先是使用Map实现根据条件查询:
@Testpublic void testListByMap() {// 根据条件查询用户记录Map<String, Object> Map= new HashMap<>();Map.put("age", "19");//前者为条件的属性名,后后者条件的具体值Map.put("sex", "女");List<Student> ss = studentService.listByMap(Map);System.out.println("用户列表:" + ss);}
运行结果及SQL语句如下:
然后是基于条件构造器Wrapper的查找,首先根据条件查找单一目标,且符合条件的目标必须只能有一个,否则会报错:
@Testpublic void testGetOne() {// 根据条件查询单个用户记录,需要保证查询条件唯一QueryWrapper<Student> queryWrapper = new QueryWrapper<>();queryWrapper.eq("name", "镜野");//前者为条件的属性名,后后者条件的具体值Student s = studentService.getOne(queryWrapper);System.out.println("查询结果:" + s);}
运行结果及SQl语句如下:
而如果我们查找出来的有多个符合条件的结果,如将条件名改为age,值改为19,则:
而其可以改变写法,实现只查找一个,如果查找的结果有多个符合条件的,取第一个:
@Testpublic void testGetOnePro() {// 根据条件查询单个用户记录,如出现多个符合条件的,则选取查询到的第一个,且不抛出异常QueryWrapper<Student> queryWrapper = new QueryWrapper<>();queryWrapper.eq("age", "19");Student s = studentService.getOne(queryWrapper, false);System.out.println("查询结果:" + s);}
运行结果如下:
初次之外,也可以使用Map接收上述的查询的结果:
@Testpublic void GetOneForMap() {// 根据条件查询单个用户记录并返回Map,如出现多个符合条件的,则选取查询到的第一个,且不抛出异常QueryWrapper<Student> queryWrapper = new QueryWrapper<>();queryWrapper.eq("age", "19");Map<String, Object> map = studentService.getMap(queryWrapper);System.out.println("查询结果:" + map);}
然后是查询所有的数据:
@Testpublic void testList() {// 查询所有用户记录List<Student> ss = studentService.list();System.out.println("用户列表:" + ss);}
结果如下:
而出了list集合的写法外,我们也可以使用Map来写,代码如下:
@Testpublic void ListMaps() {// 查询所有用户记录并返回Map列表List<Map<String, Object>> maps = studentService.listMaps();System.out.println("用户列表:" + maps);}
运行结果如下:
同时,IService也提供了分页的功能:
@Testpublic void testPage() {// 分页查询用户记录IPage<Student> page = new Page<>(1, 5);//前者为当前的页数,后者为一页放多少行数据IPage<Student> studentPage = studentService.page(page);System.out.println("分页查询结果:" + studentPage.getRecords());}
运行结果如下:
而同样的,我们也可以使用Map来接收数据:
@Testpublic void testPageMaps() {// 分页查询用户记录并返回Map列表IPage<Map<String, Object>> page = new Page<>(2, 5);//前者为当前的页数,后者为一页放多少行数据IPage<Map<String, Object>> mapPage = studentService.pageMaps(page);System.out.println("分页查询结果:" + mapPage.getRecords());}
结果如下
同时,我们可以将分页和查询结合,实现有条件的查询的同时对数据进行分页,如基于Wrapper的版本:
@Testpublic void testPageMapsByAge() {IPage<Map<String, Object>> page = new Page<>(2, 2);QueryWrapper<Student> queryWrapper = new QueryWrapper<>();// 创建QueryWrapper并添加条件queryWrapper.eq("age", 19); // 等于19的条件IPage<Map<String, Object>> mapPage = studentService.pageMaps(page, queryWrapper);System.out.println("分页查询结果:" + mapPage.getRecords());}
运行结果如下:
而使用实体类的版本如下:
@Testpublic void testPagByIdAndSex() {IPage<Student> page = new Page<>(1, 2);//前者为当前的页数,后者为一页放多少行数据QueryWrapper<Student> queryWrapper = new QueryWrapper<>();// 创建QueryWrapper并添加条件queryWrapper.eq("age", 19);queryWrapper.eq("sex", "女");IPage<Student> studentPage = studentService.page(page,queryWrapper);System.out.println("分页查询结果:" + studentPage.getRecords());}
运行结果如下: