MyBatis-Plus (opens new window)(简称 MP)是一个 MyBatis (opens new window)的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
二、特性- 特性 无侵入:只做增强不做改变,引入它不会对现有工程产生影响,如丝般顺滑
- 损耗小:启动即会自动注入基本CURD,性能基本无损耗,直接面向对象 *** 作
- 强大的 CRUD *** 作:内置通用 Mapper、通用Service,仅仅通过少量配置即可实现单表大部分 CRUD *** 作,更有强大的条件构造器,满足各类使用需求
- 支持 Lambda形式调用:通过 Lambda 表达式,方便的编写各类查询条件,无需再担心字段写错
- 支持主键自动生成:支持多达 4种主键策略(内含分布式唯一 ID 生成器 - Sequence),可自由配置,完美解决主键问题
- 支持 ActiveRecord 模式:支持ActiveRecord 形式调用,实体类只需继承 Model 类即可进行强大的 CRUD *** 作
- 支持自定义全局通用 *** 作:支持全局通用方法注入( Write once, useanywhere )
- 内置代码生成器:采用代码或者Maven 插件可快速生成 Mapper 、Model 、 Service 、 Controller层代码,支持模板引擎,更有超多自定义配置等您来使用
- 内置分页插件:基于 MyBatis 物理分页,开发者无需关心具体 *** 作,配置好插件之后,写分页等同于普通 List 查询
- 分页插件支持多种数据库:支持MySQL、MariaDB、Oracle、DB2、H2、HSQL、SQLite、Postgre、SQLServer 等多种数据库
- 内置性能分析插件:可输出 SQL 语句以及其执行时间,建议开发测试时启用该功能,能快速揪出慢查询
- 内置全局拦截插件:提供全表 delete、 update *** 作智能分析阻断,也可自定义拦截规则,预防误 *** 作
- 支持数据库: MySQL,Oracle,DB2,H2,HSQL,SQLite,PostgreSQL,SQLServer,Phoenix,Gauss ,ClickHouse,Sybase,OceanBase,Firebird,Cubrid,Goldilocks,csiidb
生成的SQL: select id,name,age,email from user where id = ?
Map<String, Object> map = userMapper.selectMapById(1l);
System.out.println(map);
结果:
// {name=test, id=1, age=18, email=test1@baomidou.com}
Set<String> keySet = map.keySet();
System.out.println(keySet);
结果:
// [name, id, age, email]
② 通过条件构造器查询一个集合,若没有条件,可以设置参数为null
生成的SQL: SELECT id,name,age,email FROM user
List<User> list = userMapper.selectList(null);
list.forEach(System.out::println);
结果:
//User(id=6, name=Jack, age=20, email=test2@baomidou.com)
//User(id=7, name=Sandy, age=21, email=test4@baomidou.com)
//User(id=8, name=Jone, age=18, email=test1@baomidou.com)
//User(id=9, name=Tom, age=28, email=test3@baomidou.com)
③ 根据map集合中所设置的条件查询数据,结果执行条件是 AND
生成的SQL: SELECT id,name,age,email FROM user WHERE name = ? AND age = ?
Map<String , Object> map = new HashMap<>();
map.put("name","Billie");
map.put("age",24);
List<User> userList = userMapper.selectByMap(map);
userList.forEach(System.out::println);
结果:
//User(id=2, name=Billie, age=24, email=test5@baomidou.com)
④ 通过ID批量查询
生成的SQL: SELECT id,name,age,email FROM user WHERE id IN ( ? , ? , ? , ? )
List<Long> list = Arrays.asList(1l, 2l, 3l, 4l);
List<User> users = userMapper.selectBatchIds(list);
users.forEach(System.out::println);
结果:
// User(id=1, name=Risc, age=18, email=test1@baomidou.com)
// User(id=2, name=Billie, age=24, email=test5@baomidou.com)
// User(id=4, name=Tom, age=21, email=test4@baomidou.com)
⑤ 通过ID查询数据
生成的SQL: SELECT id,name,age,email FROM user WHERE id=?
User user = userMapper.selectById(2l);
System.out.println(user);
结果:
// User(id=2, name=Billie, age=24, email=test5@baomidou.com)
2. 添加
① 通过对象实体类添加,只添加对象含值的属性
生成的SQL: INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
User user = new User();
user.setName("张三");
user.setAge(20);
user.setEmail("2856479@qq.com");
int result = userMapper.insert(user);
System.out.println("result:" + result);
System.out.println("id:" + user.getId());
结果:
// result = 1
// id = 192168011234 这是雪花算法生成的,默认使用雪花算法生成id
② 批量添加对象数据 注意:是单个sql语句循环添加的
生成的SQL: INSERT INTO user ( id, name, age, email ) VALUES ( ?, ?, ?, ? )
List<User> list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
User user = new User();
user.setName("ylh" + i);
user.setAge(20 + i);
user.setEmail("ylh@qq.com" + i);
list.add(user);
}
boolean result = userService.saveBatch(list);
System.out.println("result:" + result);
结果:
// result:true
3. 删除
① 通过id删除用户信息
生成的SQL: DELETE FROM user WHERE id=?
int result = userMapper.deleteById(1517498273672314882L);
System.out.println("result:" + result);
结果:
// result: 1
② 根据map集合中所设置的条件删除数据,结果执行条件是 AND
生成的SQL: DELETE FROM user WHERE name = ? AND age = ?
Map<String ,Object> map = new HashMap<>();
map.put("name","张三");
map.put("age",20);
int result = userMapper.deleteByMap(map);
System.out.println("result:" + result);
结果:
// result: 1
③ 通过ID实现批量删除
生成的SQL: DELETE FROM user WHERE id IN ( ? , ? )
List<Long> list = Arrays.asList(1l,2l, 3l);
int result = userMapper.deleteBatchIds(list);
System.out.println("result:" + result);
结果:
result:3
④ 通过ID实现批量删除,在增加逻辑删除之后,删除变成了修改
生成的SQL: UPDATE t_user SET is_deleted=1 WHERE id IN ( ? , ? , ? ) AND is_deleted=0
List<Long> list = Arrays.asList(1l,2l, 3l);
int result = userMapper.deleteBatchIds(list);
System.out.println("result:" + result);
结果:
// result:3
4. 修改
① 通过传入新对象和条件修改数据(必须要有ID),如果条件为null将会全部修改(一定要避免)
生成的SQL: UPDATE user SET name= ? (这就是没有设置Id的后果)
User user = new User();
user.setName("TOP1");
int result = userMapper.updateById(user);
System.out.println("result:" + result);
结果:
// result:3
② 通过Id来修改数据
生成的SQL: UPDATE user SET name=? WHERE id=?
User user = new User();
user.setId(1l);
user.setName("TOP1");
int result = userMapper.updateById(user);
System.out.println("result:" + result);
结果:
// result:1
以下是条件构造器的使用:
在此之前先看一张图:
在真正开发的过程中,组装条件是常见的功能,而这些条件数据来源于用户输入,是可选的,因此我们在组装这些条件时,必须先判断用户是否选择了这些条件,若选择则需要组装该条件,若没有选择则一定不能组装,以免影响SQL执行的结果。我们可以使用带condition参数的重载方法构建查
询条件,简化代码的编写。
生成的SQL: SELECT id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (name LIKE ? AND age BETWEEN ? AND ? AND email IS NOT NULL)
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name","a")
.between("age",20,30)
.isNotNull("email");
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
结果:
// User(id=4, name=Sandy, age=21, email=test4@baomidou.com, isDeleted=0)
② 查询用户信息,按照年龄的降序排序,若年龄相同,则按照ID升序排序
生成的SQL: SELECT id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 ORDER BY age DESC,id ASC
QueryWrapper<User> queryMapper = new QueryWrapper<>();
queryMapper.orderByDesc("age")
.orderByAsc("id");
List<User> list = userMapper.selectList(queryMapper);
list.forEach(System.out::println);
结果:
// User(id=5, name=Billie, age=24, email=test5@baomidou.com, isDeleted=0)
// User(id=4, name=Sandy, age=21, email=test4@baomidou.com, isDeleted=0)
// User(id=6, name=张三, age=20, email=2856479@qq.com, isDeleted=0)
// User(id=7, name=张三, age=20, email=2856479@qq.com, isDeleted=0)
// User(id=8, name=张三, age=20, email=2856479@qq.com, isDeleted=0)
③ 查询用户的用户名、年龄、邮箱信息
生成的SQL: SELECT name,age,email FROM t_user WHERE is_deleted=0
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.select("name","age","email");
List<Map<String, Object>> list = userMapper.selectMaps(queryWrapper);
list.forEach(System.out::println);
结果:
// {name=Tom, age=28, email=test3@baomidou.com}
// {name=Bngle, age=21, email=test4@baomidou.com}
// {name=Billie, age=24, email=test5@baomidou.com}
// {name=张三, age=20, email=2856479@qq.com}
// {name=张三, age=20, email=2856479@qq.com}
// {name=张三, age=20, email=2856479@qq.com}
// {name=测试update2, age=23, email=test@qq.com}
// {name=测试update2, age=18, email=test@qq.com}
④ 查询id小于等于100的用户信息
生成的SQL: SELECT id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (id IN (select id from t_user where id <= 9))
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.inSql("id","select id from t_user where id <= 9");
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
结果:
// User(id=3, name=Tom, age=28, email=test3@baomidou.com, isDeleted=0)
// User(id=4, name=Bngle, age=21, email=test4@baomidou.com, isDeleted=0)
// User(id=5, name=Billie, age=24, email=test5@baomidou.com, isDeleted=0)
// User(id=6, name=张三, age=20, email=2856479@qq.com, isDeleted=0)
// User(id=7, name=张三, age=20, email=2856479@qq.com, isDeleted=0)
// User(id=8, name=张三, age=20, email=2856479@qq.com, isDeleted=0)
// User(id=9, name=测试update2, age=23, email=test@qq.com, isDeleted=0)
⑤ 组装条件情况,将符合条件的参数写入SQL
生成的SQL: SELECT id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (name LIKE ? AND age <= ?)
String username = "a";
Integer ageBegin = null;
Integer ageEnd = 30;
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// isNotBlank判断某个字符串是否不为 空、不为null、不为空白字符串
if (StringUtils.isNotBlank(username)){
queryWrapper.like("name",username);
}
if (ageBegin != null){
queryWrapper.ge("age",ageBegin);
}
if (ageEnd != null){
queryWrapper.le("age",ageEnd);
}
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
结果:
// User(id=10, name=测试update2, age=18, email=test@qq.com, isDeleted=0)
⑥ 组装条件情况,将符合条件的参数写入SQL
生成的SQL: SELECT id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (name LIKE ? AND age <= ?)
String username = "a";
Integer ageBegin = null;
Integer ageEnd = 30;
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like(StringUtils.isNotBlank(username),"name",username)
.ge(ageBegin!=null,"age",ageBegin)
.le(ageEnd!=null,"age",ageEnd);
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
结果:
// User(id=10, name=测试update2, age=18, email=test@qq.com, isDeleted=0)
⑦ 组装条件情况,将符合条件的参数写入SQL(使用Lambda表达式)
生成的SQL: SELECT id,name,age,email,is_deleted FROM t_user WHERE is_deleted=0 AND (name LIKE ? AND age <= ?)
String username = "a";
Integer ageBegin = null;
Integer ageEnd = 30;
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(StringUtils.isNotBlank(username),User::getName,username)
.ge(ageBegin!=null,User::getAge,ageBegin)
.le(ageEnd!=null,User::getAge,ageEnd);
List<User> list = userMapper.selectList(queryWrapper);
list.forEach(System.out::println);
结果:
// User(id=10, name=测试update2, age=18, email=test@qq.com, isDeleted=0)
2. 删除
① 删除邮箱地址为null的用户信息
生成的SQL: UPDATE t_user SET is_deleted=1 WHERE is_deleted=0 AND (email IS NULL)
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.isNull("email");
int result = userMapper.delete(queryWrapper);
System.out.println("result:" + result);
结果:
// result:1
3. 修改
① 将 (年龄大于20并且用户名中包含有a) 或邮箱为 null 的用户信息修改
生成的SQL: UPDATE t_user SET name=? WHERE is_deleted=0 AND (age > ? AND name LIKE ? OR email IS NULL)
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.gt("age",20)
.like("name","a")
.or()
.isNull("email");
User user = new User();
user.setName("Angle");
int result = userMapper.update(user, queryWrapper);
System.out.println("result:" + result);
结果:
// result:1
② 将用户中包含有a并且(年龄大于20或邮箱为null)的用户信息修改 (QueryWrapper)
生成的SQL: UPDATE t_user SET name=?, email=? WHERE is_deleted=0 AND (name LIKE ? AND (age >= ? OR email IS NULL))
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.like("name","a")
.and(i->i.ge("age",20).or().isNull("email"));
User user = new User();
user.setName("测试update2");
user.setEmail("test@qq.com");
int result = userMapper.update(user, queryWrapper);
System.out.println("result:" + result);
结果:
// result:1
③ 将用户中包含有a并且(年龄大于20或邮箱为null)的用户信息修改 (UpdateWrapper)
生成的SQL: UPDATE t_user SET name=?,email=? WHERE is_deleted=0 AND (name LIKE ? AND (age >= ? OR email IS NULL))
UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
updateWrapper.like("name","a")
.and(i -> i.ge("age",20).or().isNull("email"));
updateWrapper.set("name","小黑").set("email","abc@qq.com");
int result = userMapper.update(null, updateWrapper);
System.out.println("result:" + result);
结果:
// result:1
④ 将用户中包含有a并且(年龄大于20或邮箱为null)的用户信息修改(使用Lambda表达式)
生成的SQL: UPDATE t_user SET name=?,email=? WHERE is_deleted=0 AND (name LIKE ? AND (age >= ? OR email IS NULL))
LambdaUpdateWrapper<User> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.like(User::getName,"a")
.and(i -> i.ge(User::getAge,20).or().isNull(User::getEmail));
updateWrapper.set(User::getName,"小黑").set(User::getEmail,"abc@qq.com");
int result = userMapper.update(null, updateWrapper);
System.out.println("result:" + result);
结果:
// result:1
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)