1.提前定义好的插入功能:
BaseMapper里面封装了已经定义好的insert语句,能够方便我们直接调用,无须自己手动编写sql
@GetMapping(value = "/insert") public void insert(){ Teacher teacher=new Teacher(); teacher.setTeacherName(createRandomStr(6)); teacher.setTeacherPwd(createRandomStr(6)); teacherMapper.insert(teacher); } /** * 生成随机字符串 * * @return */ private static String createRandomStr(int length){ String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; Random random=new Random(); StringBuffer sb=new StringBuffer(); for(int i=0;i<length;i++){ int number=random.nextInt(str.length()); sb.append(str.charAt(number)); } return sb.toString(); }
生成的sql语句:
INSERT INTO teacher ( id, teacher_name, teacher_pwd ) VALUES ( 0, 'mNJXIf', 'LKTnam' );
2.提前定义好的删除功能
BaseMapper里面提供有已经定义好的delete功能,
@GetMapping(value = "/delete") public void delete(){ Teacher teacher=new Teacher(); teacher.setId(11); EntityWrapper entityWrapper=new EntityWrapper(teacher); teacherMapper.delete(entityWrapper); }
生成的sql语句:
DELETE FROM teacher WHERE id=11;
3.提前定义好的更新功能
BaseMapper里面定义了相关的update功能,但是在进行update的时候有个地方需要注意,来看看下边这段代码先
@GetMapping(value = "/update") public void update(){ //update的判断条件 EntityWrapper entityWrapper=new EntityWrapper(new Teacher(1)); //更新之后的对象 Teacher teacher=new Teacher(); teacher.setTeacherPwd("new-pwd"); teacherMapper.update(teacher,entityWrapper); }
生成的sql语句:
UPDATE teacher SET teacher_pwd='new-pwd' WHERE id=1;
mybatis-plus里面提供了一个叫做EntityWrapper的对象封装操作类,通过传入相关的Entity来判断传入的查询参数,这里的使用让我回想起了以前用hibernate的那种味道,不得不说这种功能的设计确实帮开发者的开发效率提升了很多。
以前写更新语句的时候,经常是需要在xml里面进行非常多繁琐耗时的sql拼接工作,现在mybatis-plus通过EntityWrapper这个封装类,大大节省了相应时间。
4.根据指定id进行查询
mybatis-plus也默认提供了关键字查询的功能,可以将查询参数通过对象Entity的形式注入,非常方便。
ps:mybatis-plus已经默认带有了sql注入的预防功能,因此这点可以放心使用。
@GetMapping(value = "/selectAllById") public Teacher selectByTeacherName(int id){ return teacherMapper.selectOne(new Teacher(id)); }
生成的sql语句:
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd FROM teacher WHERE id=0;
5.使用Map来进行多关键字的查询
有些时候,除了使用查询时候使用Entity关键字来进行查询之外,还可以通过使用Map的形式来进行多关键字的搜索实现,相关代码如下所示:
@GetMapping(value = "/selectAllByMap") public List<Teacher> selectAllByEntity(String name){ Map<String,Object> hashMap=new HashMap<>(); hashMap.put("teacher_name",name); return teacherMapper.selectByMap(hashMap); }
注意,这里的map放入的key值需要和表里面的字段命名一致。
生成的sql语句:
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd FROM teacher WHERE teacher_name = 'qwe';
6.统计查询
@GetMapping(value = "/selectCountByEntity") public int selectCount(String name){ Teacher teacher=new Teacher(); teacher.setId(1); teacher.setTeacherName(name); EntityWrapper<Teacher> entityWrapper=new EntityWrapper<>(teacher); return teacherMapper.selectCount(entityWrapper); }
生成的sql语句:
SELECT COUNT(1) FROM teacher WHERE id=1 AND teacher_name='qwe';
7.分页查询
在实际开发中,分页查询功能一直是非常普遍需要运用到的一点,在mybatis-plus里面,他提供了一个叫做Page的类供我们使用,相应的代码如下所示:
@GetMapping(value = "/selectAllInPage") public List<Teacher> selectAllInPage(int pageNumber,int pageSize){ Page<Teacher> page =new Page<>(pageNumber,pageSize); EntityWrapper<Teacher> entityWrapper = new EntityWrapper<>(); entityWrapper.ge("id", 1); return teacherMapper.selectPage(page,entityWrapper); }
生成的sql语句:
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd FROM teacher WHERE (id >= 1) LIMIT 0,1;
mybatis-plus使用的分页技术是传统的 limit 物理分页方式。
8. in 查询
查询的时候,如果需要使用in查询的话,可以使用selectBatchIds这个api功能:
@GetMapping(value = "/selectInIdArr") public List<Teacher> selectInIdArr(){ List<Integer> idList=new ArrayList<>(); idList.add(1); idList.add(10); idList.add(11); return teacherMapper.selectBatchIds(idList); }
生成的sql语句:
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd FROM teacher WHERE id IN ( 1 , 10 , 11 );
9.复杂条件查询
针对于比较复杂的多条件查询,mybatis-plus内部提供的EntityWrapper支持有许多丰富的查询api功能供我们使用。
为了方便理解,下边通过实际的代码案例来进行解释
allEq查询(相当于条件全部都要求满足的情况)
@GetMapping(value = "/selectAllByWrapper1") public List<Teacher> selectAllByWrapper1(){ Map<String,Object> map=new HashMap<>(); map.put("teacher_name","name"); map.put("teacher_pwd","pwd"); EntityWrapper entity=new EntityWrapper(); entity.allEq(map); return teacherMapper.selectList(entity); }
生成的sql语句:
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd FROM teacher WHERE (teacher_pwd = 'pwd' AND teacher_name = 'name');
ne查询
@GetMapping(value = "/selectAllByWrapper3") public List<Teacher> selectAllByWrapper3(){ EntityWrapper entity=new EntityWrapper(); entity.ne("teacher_name","name"); return teacherMapper.selectList(entity); }
生成的sql语句:
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd FROM teacher WHERE (teacher_name <> 'name');
eq查询
@GetMapping(value = "/selectAllByWrapper2") public List<Teacher> selectAllByWrapper2(){ EntityWrapper entity=new EntityWrapper(); entity.eq("teacher_name","name"); return teacherMapper.selectList(entity); }
生成的sql语句:
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd FROM teacher WHERE (teacher_name = 'name');
复杂的多条件查询:
@GetMapping(value = "/selectAllByWrapper4") public List<Teacher> selectAllByWrapper4(){ EntityWrapper entity=new EntityWrapper(); entity.gt("id","0"); entity.le("id",11); entity.ne("teacher_name","null_name"); entity.like("teacher_name","tt"); entity.notLike("teacher_pwd","sadas"); entity.orderBy("id"); return teacherMapper.selectList(entity); }
生成的sql语句:
组合的and和or条件查询:
生成的sql语句:
SELECT id,teacher_name AS teacherName,teacher_pwd AS teacherPwd FROM teacher WHERE (id>1) OR (id=0 AND teacher_name='name' AND teacher_pwd IS NULL);
having和groupby查询: