【Mybatis-Plus】 Mapper 接口使用总结

news/2024/10/30 21:21:24/

本文主要内容是 Mapper接口的使用 示例。

注意:我们需要方法 执行的SQL 是什么样的,以及返回值。

Mapper CRUD 接口

新增

insert方法

==>  Preparing: INSERT INTO user ( user_id, user_name, create_time ) VALUES ( ?, ?, ? ) 
==> Parameters: halo(String), 哈喽啊(String), 2023-07-11 14:01:59.864(Timestamp)
<==    Updates: 1

删除

delete

根据 entity 条件,删除记录

注意:queryWrapper后面可以跟条件构造器

@Test
public void delete() {QueryWrapper queryWrapper = new QueryWrapper<>();System.out.println(userDao.delete(queryWrapper));
}
==>  Preparing: DELETE FROM user 
==> Parameters: 
<==    Updates: 1

deleteBatchIds

删除(根据ID 批量删除)

@Test
public void deleteBatchIds() {List<Integer>  deleteIds = new ArrayList<>();deleteIds.add(1);deleteIds.add(2);deleteIds.add(3);System.out.println(userDao.deleteBatchIds(deleteIds));
}
==>  Preparing: DELETE FROM user WHERE id IN ( ? , ? , ? ) 
==> Parameters: 1(Integer), 2(Integer), 3(Integer)
<==    Updates: 1

deleteById

根据 ID 删除

 @Test
public void deleteById(){Integer id=1;System.out.println(userDao.deleteById(id));
}

执行的SQL语句

==>  Preparing: DELETE FROM user WHERE id=? 
==> Parameters: 1(Integer)
<==    Updates: 0

deleteByMap

根据 columnMap 条件,删除记录

  @Testpublic void deleteByMap() {Map<String,Object> params = new HashMap<>();params.put("user_id","halo");System.out.println(userDao.deleteByMap(params));}
==>  Preparing: DELETE FROM user WHERE user_id = ? 
==> Parameters: halo(String)
<==    Updates: 0

修改

update

根据 whereWrapper 条件,更新记录

@Test
public void update() {User user = new User();user.setUserId("tony");QueryWrapper queryWrapper = new QueryWrapper();System.out.println(userDao.update(user,queryWrapper));
}
==>  Preparing: UPDATE user SET user_id=? 
==> Parameters: tony(String)
<==    Updates: 1

updateById

根据 ID 修改

@Testpublic void updateById() {User user = new User();user.setUserId("halo");user.setUserName("哈喽啊");user.setCreateTime(new Date());user.setId(1);System.out.println(userDao.updateById(user));}
==>  Preparing: UPDATE user SET user_id=?, user_name=?, create_time=? WHERE id=? 
==> Parameters: halo(String), 哈喽啊(String), 2023-07-11 14:17:01.184(Timestamp), 1(Integer)
<==    Updates: 0

查询

selectById
根据 ID 查询

  @Testpublic void selectById() {Integer i = 1;System.out.println(userDao.selectById(i));}
==>  Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user WHERE id=? 
==> Parameters: 1(Integer)
<==      Total: 0

selectOne

根据 entity 条件,查询一条记录

注意:如果 查询出来的记录是多条的话,会报错的哦

 @Test
public void selectOne() {System.out.println(userDao.selectOne(new QueryWrapper<User>().eq("user_id","halo")));
}
==>  Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user WHERE (user_id = ?) 
==> Parameters: halo(String)
<==      Total: 0

selectBatchIds

查询(根据ID 批量查询)

@Test
public void selectBatchIds() {List<Integer> list = new ArrayList<>();list.add(1);list.add(2);userDao.selectBatchIds(list);   
}
==>  Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user WHERE id IN ( ? , ? ) 
==> Parameters: 1(Integer), 2(Integer)
<==      Total: 0

selectList

根据 entity 条件,查询全部记录

 @Test
public void selectList() {System.out.println(userDao.selectList(new QueryWrapper<>()));
}
==>  Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user 
==> Parameters: 
<==    Columns: id, user_id, user_name, create_time, dept_id
<==        Row: 5, tony, 哈喽啊, 2023-07-11 14:11:20.0, null
<==        Row: 6, halo1, 哈喽啊2, 2023-07-11 14:35:42.0, null
<==        Row: 7, halo3, 哈喽啊24, 2023-07-11 14:35:51.0, null
<==      Total: 3

selectByMap

查询(根据 columnMap 条件)

注意:这里的返回值与selectMaps 返回值的区别哦

@Test
public void selectByMap() {Map<String,Object> params = new HashMap();params.put("user_id","halo");System.out.println(userDao.selectByMap(params));
}
==>  Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user 
==> Parameters: 
<==    Columns: id, user_id, user_name, create_time, dept_id
<==        Row: 5, tony, 哈喽啊, 2023-07-11 14:11:20.0, null
<==        Row: 6, halo1, 哈喽啊2, 2023-07-11 14:35:42.0, null
<==        Row: 7, halo3, 哈喽啊24, 2023-07-11 14:35:51.0, null
<==      Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@a451491]
[User(id=5, userId=tony, userName=哈喽啊, createTime=Tue Jul 11 14:11:20 CST 2023, deptId=null), 
User(id=6, userId=halo1, userName=哈喽啊2, createTime=Tue Jul 11 14:35:42 CST 2023, deptId=null), 
User(id=7, userId=halo3, userName=哈喽啊24, createTime=Tue Jul 11 14:35:51 CST 2023, deptId=null)]

selectMaps

查询(根据 columnMap 条件)

注意返回值哦

  @Test
public void selectMaps() {List<Map<String, Object>>  list = userDao.selectMaps(new QueryWrapper<>());System.out.println(list);
}
==>  Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user 
==> Parameters: 
<==    Columns: id, user_id, user_name, create_time, dept_id
<==        Row: 5, tony, 哈喽啊, 2023-07-11 14:11:20.0, null
<==        Row: 6, halo1, 哈喽啊2, 2023-07-11 14:35:42.0, null
<==        Row: 7, halo3, 哈喽啊24, 2023-07-11 14:35:51.0, null
<==      Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@a451491]
[{create_time=2023-07-11 14:11:20.0, user_id=tony, user_name=哈喽啊, id=5},{create_time=2023-07-11 14:35:42.0, user_id=halo1, user_name=哈喽啊2, id=6}, {create_time=2023-07-11 14:35:51.0, user_id=halo3, user_name=哈喽啊24, id=7}]

selectObjs

根据 Wrapper 条件,查询全部记录。注意: 只返回第一个字段的值

@Test
public void selectObjs() {List<Object> list =  userDao.selectObjs(new QueryWrapper<>());System.out.println(list);
}
==>  Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user 
==> Parameters: 
<==    Columns: id, user_id, user_name, create_time, dept_id
<==        Row: 5, tony, 哈喽啊, 2023-07-11 14:11:20.0, null
<==        Row: 6, halo1, 哈喽啊2, 2023-07-11 14:35:42.0, null
<==        Row: 7, halo3, 哈喽啊24, 2023-07-11 14:35:51.0, null
<==      Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@7ceb4478]
[5, 6, 7]

selectPage

根据 entity 条件,查询全部记录(并翻页)

注意;这里我们需要配置一下分页哦

@Configuration
public class MyBatisPlusConfig {/*** 分页插件*/@Beanpublic PaginationInterceptor paginationInterceptor() {return new PaginationInterceptor();}
}
 @Test
public void selectPage() {Page page = new Page();page.setCurrent(1);page.setPages(10);QueryWrapper queryWrapper = new QueryWrapper();queryWrapper.eq("user_id","tony");IPage<Map<String, Object>> iPage = userDao.selectPage(page,queryWrapper);System.out.println(iPage.toString());}
JsqlParserCountOptimize sql=SELECT  id,user_id,user_name,create_time,dept_id  FROM user WHERE (user_id = ?)
==>  Preparing: SELECT COUNT(1) FROM user WHERE (user_id = ?) 
==> Parameters: tony(String)
<==    Columns: COUNT(1)
<==        Row: 1
==>  Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user WHERE (user_id = ?) LIMIT ?,? 
==> Parameters: tony(String), 0(Long), 10(Long)
<==    Columns: id, user_id, user_name, create_time, dept_id
<==        Row: 5, tony, 哈喽啊, 2023-07-11 14:11:20.0, null
<==      Total: 1

selectMapsPage

根据 Wrapper 条件,查询全部记录(并翻页)

   @Testpublic void selectMapsPage() {Page page = new Page();page.setCurrent(1);page.setPages(10);QueryWrapper queryWrapper = new QueryWrapper();queryWrapper.eq("user_id","tony");IPage<Map<String, Object>> iPage = userDao.selectMapsPage(page,queryWrapper);System.out.println(iPage);}
JsqlParserCountOptimize sql=SELECT  id,user_id,user_name,create_time,dept_id  FROM user WHERE (user_id = ?)
==>  Preparing: SELECT COUNT(1) FROM user WHERE (user_id = ?) 
==> Parameters: tony(String)
<==    Columns: COUNT(1)
<==        Row: 1
==>  Preparing: SELECT id,user_id,user_name,create_time,dept_id FROM user WHERE (user_id = ?) LIMIT ?,? 
==> Parameters: tony(String), 0(Long), 10(Long)
<==    Columns: id, user_id, user_name, create_time, dept_id
<==        Row: 5, tony, 哈喽啊, 2023-07-11 14:11:20.0, null
<==      Total: 1

selectCount

根据 Wrapper 条件,查询总记录数

@Test
public void selectCount() {System.out.println(userDao.selectCount(new QueryWrapper<>()));
}
==>  Preparing: SELECT COUNT( 1 ) FROM user 
==> Parameters: 
<==    Columns: COUNT( 1 )
<==        Row: 3
<==      Total: 1

http://www.ppmy.cn/news/836499.html

相关文章

mysql,对表的简单操作

一.创建表并插入数据 mysql> create table worker(-> department_id int(11) not null comment 部门号,-> worker_id int(11) primary key not null comment 职工号,-> worker_date date not null comment 工作时间,-> wages float(8,2) not null comment 工资…

SpringBoot快速实践 --Ⅰ

文章目录 启动一个SpringBoot项目如何替换内嵌容器玩转SpringBoot配置全局异常处理过滤器拦截器使用Lombok简洁代码使用IDEA HTTP Client进行接口调试 启动一个SpringBoot项目 如果你觉得使用官网来创建太慢了&#xff0c;那你直接把以前项目的依赖粘过来就行了&#xff1a; …

【sfu】追踪ms如何提供flv服务

media request : mrmedia source : msMediaSourceMgr 根据mr 创建ms std::shared_ptr<MediaSource> MediaSourceMgr::FetchOrCreateSource(MediaSource::Config& cfg,std::shared_ptr<MediaRequest> req)

Freepiano如何使用sf2音源,sfark如何转换至sf2?

补充2&#xff1a; 我发现 Riccardo Loi has created two other piano files in SFZ format while running his Keppys Studio. Here they are:Keppys Steinway Piano (as a 7z compressed file, compressed size 232MB).Keppys Farfisa Grand Piano (as a 7z compressed file…

ISP-ASF

1. 概述 1.1 高频与低频区分&#xff1a; 如何区分图像的高频信息和低频信息&#xff0c;所谓高频就是该像素点与周围像素差异较大&#xff0c;常见于一副图像的边缘细节和噪声等&#xff1b;而低频就是该像素点与周围像素差异变化不大&#xff0c;一般体现为图像的平坦区&am…

elasitcsearch管道应用 加工字段

需求 根据 字段 1 和字段2来加工字段3 当 字段 1 2 没有值 的时候不加工 并且字段四 是固定只 使用管道来 解决 1 创建管道 PUT _ingest/pipeline/activity_allintenet_pipeline {"description": " Network wide travel data ","processors": […

k8s(Kubernetes)设置 pod,Deployment 域名自定义映射ip,hosts 解析 HostAliases

核心 hostAliases:# 解决服务器内,因路由器配置不全&#xff0c; 不能访问外网问题- hostnames:- graph.requarks.ioip: 104.26.14.122案例 wiki k8s Deployment 直接 编辑修改 Deployment kubectl edit Deployment wiki最后内容如下&#xff0c;如需保存 按 esc 键&#xff…

Radeon 驱动升级到20.x后如何设置显卡切换

笔记本是E480&#xff0c;intel集成显卡AMD独显双显卡。 以前Radeon 19.x版本显卡驱动设置里有个页面可以指定程序使用的显卡&#xff0c;但是升级到Radeno 20.x后&#xff0c;显卡驱动中就没有这个设置了。 在windows10 1909中这个设置在下面这个地方&#xff1a; 设置&…