mybatis自身附带的查询有很多,也许在工作中用不到,但是有一定的学习必要,可以理解其查询设计的思路。
部分查询测试,关于lambda部分测试目前难以理解,等熟练lambda在回头学习
在SpringBoot中仅仅需要完成如下,即可进行测试
在dao层
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xiong.java.pojo.User;
public interface UserMapper extends BaseMapper<User> {
}
package com.xiong.java;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiong.java.mapper.UserMapper;
import com.xiong.java.pojo.User;
import com.xiong.java.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.Alias;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 测试 mybatis plus自身定义的查询
*/
@Slf4j
@SpringBootTest
public class UserTest {
@Autowired
UserMapper mapper;
/**
* 普通查询
*/
//最基本的通过id查询。
@Test
void testSelect(){
User user = mapper.selectById(1);
System.out.println(user);
}
@Test
void testSelectWrapper01(){
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("name","Jone");
//通过Wrapper查询单个实体
User user = mapper.selectOne(userQueryWrapper);
System.out.println(user);
Integer integer = mapper.selectCount(userQueryWrapper);
System.out.println(integer);
Map<String,Object> map=new HashMap<>();
map.put("name","Jack");
map.put("age",20);
//通过集合查询多个实体
List<User> users = mapper.selectByMap(map);
System.out.println(users);
}
@Test
void testSelectWrapper02(){
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
//name!=Jack的实体
userQueryWrapper.ne("name","Jack");
userQueryWrapper.gt("age",23);
//查询多个实体
List<User> users = mapper.selectList(userQueryWrapper);
for (User user : users) {
System.out.println(user);
}
}
/**
* 通过 Map集合查询,返回值也是 Map集合
* 返回值为 Maps
*/
@Test
void testSelectWrapper03(){
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.ne("name","Jack");
List<Map<String, Object>> list = mapper.selectMaps(userQueryWrapper);
for (Map<String, Object> stringObjectMap : list) {
System.out.println(stringObjectMap);
}
}
/**
*需要导入bean
*
* package com.xiong.java.config;
*
* import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
* import org.springframework.context.annotation.Bean;
* import org.springframework.context.annotation.Configuration;
*
* //分页插件
* @Configuration
* public class MybatisPlusConfig{
* @Bean
* public PaginationInterceptor paginationInterceptor() {
* return new PaginationInterceptor();
* }
* }
*
*测试分页
*Page<User> userPage = mapper.selectPage(page, userQueryWrapper);
*/
@Test
void testSelectWrapperPage01(){
Page<User> page=new Page<>(1,3);
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
Page<User> userPage = mapper.selectPage(page, userQueryWrapper);
System.out.println(userPage);//com.baomidou.mybatisplus.extension.plugins.pagination.Page@286855e
//总共页数
System.out.println("getPages="+page.getPages());
//每页大小
System.out.println("getSize="+page.getSize());
//总共记录条数
System.out.println("getTotal="+page.getTotal());
System.out.println("getCountId="+page.getCountId());
//当前页码
System.out.println("getCurrent="+page.getCurrent());
System.out.println("getMaxLimit="+page.getMaxLimit());
System.out.println("getOrders="+page.getOrders());
System.out.println("records-------------");
//获取当前页的所有记录
List<User> records = userPage.getRecords();
for (User user : records) {
System.out.println(user);
}
System.out.println("orders-------------");
List<OrderItem> orders = userPage.getOrders();
for (OrderItem order : orders) {
System.out.println(order);
}
String s = userPage.toString();
System.out.println(s);
}
/**
*测试 mapper.selectMapsPage()
*不太行
*/
@Test
void testSelectWrapperPage02(){
Page<User> page=new Page<>(2,3);
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.ne("name","xiong");
// E selectMapsPage = mapper.selectMapsPage(page, userQueryWrapper);
}
/**
*测试 lambda
*
*
*/
@Test
void testSelectLambda01(){
LambdaQueryWrapper<User> userLambdaQueryWrapper = Wrappers.<User>lambdaQuery();
userLambdaQueryWrapper.likeRight(User::getName,"Jack")
.and(age->age.lt(User::getAge,20)).or().isNotNull(User::getEmail);
List<User> users = mapper.selectList(userLambdaQueryWrapper);
users.forEach(System.out::println);
}
/**
* int update(@Param("et") T entity, @Param("ew") Wrapper<T> updateWrapper);
* 只修改设置的属性,其他属性保持不变
* 返回值是修改的条数
*/
@Test
void testWrapperUpdate01(){
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper.eq("id","1");
User user=new User();
user.setName("John");
int update = mapper.update(user, userUpdateWrapper);
System.out.println(update);
}
/**
*大概就是以一个user中的值控制SQL语句的前面
* 以WhereUser的值控制where后边
*/
@Test
void testWrapperUpdate02(){
//控制SQL语句where后边
User WhereUser = new User();
WhereUser.setId(2L);
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>(WhereUser);
//控制SQL语句where前边
User user=new User();
user.setName("Ben");
//无用语句
user.setId(3l);
int update = mapper.update(user, userUpdateWrapper);
System.out.println("update = " + update);
}
/**
* 增加 目前就一种
*/
/**
* mapper 自带的insert
* 无需使用lambda wrapper
*/
@Test
void testInsert01(){
User user=new User();
user.setName("Jack Ma");
user.setAge(23);
user.setEmail("1822649111@qq.com");
int insert = mapper.insert(user);
System.out.println("insert = " + insert);
}
/**
* 删除 目前就四种
*/
/**
*直接删除
*/
@Test
void testDelete(){
int delete = mapper.deleteById(1);
System.out.println("delete = " + delete);
}
/**
*通过QueryWrapper删除
* 可以实现单个删除和批量删除
*
* 如:若需要删除id从1到10的所有实体
* 该方法就不行了,当然你可以通过循环解决,不过太low
*/
@Test
void testWrapperDelete01(){
QueryWrapper queryWrapper = new QueryWrapper();
//单个删除
// queryWrapper.eq("id",1);
// queryWrapper.eq("id",2);
// queryWrapper.eq("id",3);
//批量删除 删除name=Jack的所有实体
queryWrapper.eq("name","Jack");
int delete = mapper.delete(queryWrapper);
System.out.println("delete = " + delete);
}
/**
* 批量删除 通过list集合
*/
@Test
void testCollectionDelete01(){
List<Integer> list=new ArrayList<>();
list.add(2);
list.add(3);
int delete = mapper.deleteBatchIds(list);
System.out.println("delete = " + delete);
}
/**
* 删除 通过Map集合
*/
@Test
void testCollectionDelete02(){
Map<String,Object> map=new HashMap<>();
map.put("age",23);
int delete = mapper.deleteByMap(map);
System.out.println("delete = " + delete);
}
}