Skip to content

ORM框架:MybatisPlus

About 728 wordsAbout 2 min

mybatis-plus

2025-01-08

Java学的半吊子, 慢慢回顾学习。今天要学习的是数据库ORM框架-MybatisPlus。

Java领域的ORM(Object Relation Mapping)框架有很多,像是Spring JDBC、Spring Data JPA、Mybatis、Mybatis-plus等等,反正我都没有用过。

随便在网上都能找到一个篇5,6w+阅读的文章,我们就跟着下面这个文章学习:

MyBatis-Plus快速入门-(干货满满+超详细)

为什么加一个@SpringBootApplication, 修改main函数调用SpringApplication.run()就能够启动一个SpringBoot项目。

@SpringBootApplication
public class le_MybatisPlusStartUp {
    public static void main(String[] args) {
        // System.out.println("Hello world!");
        SpringApplication.run(le_MybatisPlusStartUp.class ,args);
    }
}

一、Mybatis-Plus基本操作

Mybatis-Plus作为Mybatis的增强,自己封装了很多简单好用的方法,来解脱自己写sql。

方法一、根据主键ID去查询单个结果selectById

/**
* 根据 ID 查询
*
* @param id 主键ID
* T selectById(Serializable id);
*/
User user = userMapper.selectByID(1);

方法二、查询多条数据库中的记录selectList

/**
* 根据 entity 条件,查询全部记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
* List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
*/
List<User> users = userMaper.selectList(null);

方法三、查询多条数据库中的记录-条件查询selectList(wrapper)

/**
* 根据 entity 条件,查询全部记录
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
* List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
*/
QueryWrapper wrapper = new QueryWrapper();
wrapper.eq("id",1);//相当于where id=1
List<User> list = userMapper.selectList(wrapper);

方法四、根据主键的id集合进行多条数据的查询selectBatchIds

/**
* 查询(根据ID 批量查询)
*
* @param idList 主键ID列表(不能为 null 以及 empty)
* List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList);
*/
List list1 = Arrays.asList(1,2);
List<User> list2 = userMapper.selectBatchIds(list1);

方法五、分页查询selectPage

/**
* 根据 entity 条件,查询全部记录(并翻页)
*
* @param page         分页查询条件(可以为 RowBounds.DEFAULT)
* @param queryWrapper 实体对象封装操作类(可以为 null)
* <P extends IPage<T>> P selectPage(P page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
*/
IPage<User> page = new Page<>(1,2);//参数一:当前页,参数二:每页记录数
//这里想加分页条件的可以如方法三自己构造条件构造器
IPage<User> userIPage = userMapper.selectPage(page, null);

二、配置Mybatis-Plus项目

SpringBoot+Mybatis-Plus的入门搭建与配置测试

Changelog

6/3/25, 1:49 AM
View All Changelog
  • d3a6d-Merge branch 'dev1'on

求求了,快滚去学习!!!

求求了求求了,快去学习吧!

【题单】贪心算法

不知道方向的时候,可以多看看书,书会给你指明下一步该干什么,加油!