You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
259 lines
8.5 KiB
259 lines
8.5 KiB
package cc.bnblogs.mybatis_study; |
|
|
|
import cc.bnblogs.mybatis_study.mapper.UserMapper; |
|
import cc.bnblogs.mybatis_study.pojo.User; |
|
import cc.bnblogs.mybatis_study.service.UserService; |
|
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.IPage; |
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
|
import org.junit.jupiter.api.Test; |
|
import org.mybatis.spring.annotation.MapperScan; |
|
import org.springframework.beans.factory.annotation.Autowired; |
|
import org.springframework.boot.test.context.SpringBootTest; |
|
|
|
import javax.annotation.Resource; |
|
import java.util.ArrayList; |
|
import java.util.HashMap; |
|
import java.util.List; |
|
import java.util.Map; |
|
|
|
@SpringBootTest |
|
class MybatisStudyApplicationTests { |
|
|
|
@Autowired |
|
private UserMapper userMapper; |
|
@Autowired |
|
private UserService service; |
|
|
|
@Test |
|
public void testSelect() { |
|
List<Long> list = new ArrayList<>(); |
|
list.add(1L); |
|
list.add(3L); |
|
list.add(5L); |
|
userMapper.deleteBatchIds(list); |
|
// 查询表中所有数据 |
|
List<User> userList = userMapper.selectList(null); |
|
userList.forEach(System.out::println); |
|
} |
|
|
|
@Test |
|
public void insertTest() { |
|
User u = new User(1, "aaa", 19, "111@qq.com"); |
|
userMapper.updateById(u); |
|
} |
|
|
|
@Test |
|
void simpleSelect() { |
|
System.out.println("service.count() = " + service.count()); |
|
System.out.println("service.list() = " + service.list()); |
|
System.out.println("service.listMaps() = " + service.listMaps()); |
|
System.out.println("service.listObjs() = " + service.listObjs()); |
|
System.out.println("service.getById(1580903310957416450L) = " + service.getById(1580903310957416450L)); |
|
} |
|
|
|
@Test |
|
void insertData() { |
|
// 存入一个user |
|
User user = new User(null, "zzz", 18, "aa"); |
|
// service.save(user); |
|
// 批量存入 |
|
List<User> list = new ArrayList<>(); |
|
list.add(user); |
|
list.add(user); |
|
list.add(user); |
|
service.saveBatch(list); |
|
// 查看所有数据 |
|
service.list().forEach(System.out::println); |
|
} |
|
|
|
@Test |
|
void insertUpdate() { |
|
System.out.println("service.list() = " + service.list()); |
|
User user = new User(null, "9999", 20, "xxx@qq.com"); |
|
// 根据主键进行插入或者更新某条记录,如果数据库中不存在这个主键,那么将会进行插入; |
|
// 如果存在这个主键,就进行更新这条记录 |
|
service.saveOrUpdate(user); |
|
System.out.println("service.list() = " + service.list()); |
|
} |
|
|
|
@Test |
|
void insertBatch() { |
|
service.list().forEach(System.out::println); |
|
} |
|
|
|
@Test |
|
void select() { |
|
System.out.println("service.count() = " + service.count()); |
|
System.out.println("service.list() = " + service.list()); |
|
System.out.println("service.listMaps() = " + service.listMaps()); |
|
System.out.println("service.listObjs() = " + service.listObjs()); |
|
System.out.println("service.getById(1) = " + service.getById(1)); |
|
} |
|
|
|
@Test |
|
void test() { |
|
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); |
|
queryWrapper.ge("id", 5) |
|
.or(qw -> qw.ge("age",25).le("age",100)); |
|
// 小于 |
|
// queryWrapper.lt("id", 3); |
|
|
|
// 小于等于 |
|
// queryWrapper.le("id", 3); |
|
|
|
// 大于 |
|
// queryWrapper.gt("id", 2); |
|
|
|
// 大于等于 |
|
// queryWrapper.ge("id", 3); |
|
|
|
// 等于 |
|
// queryWrapper.eq("id", 3); |
|
|
|
// 不等于 |
|
// queryWrapper.ne("id", 2); |
|
|
|
// 联合起来写,代表大于等于2且小于等于5,且不等于3 |
|
// queryWrapper.ge("id", 2) |
|
// .le("id", 5) |
|
// .ne("id", 3); |
|
|
|
// between条件 |
|
// queryWrapper.between("id", 2, 4); // id在2到4 |
|
|
|
// like条件 |
|
// queryWrapper.like("name", "J%"); |
|
|
|
// not like |
|
// queryWrapper.notLike("name", "%a%"); |
|
|
|
// 查询某几个字段 |
|
// queryWrapper.select("id", "name"); |
|
|
|
/* |
|
也可以使用group by,select中也可以写别名 |
|
*/ |
|
// queryWrapper.groupBy("age"); |
|
// queryWrapper.select("age 年龄", "count(*) 数量"); |
|
// queryWrapper.select("age as 年龄", "count(*) as 数量"); |
|
|
|
// in的用法 |
|
// queryWrapper.in("id", 2, 3, 4); |
|
// queryWrapper.in("id", List.of(2, 3, 4)); |
|
|
|
// not in |
|
// queryWrapper.notIn("id", 2, 3, 5); |
|
// queryWrapper.notIn("id", List.of(2, 3, 5)); // jdk 9新API |
|
|
|
// 排序 |
|
// 正序 |
|
// queryWrapper.orderBy(true, true, "id"); |
|
// queryWrapper.orderByAsc("id"); |
|
// 倒序 |
|
// queryWrapper.orderBy(true, true, "id"); |
|
// queryWrapper.orderByDesc("id"); |
|
|
|
// ((id >= ?) OR (age >= ? AND age <= ?)) |
|
// queryWrapper.ge("id", 5) |
|
// .or() |
|
// .ge("age", 25) |
|
// .le("age", 100); |
|
|
|
|
|
// queryWrapper.ge("id", 5) |
|
// .or(wrapper -> wrapper.ge("age", 25).le("age", 100)); |
|
|
|
// queryWrapper.isNull("列名").isNotNull("列名") |
|
userMapper.selectList(queryWrapper).forEach(System.out::println); |
|
} |
|
|
|
|
|
@Test |
|
void delete() { |
|
service.removeById(1); |
|
List<Integer> list = new ArrayList<>(); |
|
list.add(1); |
|
list.add(2); |
|
list.add(3); |
|
service.removeByIds(list); |
|
list.add(4); |
|
service.removeBatchByIds(list); |
|
System.out.println("service.list() = " + service.list()); |
|
HashMap<String, Object> map = new HashMap<>(); |
|
map.put("id", 5); |
|
map.put("name", "Billie"); |
|
service.removeByMap(map); |
|
System.out.println("service.list() = " + service.list()); |
|
} |
|
|
|
|
|
@Test |
|
void condition() { |
|
// QueryWrapper<User> qw = new QueryWrapper<>(); |
|
// qw.select("max(age) as max,min(age) as min"); |
|
// List<Map<String, Object>> res = userMapper.selectMaps(qw); |
|
// System.out.println(res.get(0)); |
|
// System.out.println(Integer.valueOf(res.get(0).get("max").toString())); |
|
// System.out.println(Integer.valueOf(res.get(0).get("min").toString())); |
|
|
|
|
|
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); |
|
String name = "名称"; |
|
Integer minAge = 10, maxAge = 1500; |
|
/** |
|
* 如果名字不为空,会按照名称进行查询 |
|
* 如果最小年龄不为空,同时也会将这个条件加进去 |
|
* 如果最大年龄不会空,也会将这个条件加进去 |
|
*/ |
|
queryWrapper.eq(name != null, "name", name); |
|
queryWrapper.ge(minAge != null, "age", minAge); |
|
queryWrapper.le(maxAge != null, "age", maxAge); |
|
userMapper.selectList(queryWrapper); |
|
} |
|
|
|
@Test |
|
void lambdaQueryWrapperTest() { |
|
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>(); |
|
lqw.select(User::getName,User::getAge); |
|
userMapper.selectMaps(lqw).forEach(System.out::println); |
|
} |
|
|
|
@Test |
|
public void selectByWrapperPage() { |
|
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); |
|
Page<User> page = new Page<>(1,10); |
|
IPage<User> iPage = userMapper.selectPage(page, queryWrapper); |
|
|
|
System.out.println("总页数: " + iPage.getPages()); |
|
System.out.println("数据总数: " + iPage.getTotal()); |
|
// 返回分页数据 |
|
List<User> userList = iPage.getRecords(); |
|
userList.forEach(System.out::println); |
|
|
|
// 获取分页的其他信息 |
|
System.out.println("当前页号 = " + iPage.getCurrent()); |
|
System.out.println("页大小 = " + iPage.getSize()); |
|
} |
|
|
|
@Test |
|
public void selectByWrapperPageByAge() { |
|
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); |
|
Page<User> page = new Page<>(1,3); |
|
IPage<User> iPage = userMapper.selectByAge(page,25); |
|
iPage.getRecords().forEach(System.out::println); |
|
|
|
System.out.println("总页数: " + iPage.getPages()); |
|
System.out.println("数据总数: " + iPage.getTotal()); |
|
// 返回分页数据 |
|
List<User> userList = iPage.getRecords(); |
|
userList.forEach(System.out::println); |
|
|
|
// 获取分页的其他信息 |
|
System.out.println("当前页号 = " + iPage.getCurrent()); |
|
System.out.println("页大小 = " + iPage.getSize()); |
|
} |
|
|
|
}
|
|
|