准备工作
(1)、创建测试的数据库表
(2)、配置mybatisPlus环境
1、Dao层(Mapper层)的增删改查方法总结
一、Mapper层insert插入
下面是官网的截图
我的例子:
@Test
public void insertTest() {
User user = new User();
user.setName("测试增加一个");
userMapper.insert(user);
}
打印的日志入下
==> Preparing: INSERT INTO tb_user ( name, create_time ) VALUES ( ?, ? )
==> Parameters: 测试增加一个(String), null
<== Updates: 1
二、Mapper层delete删除
官网的解释如下:
我的列子如下
(1)、delete方法
传入删除条件为 name=“测试增加一个”的方法
@Test
public void deleteTest() {
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("name","测试增加一个");
int delete = userMapper.delete(userQueryWrapper);
System.out.println(delete);
}
使用Lambda表达式构造条件,调用delete方法删除,和上面一个一样
@Test
public void deleteTest() {
// QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
// userQueryWrapper.eq("name","测试增加一个");
// int delete = userMapper.delete(userQueryWrapper);
// System.out.println(delete);
LambdaQueryWrapper<User> objectLambdaQueryWrapper = Wrappers.<User>lambdaQuery();
objectLambdaQueryWrapper.eq(User::getName,"测试增加一个");
int delete = userMapper.delete(objectLambdaQueryWrapper);
System.out.println("删除了"+delete);
}
(2)、deleteBatchIds 批量删除ID
@Test
public void deleteTest2() {
int i = userMapper.deleteBatchIds(Arrays.asList(11, 1094592041087729666L));
System.out.println("删除了"+i);
}
(3)、deleteById 根据 ID 删除
@Test
public void deleteTest3() {
int i = userMapper.deleteById(11);
System.out.println(i);
}
(4)、deleteByMap 根据 columnMap 条件,删除记录
删除前
删除后
这个就和第一个delete方法差不多的,满足map中的条件就能删除
@Test
public void deleteTest4() {
Map<String,Object> someMap=new HashMap<>();
someMap.put("name","测试增加一个");
someMap.put("age",22);
int i = userMapper.deleteByMap(someMap);
System.out.println("删除了"+i);
}
三、Mapper层update修改
官网的解释如下
(1)、updateById
修改前数据库表如下
修改后数据库表如下
除了通过userMapper.updateById的方法进行修改,还能通过实体类进行user.updateById();直接调用哦,同样user也能调用insert方法,以及 user.insertOrUpdate();当实体类中ID存在就修改,不存在就新增
@Test
public void updateTest() {
User user = new User();
user.setName("建设局三");
user.setId(1094592041087729690L);
user.setAge(22);
// boolean updateById = user.updateById();
int id = userMapper.updateById(user);
System.out.println(id);
}
(2)、update 方法
update方法,传入参数,第一个是修改的参数,第二个是查询需要修改的条件
public void updateTest22() {
QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>();
userQueryWrapper.eq("name","建设").eq("age",22);
User user = new User();
user.setAge(999);
int update = userMapper.update(user, userQueryWrapper);
System.out.println(update);
}
采用 Lambda防误写功能,方式和上面一样的,修改方法
@Test
public void updateTest2() {
LambdaUpdateWrapper<User> lambdaUpdateWrapper = Wrappers.<User>lambdaUpdate();
lambdaUpdateWrapper.eq(User::getName, "建设").eq(User::getAge, 22).set(User::getAge,32);
int update = userMapper.update(null, lambdaUpdateWrapper);
System.out.println(update);
}
四、Mapper层select查询
官网的解释如下
(1)selectById
通过ID查询
@Test
public void selectTest() {
User user = userMapper.selectById(1094592041087729691L);
System.out.println(user);
}
(2)selectOne
返回一个实体类,限制只能查询一个,如果满足查询条件的有多个,则会报错
@Test
public void selectTest2() {
QueryWrapper<User> userQueryWrapper = new QueryWrapper<User>();
userQueryWrapper.eq("name","建设");
User user = userMapper.selectOne(userQueryWrapper);
System.out.println(user);
}
(3)selectBatchIds
通过ID的集合查询,返回一个实体类的数组
public void selectTest3() {
List<User> userList = userMapper.selectBatchIds(Arrays.asList(1094592041087729693L,1094592041087729692L));
userList.forEach(System.out::println);
}
(4)、selectList 根据 entity 条件,查询全部记录
1、采用QueryWrapper编辑查询条件
@Test
public void selectTest4() {
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("name","建设").le("age",999);
List<User> userList = userMapper.selectList(userQueryWrapper);
userList.forEach(System.out::println);
}
2、采用LambdaQueryWrapper编辑查询条件(具有防误写功能)
public void selectTest4() {
// lamaDa表达式防误写功能
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getName, "建设").le(User::getAge, 999);
List<User> userList = userMapper.selectList(queryWrapper);
userList.forEach(System.out::println);
}
(5)、selectByMap 通过Map添加查询条件
@Test
public void selectTest5() {
Map<String, Object> map = new HashMap<>();
map.put("name","建设");
map.put("age",999);
List<User> userList = userMapper.selectByMap(map);
userList.forEach(System.out::println);
}
(6)、 selectMaps 根据 Wrapper 条件,查询全部记录
@Test
public void selectTest6() {
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getEmail, "3333");
List<Map<String, Object>> list = userMapper.selectMaps(queryWrapper);
list.forEach(System.out::println);
}
(7)、selectObjs 根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值
@Test
public void selectTest7() {
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getName, "建设");
List<Object> objects = userMapper.selectObjs(queryWrapper);
objects.forEach(System.out::println);
}
打印的代码如下,
==> Preparing: SELECT id,name,age,email,manager_id,create_time,update_time,version,deleted FROM tb_user WHERE (name = ?)
==> Parameters: 建设(String)
<== Columns: id, name, age, email, manager_id, create_time, update_time, version, deleted
<== Row: 1094592041087729690, 建设, 32, null, null, null, null, 1, 0
<== Row: 1094592041087729692, 建设, 999, null, null, null, null, 1, 0
<== Row: 1094592041087729693, 建设, 999, 3333, null, null, null, 1, 0
<== Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@34a2d6e0]
1094592041087729690
1094592041087729692
1094592041087729693
(8)、selectPage 根据 entity 条件,查询全部记录(并翻页)
查询返回的是一个IPage的对象,里面包含总记录数getTotal,和查询内容getRecords,以及当前页等其他信息
@Test
public void selectTest8() {
IPage<User> userIPage = new Page<>(1, 2);
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("name", "建设");
IPage<User> userIPage1 = userMapper.selectPage(userIPage, userQueryWrapper);
System.out.println("总数为"+userIPage1.getTotal());
System.out.println("查询内容"+userIPage1.getRecords());
}
查询结果
总数为3
查询内容[User(id=1094592041087729690, name=建设, age=32, email=null, managerId=null, createTime=null, updateTime=null, version=1, deleted=0), User(id=1094592041087729692, name=建设, age=999, email=null, managerId=null, createTime=null, updateTime=null, version=1, deleted=0)]
(9)、selectMapsPage 根据 Wrapper 条件,查询全部记录(并翻页)
@Test
public void selectTest9() {
// IPage<User> userIPage2 = new Page<>(1, 2);
IPage<Map<String,Object>> userIPage2=new Page<>(1,2);
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("name", "建设");
IPage<Map<String, Object>> mapIPage = userMapper.selectMapsPage(userIPage2, userQueryWrapper);
System.out.println("纪录record:"+mapIPage.getRecords());
System.out.println("记录数Total:"+mapIPage.getTotal());
System.out.println("总共页数Pages:" +mapIPage.getPages());
}
查询的结果为
纪录record:[{deleted=0, name=建设, id=1094592041087729690, version=1, age=32}, {deleted=0, name=建设, id=1094592041087729692, version=1, age=999}]
记录数Total:3
总共页数Pages:2
(10)selectCount 根据 Wrapper 条件,查询总记录数
@Test
public void selectTest10() {
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("name", "建设");
Integer integer = userMapper.selectCount(userQueryWrapper);
System.out.println("查询的总记录数" + integer);
}
2、Service CRUD 接口总结
一、Service 层的Save增加
官网的介绍如下
(1)、save 插入一条记录(选择字段,策略插入)
@Test
public void saveTest(){
User user = new User();
user.setAge(888);
user.setName("浩浩");
boolean save = userService.save(user);
System.out.println(save);
}
(2)、saveBatch 插入(批量)
通过数组的方式插入多个
@Test
public void saveTest2() {
User user = new User();
user.setAge(888);
user.setName("浩浩");
User user2 = new User();
user2.setAge(8888);
user2.setName("浩浩皓");
List<User> userList=Arrays.asList(user,user2);
boolean b = userService.saveBatch(userList);
System.out.println(b);
}
3、saveBatch 插入(批量)
虽然都是通过数组的方式插入,但是这个数量官方说法是,插入批次数量,也是每次系统执行SQL的数量打印测试代码入下
@Test
public void saveTest2() {
User user = new User();
user.setAge(888);
user.setName("浩浩");
User user2 = new User();
user2.setAge(8888);
user2.setName("浩浩皓");
User user3 = new User();
user3.setAge(8888);
user3.setName("浩浩皓222");
// List<User> userList=Arrays.asList(user,user2,user3);
List<User> userList=new ArrayList<>();
userList.add(user);
userList.add(user2);
userList.add(user3);
boolean b = userService.saveBatch(userList,3);
System.out.println(b);
}
当设置为1的时候,每次执行的条数为1的时候
boolean b = userService.saveBatch(userList,1);
System.out.println(b);
因为数组长度为3,所以执行3次SQL
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@14fded9d] will be managed by Spring
==> Preparing: INSERT INTO tb_user ( name, age, create_time ) VALUES ( ?, ?, ? )
==> Parameters: 浩浩(String), 888(Integer), null
==> Preparing: INSERT INTO tb_user ( name, age, create_time ) VALUES ( ?, ?, ? )
==> Parameters: 浩浩皓(String), 8888(Integer), null
==> Preparing: INSERT INTO tb_user ( name, age, create_time ) VALUES ( ?, ?, ? )
==> Parameters: 浩浩皓222(String), 8888(Integer), null
true
当设置每次执行条数为2时,
boolean b = userService.saveBatch(userList,2);
System.out.println(b);
因为数组长度为3,所以总共执行两次SQL,
==> Preparing: INSERT INTO tb_user ( name, age, create_time ) VALUES ( ?, ?, ? )
==> Parameters: 浩浩(String), 888(Integer), null
==> Parameters: 浩浩皓(String), 8888(Integer), null
==> Preparing: INSERT INTO tb_user ( name, age, create_time ) VALUES ( ?, ?, ? )
==> Parameters: 浩浩皓222(String), 8888(Integer), null
true
二、Service 层的SaveOrUpdate增加或者修改
(1)、saveOrUpdate (TableId 注解存在更新记录,否插入一条记录)
如果有setId,且setId在数据库表中存在,这样就是调用update修改方法,否则是insert 插入方法
@Test
public void saveOrUpdateTest() {
User user = new User();
user.setId(1094592041087729719L);
user.setName("建设");
user.setAge(222);
boolean b = userService.saveOrUpdate(user);
}
(2)saveOrUpdate (根据updateWrapper尝试更新,否继续执行saveOrUpdate(T)方法)
1、将名称name为建设,且年龄age小于等于999,的表单,修改成名称为建设重建,age为222
2、如果查询条件userQueryWrapper ,表中都不满足,则将user实体做新增操作
@Test
public void saveOrUpdateTest2() {
User user = new User();
user.setName("建设重建");
user.setAge(222);
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("name","建设").le("age",999);
boolean b = userService.saveOrUpdate(user,userQueryWrapper);
}
3、saveOrUpdateBatch (批量修改插入),或者后面带参数,每次执行的条数
@Test
public void saveOrUpdateTest3() {
//第一个设置真实ID值
User user = new User();
user.setId(1094592041087729721L);
user.setAge(888);
user.setName("浩浩");
//
User user2 = new User();
user2.setAge(777);
user2.setName("浩2浩");
//
User user3 = new User();
user3.setAge(666);
user3.setName("浩3浩");
List<User> userList = new ArrayList<>();
userList.add(user);
userList.add(user2);
userList.add(user3);
boolean b = userService.saveOrUpdateBatch(userList);
System.out.println(b);
}
打印的日志入下
==> Preparing: UPDATE tb_user SET name=?, age=?, update_time=? WHERE id=?
==> Parameters: 浩浩(String), 888(Integer), null, 1094592041087729721(Long)
==> Preparing: INSERT INTO tb_user ( name, age, create_time ) VALUES ( ?, ?, ? )
==> Parameters: 浩2浩(String), 777(Integer), null
==> Parameters: 浩3浩(String), 666(Integer), null
三、Service 层的 Remove 移除
官方的解释为入下
(1)、remove 移除
通过条件构造来移除对应的列表
@Test
public void removeTest() {
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getName,"建设重建").le(User::getAge,222);
boolean remove = userService.remove(queryWrapper);
System.out.println(remove);
}
(2)、removeById 根据 ID 删除
@Test
public void removeTest2() {
boolean b = userService.removeById(1094592041087729723L);
System.out.println(b);
}
(3)、removeByMap (根据 columnMap 条件,删除记录)
设置key 对应数据库字段,value是对应查询的值,复合条件的就删除
@Test
public void removeTest3() {
Map<String, Object> map = new HashMap<>();
map.put("name", "建设");
map.put("age", 777);
boolean b = userService.removeByMap(map);
}
(4)removeByIds批量删除
@Test
public void removeTest4() {
boolean b = userService.removeByIds(Arrays.asList(1094592041087729720L, 1094592041087729721L));
System.out.println(b);
}
四、Service 层的Update修改
官方的定义入下
(1)、update (根据 UpdateWrapper 条件,更新记录 需要设置sqlset)
@Test
public void updateTest() {
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper.set("name","小浩浩").eq("name","浩浩");
boolean update = userService.update(userUpdateWrapper);
}
(2)update (根据 whereWrapper 条件,更新记录)
前面user是修改的内容,后面是查询条件
@Test
public void updateTest2() {
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper.eq("name","小浩浩");
User user = new User();
user.setAge(999);
user.setEmail("857903178@uuu");
boolean update = userService.update(user, userUpdateWrapper);
}
(3)updateById 通过ID删除,传入一个实体类对象
@Test
public void updateTest3() {
User user = new User();
user.setId(1094592041087729694L);
user.setAge(999);
user.setEmail("857903178@uuu");
boolean updateById = userService.updateById(user);
}
(4)updateBatchById (批量修改)传入一个实体类的数组,后面还能跟一个int 参数,表示每批次修改多少个
@Test
public void updateTest4() {
User user = new User();
user.setId(1094592041087729721L);
user.setAge(888);
user.setName("浩浩");
User user2 = new User();
user2.setId(1094592041087729694L);
user2.setAge(88998);
user2.setName("浩浩");
List<User> userList=Arrays.asList(user,user2);
boolean b = userService.updateBatchById(userList);
}
五、Service 层的Get方法
官方的解释如下
(1)、getById (根据 ID 查询)
@Test
public void GetTest() {
User byId = userService.getById(1094592041087729721L);
System.out.println(byId);
}
(2)、getOne 根据 Wrapper,查询一条记录。结果集,如果是多个会抛出异常,随机取一条加上限制条件 wrapper.last(“LIMIT 1”)
如果使用getOne,则在查询条件后面添加last(“LIMIT 1”),来限制查询一个,不然会报错
或者是在查询条件后面加false,不报异常
getOne方法,默认后面跟true,返回一条不报错,但是如果数据库有多条就要报错,后面跟false,如果多条,日志警告( Warn: execute Method There are 5 results.),返回第一条
T getOne(Wrapper<T> queryWrapper, false);
@Test
public void GetTest2() {
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper.eq("name","浩浩").last("LIMIT 1");
User one = userService.getOne(userUpdateWrapper);
System.out.println(one);
}
(3)getMap 根据 Wrapper,查询一条记录
@Test
public void GetTest3() {
UpdateWrapper<User> userUpdateWrapper = new UpdateWrapper<>();
userUpdateWrapper.eq("name","浩浩");
Map<String, Object> map = userService.getMap(userUpdateWrapper);
map.forEach((key, value) -> {
System.out.print(key + "-" + value + "\t");
});
}
日志记录如下
==> Preparing: SELECT id,name,age,email,manager_id,create_time,update_time,version,deleted FROM tb_user WHERE (name = ?)
==> Parameters: 浩浩(String)
<== Columns: id, name, age, email, manager_id, create_time, update_time, version, deleted
<== Row: 1094592041087729694, 浩浩, 22, 857903178@uuu, null, null, null, 1, 0
<== Row: 1094592041087729721, 浩浩, 888, 857903178@uuu, null, null, null, 1, 0
<== Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3458eca5]
Warn: execute Method There are 2 results.
deleted-0 name-浩浩 id-1094592041087729694 version-1 age-22 email-857903178@uuu
(4)getObj 根据 Wrapper,查询一条记录
这个我没有弄
五、Service 层的List方法
官方的文档为
(1)list 查询所有
@Test
public void LIstTest() {
List<User> list = userService.list();
list.forEach(System.out::println);
}
(2)list 查询列表(根据QueryWrapper中的条件查询全部)
@Test
public void LIstTest() {
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("name","浩浩");
List<User> list = userService.list(userQueryWrapper);
list.forEach(System.out::println);
}
(3)listByIds (通过传入的Id数组查询全部)
@Test
public void LIstTest2() {
List<User> userList = userService.listByIds(Arrays.asList(1094592041087729694L, 1094592041087729721L));
userList.forEach(System.out::println);
}
(4)listByMap 查询(根据 columnMap 条件),通过Map添加查询条件
@Test
public void LIstTest4() {
Map<String, Object> stringObjectHashMap = new HashMap<>();
// userService
stringObjectHashMap.put("name","浩浩");
List<User> userList = userService.listByMap(stringObjectHashMap);
userList.forEach(System.out::println);
}
(5)listMaps 查询列表,可以添加条件或者不添加条件查询,添加条件就是QueryWrapper自定义查询条件
@Test
public void LIstTest3() {
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("name","浩浩");
List<Map<String, Object>> list = userService.listMaps(userQueryWrapper);
list.forEach(System.out::println);
}
(6)listObjs 查询全部,返回查询的ID值,也就是返回记录数
@Test
public void LIstTest5() {
List<Object> objects = userService.listObjs();
objects.forEach(System.out::println);
}
打印的结果如下所示
=> Preparing: SELECT id,name,age,email,manager_id,create_time,update_time,version,deleted FROM tb_user
==> Parameters:
<== Columns: id, name, age, email, manager_id, create_time, update_time, version, deleted
<== Row: 1087982257332887553, 大boss, 40, boss@baomidou.com, null, 2019-01-11 14:20:20, null, 1, 0
<== Row: 1088248166370832385, 王王王, 26, wtf@baomidou.com, 1087982257332887553, 2019-02-05 11:12:22, null, 1, 0
<== Row: 1088250446457389058, 李艺伟, 33, 857903497@qq.com, 1088248166370832385, 2019-02-14 08:31:16, null, 1, 0
<== Row: 1094590409767661570, 张雨琪, 31, zjq@baomidou.com, 1088248166370832385, 2019-02-14 09:15:15, null, 1, 0
<== Row: 1094592041087729671, 蒋皓洁, 26, 22222, 1087982257332887553, 2021-02-18 15:03:35, null, 1, 0
<== Row: 1094592041087729689, 测试增加一个, null, null, null, null, null, 1, 0
<== Row: 1094592041087729691, 嘻嘻哈哈哈, 22, null, 1094592041087729690, null, null, 1, 0
<== Row: 1094592041087729694, 浩浩, 22, 857903178@uuu, null, null, null, 1, 0
<== Row: 1094592041087729721, 浩浩, 888, 857903178@uuu, null, null, null, 1, 0
<== Total: 9
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@1084f78c]
1087982257332887553
1088248166370832385
1088250446457389058
1094590409767661570
1094592041087729671
1094592041087729689
1094592041087729691
1094592041087729694
1094592041087729721
(7)、listObjs根据queryWrapper拼接的查询条件,返回总的记录数
// 根据 Wrapper 条件,查询全部记录
List<Object> listObjs(Wrapper<T> queryWrapper);
六、service层的Page方法
官网的解释如下
1、page(无条件分页)
@Test
public void PageTest() {
IPage<User> page =new Page<User>(1,5);
IPage<User> page1 = userService.page(page);
System.out.println("纪录record:" + page1.getRecords());
System.out.println("记录数Total:" + page1.getTotal());
System.out.println("总共页数Pages:" + page1.getPages());
}
2、page(带条件分页)
@Test
public void PageTest2() {
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("name","浩浩");
IPage<User> page =new Page<User>(1,2);
IPage<User> page1 = userService.page(page,userQueryWrapper);
System.out.println("纪录record:" + page1.getRecords());
System.out.println("记录数Total:" + page1.getTotal());
System.out.println("总共页数Pages:" + page1.getPages());
}
3、pageMaps (无条件查询)
@Test
public void PageTest3() {
IPage<Map<String, Object>> page = new Page<>(1, 2);
IPage<Map<String, Object>> page1 = userService.pageMaps(page);
System.out.println("纪录record:" + page1.getRecords());
System.out.println("记录数Total:" + page1.getTotal());
System.out.println("总共页数Pages:" + page1.getPages());
}
4、pageMaps 带条件查询
@Test
public void PageTest3() {
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("name","浩浩");
IPage<Map<String, Object>> page = new Page<>(1, 2);
IPage<Map<String, Object>> page1 = userService.pageMaps(page,userQueryWrapper);
System.out.println("纪录record:" + page1.getRecords());
System.out.println("记录数Total:" + page1.getTotal());
System.out.println("总共页数Pages:" + page1.getPages());
}
七、Service层的Count方法
1、count 全表搜索数据长度
@Test
public void countTest() {
int count = userService.count();
System.out.println(count);
}
2、count 条件搜索查询长度
@Test
public void countTest2() {
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("name","浩浩");
int count = userService.count(userQueryWrapper);
System.out.println(count);
}