Mybatis-Plus 实战完整学习笔记(七)------select测试二
1.查询selectOne (3.0.3版)
@Test
public void selectMethod() throws SQLException { // 根据ID获取一个对象的数据 Employee employee =employeeMapper.selectOne(new QueryWrapper<Employee>().eq("id","1")); if (employee!=null) {
logger.info("++一个员工信息+++++"+gson.toJson(employee));
} }
其相当于SQL语句:SELECT id,last_name,email,gender,age FROM tbl_employee WHERE id = ?
2.3版本的写法是直接注入一个对象,对象的set值就是查询的条件,同时,ID要是Long或Integer类型,条件一定要唯一,否则,会报数据重复的异常。
Employee employee = new Employee();
employee.setId(1); Employee result =employeeMapper.selectOne(employee); if (result!=null) {
logger.info("++一个员工信息+++++"+gson.toJson(result));
}
2、查询selectMaps(3.0.3新出)
@Test
public void selectMethod() throws SQLException { // 6、多个Map查询
Employee employee = employeeMapper.selectById(1); QueryWrapper<Employee> employeeQueryWrapper = new QueryWrapper<>(); List<Map<String, Object>> employeeList =employeeMapper.selectMaps(employeeQueryWrapper); if (!employeeList.isEmpty()) {
logger.info("++一个员工信息+++++"+gson.toJson(employeeList));
} }
写法跟其他的查询没有区别,只是返回的结果,变成List<Map<String,Object>>,条件查询跟上边的一样。
返回样式
++一个员工信息+++++[{"gender":"0","last_name":"Marry","id":1,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":2,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":3,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":4,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":12,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":13,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":14,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":15,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":16,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":17,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":18,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":19,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":20,"email":"1","age":55},{"gender":"0","last_name":"Marry","id":21,"email":"1","age":55}] (TestCRUD.java:159)
本来以为是要装多个map进去查询就写成下边的样子,结果不是那个意思
// 6、多个Map查询
Employee employee = employeeMapper.selectById(1); Map<String,Object> map1 = new HashMap<>(16);
map1.put("email",employee.getEmail());
map1.put("age",employee.getAge()); Employee employee2 = employeeMapper.selectById(2);
Map<String,Object> map2 = new HashMap<>(16);
map2.put("email",employee2.getEmail());
map2.put("age",employee2.getAge()); List<Map<String,Object>> mapList = new ArrayList<>();
mapList.add(map1);
mapList.add(map2); QueryWrapper<List<Map<String, Object>>> maps = new QueryWrapper<List<Map<String, Object>>>();
maps.setEntity((mapList)); List<Map<String, Object>> employeeList =employeeMapper.selectMaps(maps);
3、查询selectObjs
@Test
public void selectMethod() throws SQLException { // 7、selectObjs
Employee employee = employeeMapper.selectById(1); List<Object> employeeList =employeeMapper.selectObjs(new QueryWrapper<Employee>().eq("email",employee.getEmail())); if (!employeeList.isEmpty()) {
logger.info("++一个员工信息+++++"+gson.toJson(employeeList));
} }
用法一样,就是返回的结果是个Object类型。
4、统计结果查询selectCount
@Test
public void selectMethod() throws SQLException { // 8、selectCount
Employee employee = employeeMapper.selectById(1); Integer count = employeeMapper.selectCount(new QueryWrapper<Employee>().eq("email", employee.getEmail())); if (count > 0) {
logger.info("++统计结果:+++++" + count);
} }
5、分页查询selectPage
@Test
public void selectMethod() throws SQLException { // 9、分页查询 Integer count = employeeMapper.selectCount(new QueryWrapper<Employee>().between("age", 0, 100)
.eq("gender", 0)); IPage<Employee> employeeIPage = new Page<Employee>(1, 3,count); IPage<Employee> employeeList = employeeMapper.selectPage(employeeIPage,new QueryWrapper<Employee>()
.between("age", 0, 100)
.eq("gender", 0)); if (!employeeList.getRecords().isEmpty()) {
logger.info("++一个员工信息+++++" + gson.toJson(employeeList));
} }
6、分页查询返回map类型的selectMapsPage
@Test
public void selectMethod() throws SQLException { // 10、分页查询Map结果返回
Integer count = employeeMapper.selectCount(new QueryWrapper<Employee>().between("age", 0, 100)
.eq("gender", 0)); IPage<Employee> employeeIPage = new Page<Employee>(1, 3, count); IPage<Map<String, Object>> employeeList = employeeMapper.selectMapsPage(employeeIPage, new QueryWrapper<Employee>()
.between("age", 0, 100)
.eq("gender", 0)); if (!employeeList.getRecords().isEmpty()) {
logger.info("++一个员工信息+++++" + gson.toJson(employeeList));
} }
System.out.println("返回数据:"+employeeIPage.getRecords());
System.out.println("总条数:"+page.getTotal());
System.out.println("当前页码:"+page.getCurrent());
System.out.println("总页码:"+page.getPages());
System.out.println("每页显示条数:"+page.getSize());
System.out.println("是否有上一页:"+page.hasPrevious());
System.out.println("是否有下一页:"+page.hasNext());
System.out.println("返回的数据:"+page.getRecords());
Mybatis-Plus 实战完整学习笔记(七)------select测试二的更多相关文章
- Mybatis-Plus 实战完整学习笔记(六)------select测试一
查询方法(3.0.3) 1.查询一个员工的数据 @Test public void selectMethod() throws SQLException { // 根据ID获取一个对象的数据 Empl ...
- Mybatis-Plus 实战完整学习笔记(八)------delete测试
1.根据ID删除一个员工deleteById /** * 删除客户 * * @throws SQLException */ @Test public void deletedMethod() thro ...
- Mybatis-Plus 实战完整学习笔记(五)------insert测试
一.更新操作探究(3.0.3版本) demo /** * 修改用户 * @throws SQLException */ @Test public void update() throws SQLExc ...
- Mybatis-Plus 实战完整学习笔记(一)------简介
第一章 简介 1. 什么是MybatisPlus MyBatis-Plus(简称 MP)是一个 MyBatis 的增强工具,在 MyBatis 的基础上只 ...
- Mybatis-Plus 实战完整学习笔记(十一)------条件构造器删除,修改,conditon
1.修改功能--其他过滤方式跟select一样 /** * 修改条件构造器 * @throws SQLException */ @Test public void selectUpdate() thr ...
- Mybatis-Plus 实战完整学习笔记(十)------条件构造器核心用法大全(下)
31.升序orderByAsc 31.升序orderByAsc List<Employee> employeeList = employeeMapper.selectList(new Qu ...
- Mybatis-Plus 实战完整学习笔记(九)------条件构造器核心用法大全(上)
一.Mybatisplus通用(公共方法)CRUD,一共17种(3.0.3版),2.3系列也是这么多,这个新版本一定程度进行了改造和删减. 二.构造器UML图(3.0.3)-----实体包装器,主要用 ...
- Mybatis-Plus 实战完整学习笔记(四)------全局参数配置
一.全局配置设置 (1)全局配置Id自动生成 <!--定义mybatisplus全局配置--> <bean id="globalConfig" class=&qu ...
- Mybatis-Plus 实战完整学习笔记(二)------环境搭建
第二章 使用实例 1.搭建测试数据库 -- 创建库 CREATE DATABASE mp; -- 使用库 USE mp; -- 创建表 CREATE TABLE tbl_employee( ...
随机推荐
- 开机进入boot menu和application menu,无法开机
1.开机点击F1进入到bios界面 2.进入Security—Secure Boot—Disabled 如果不修改Secure boot选项为Disabled,在光驱引导时可能会出现报错 3. ...
- java遍历当前会话所有Session
//方法一:通过遍历的方法进行遍历 String FileName=""; HttpSession session=request.getSession();//获取session ...
- SI和DI
si和di是8086CPU中和bx功能相近的寄存器,si和di不能够分成两个8位寄存器来使用. 用si和di实现将字符串"welcome to masm!"复制到它后面的数据区中. ...
- html5的地理位置定位
html5提供的地理位置定位使开发人员不用借助其他软件就能轻松实现位置查找,地图应用,导航等功能. 地理位置定位基本原理GPS, WIFI, IP, 手机信号基站 核心对象Geolocation是wi ...
- 比特币学习-Transaction的locktime属性
Locktime, also known as nLockTime from the variable name used in the reference client, defines the e ...
- js去除字符串空格(空白符)
使用js去除字符串内所带有空格,有以下三种方法: ( 1 ) replace正则匹配方法 去除字符串内所有的空格:str = str.replace(/\s*/g,""); 去除字 ...
- Vue-CLI3.x 高版本覆盖低版本Vue-CLI2.x 解决方法
因为Vue-cli 3 和旧版使用了相同的vue命令,所以Vue CLI 2(vue-cli) 被覆盖了.如果你任然需要旧版本的 vue init 功能 ,你可以全局安装一个交接工具: npm ins ...
- 9.Mysql字符集
9.字符集9.1 字符集概述 字符集就是一套文字符号及其编码.比较规则的集合. ASCII(American Standard Code for Information Interchange)字符集 ...
- list集合与HashMap的使用
List<Map<String, Object>> arrays= new ArrayList<Map<String, Object>>(); Hash ...
- PAT 1018 锤子剪刀布(20)
1018 锤子剪刀布 (20)(20 分) 大家应该都会玩"锤子剪刀布"的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统计双方的胜.平.负次数,并且给出双方 ...