mybatis plus(MP快速入门)基础使用教程
0、介绍
MyBatis Plus,以下使用简称MP。MP是一个MyBatis的增强工具,它简化了MyBatis的开发流程,提供了许多实用的功能,例如自动生成代码、分页查询、逻辑删除等。使用MP可以大大提高开发效率,减少重复代码的编写。
要使用MP,你需要在项目中添加MP的依赖,并在代码中使用MP提供的API。具体使用方法可以参考MP的官方文档,官方网站:baomidou.com/ ,MP是由国人开发,官网全为中文,可无障碍阅读和学习,阅读和学习成本低,但是官网内容较多,本文直接了当教你怎么使用,进阶学习还请移步官网或其他学习教程和网站。 按照以下步骤即可开始使用MP。目前只在springboot和spring cloud中使用过,没有尝试在spring中使用,官方也没有spring相关
1、安装MP依赖
1.1 springboot依赖
maven的pom文件:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
gradle的build.gradle文件:
//Gradle Version:<4.1 gradle4.1版本之前是compile
compile group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.4.1'
//Gradle 7.0. gradle7.0版本之后是implementation
implementation 'com.baomidou:mybatis-plus-boot-starter:3.4.1'
1.2 spring依赖
maven的pom文件:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>最新版本</version>
</dependency>
gradle的build.gradle文件:
//Gradle Version:<4.1 gradle4.1版本之前是compile
compile group: 'com.baomidou', name: 'mybatis-plus', version: '最新版本'
//Gradle 7.0. gradle7.0版本之后是implementation
implementation 'com.baomidou:mybatis-plus:最新版本'
gradle安装依赖注意一下grdle版本,7.0之后就是implementation替换compile了。
2、MP基础使用
2.1 mapper接口继承BaseMapper即可使用
Mapper接口继承BaseMapper接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
如下示例:
@Mapper
public interface TestMaper extends BaseMapper<TestEntity> {
}
2.2 Service中调用basemapper方法使用
如下示例:
@Service
@Transactional
public class TestService {
@Autowired
private TestMaper testMapper;
pubulic void deleteByI(String id){
testMapper.deleteById(id);
}
//分页查询
pubulic void testSelectPage(){
IPage<User> page=new Page<>(1,3);
testMapper.selectPage(page,null);
System.out.println("当前页码值:"+page.getCurrent());
System.out.println("每页显示数:"+page.getSize());
System.out.println("总页数:"+page.getPages());
System.out.println("总条数:"+page.getTotal());
System.out.println("当前页数据:"+page.getRecords());
}
2.3 BaseMapper源码中提供的方法
如下: deleteById和selectPage方法在2.2中有简单示例。
/**
* 插入一条记录
*
* @param entity 实体对象
*/
int insert(T entity);
/**
* 根据 ID 删除
*
* @param id 主键ID
*/
int deleteById(Serializable id);
/**
* 根据 columnMap 条件,删除记录
*
* @param columnMap 表字段 map 对象
*/
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
/**
* 根据 entity 条件,删除记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
*/
int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 删除(根据ID 批量删除)
*
* @param idList 主键ID列表(不能为 null 以及 empty)
*/
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
/**
* 根据 ID 修改
*
* @param entity 实体对象
*/
int updateById(@Param(Constants.ENTITY) T entity);
/**
* 根据 whereEntity 条件,更新记录
*
* @param entity 实体对象 (set 条件值,可以为 null)
* @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
*/
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper);
/**
* 根据 ID 查询
*
* @param id 主键ID
*/
T selectById(Serializable id);
/**
* 查询(根据ID 批量查询)
*
* @param idList 主键ID列表(不能为 null 以及 empty)
*/
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
/**
* 查询(根据 columnMap 条件)
*
* @param columnMap 表字段 map 对象
*/
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap);
/**
* 根据 entity 条件,查询一条记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 Wrapper 条件,查询总记录数
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 entity 条件,查询全部记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 Wrapper 条件,查询全部记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 Wrapper 条件,查询全部记录
* <p>注意: 只返回第一个字段的值</p>
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 entity 条件,查询全部记录(并翻页)
*
* @param page 分页查询条件(可以为 RowBounds.DEFAULT)
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
<E extends IPage<T>> E selectPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
/**
* 根据 Wrapper 条件,查询全部记录(并翻页)
*
* @param page 分页查询条件
* @param queryWrapper 实体对象封装操作类
*/
<E extends IPage<Map<String, Object>>> E selectMapsPage(E page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
2.4 小结
小结:安装好MP依赖后,在mapper接口上直接继承BaseMapper后,一般的CRUD即可不用编写Mapper.xml文件写SQL代码,直接在Service层直接即可调用BaseMapper提供的方法操作数据表。
3、MP进阶使用
3.1 条件查询
完全的条件构造器使用请移步官网学习:www.baomidou.com/pages/10c80…
//①按条件查询:基础的使用方式
QueryWrapper<Entity> qw=new QueryWrapper<Entity>();
qw.lt("age", 18);
List<Entity> list = testMapper.selectList(qw);
System.out.println(list);
//②按条件查询:方法引用+lambda的使用方式
QueryWrapper<Entity> qw = new QueryWrapper<Entity>();
qw.lambda().lt(Entity::getAge, 10);
List<Entity> list1 = testMapper.selectList(qw);
System.out.println(list1);
//③按条件查询:方法引用+lambda的简写方式
LambdaQueryWrapper<Entity> lqw = new LambdaQueryWrapper<Entity>();
lqw.lt(Entity::getAge, 10);
List<Entity> list2 = testMapper.selectList(lqw);
System.out.println(list2);
注:lt等价于“<” 如:lt("age", 18)
等价于age < 18
3.2 条件and和or
主动调用or
表示紧接着下一个方法不是用and
连接!(不调用or
则默认为使用and
连接),如下:
3.2.1 and
LambdaQueryWrapper<Entity> lqw = new LambdaQueryWrapper<Entity>();
//10到30岁之间
lqw.lt(Entity::getAge, 30).gt(Entity::getAge, 10);
List<Entity> list = testMapper.selectList(lqw);
System.out.println(list);
3.2.2 or
LambdaQueryWrapper<Entity> lqw = new LambdaQueryWrapper<Entity>();
//小于10岁或者大于30岁
lqw.lt(Entity::getAge, 10).or().or().gt(Entity::getAge, 30);
List<Entity> list = testMapper.selectList(lqw);
System.out.println(list);
4、结语
以上是MP的简单使用入门示例,进阶学习可参照官网。 全文所用Entity为数据库表的对应实体类,需使用注解并使用注解@Table指定表名。同时,MP在对复杂的SQL语句,如联表查询等来说,实现需要一点难度,所以MP支持的CRUD仅限于单表。全文如有不对或者需要补充的地方,欢迎各位读者提出。
转载自:https://juejin.cn/post/7233304089371476005