千家信息网

mybatis-plus如何实现查询传入参数Map,返回List<Map>方式

发表于:2025-02-23 作者:千家信息网编辑
千家信息网最后更新 2025年02月23日,这篇文章主要介绍mybatis-plus如何实现查询传入参数Map,返回List方式,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!mybatis-plus 查询传入参数Map,
千家信息网最后更新 2025年02月23日mybatis-plus如何实现查询传入参数Map,返回List<Map>方式

这篇文章主要介绍mybatis-plus如何实现查询传入参数Map,返回List方式,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

mybatis-plus 查询传入参数Map,返回List

原因有时实体类属性不够用,又不想写自定义VO了,所以用map,这样直接返回前台用起来也很方便

1、mapper.xml

注意是resultType 不是resultMap 否则报错

2、mapper.java

List> getOrder(Map map);

3、service 组装查询条件

public List> getOrder(String storeId) {    Map map=new HashMap();    map.put("orderId",orderId);    return storeApiOrderMapper.getOrder(map);}

mybatis-plus 基本使用

首先我们需要创建一个数据库表

用于演示MyBatis-Plus的基本用法。

CREATE TABLE `user` (    `id` varchar(32) NOT NULL,    `username` varchar(32) DEFAULT '',    `password` varchar(32) DEFAULT '',    PRIMARY KEY (`id`));

然后创建一个Spring Boot项目

pom.xml和配置如下:

    4.0.0    org.kaven    mybatis-plus    1.0-SNAPSHOT            org.springframework.boot        spring-boot-starter-parent        2.3.4.RELEASE                        1.8                            org.springframework.boot            spring-boot-starter                            org.springframework.boot            spring-boot-starter-test                            org.springframework.boot            spring-boot-starter-webflux                            com.baomidou            mybatis-plus-boot-starter            3.4.0                            mysql            mysql-connector-java            5.1.49                            org.projectlombok            lombok                                                    org.springframework.boot                spring-boot-maven-plugin                        
spring:  application:    name: mybatis-plus  datasource:    driver-class-name: com.mysql.jdbc.Driver    username: root    password: 123456    url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&useSSL=falseserver:  port: 8085  logging:  level:    root: warn    com.kaven.mybatisplus.dao: trace  pattern:    console: '%p%m%n'

实体类User:

package com.kaven.mybatisplus.entity;import com.baomidou.mybatisplus.annotation.TableField;import com.baomidou.mybatisplus.annotation.TableId;import com.baomidou.mybatisplus.annotation.TableName;import lombok.Data;@TableName("user")@Datapublic class User {    @TableId    private String id;    @TableField("username")    private String username;    @TableField("password")    private String password;    /**     * 使用 @TableField(exist = false) ,表示该字段在数据库中不存在 ,所以不会插入到数据库中     * 使用 transient 、 static 修饰的属性也不会插入数据库中     */    @TableField(exist = false)    private String phone;}

Mapper接口UserMapper:

package com.kaven.mybatisplus.dao;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.kaven.mybatisplus.entity.User;import org.springframework.stereotype.Component;@Componentpublic interface UserMapper extends BaseMapper {}

UserMapper需要继承MyBatis-Plus的BaseMapper接口。

BaseMapper接口源码如下,其实就是定义了一些数据库表的CRUD方法。

package com.baomidou.mybatisplus.core.mapper;import com.baomidou.mybatisplus.core.conditions.Wrapper;import com.baomidou.mybatisplus.core.metadata.IPage;import java.io.Serializable;import java.util.Collection;import java.util.List;import java.util.Map;import org.apache.ibatis.annotations.Param;public interface BaseMapper extends Mapper {    int insert(T entity);    int deleteById(Serializable id);    int deleteByMap(@Param("cm") Map columnMap);    int delete(@Param("ew") Wrapper wrapper);    int deleteBatchIds(@Param("coll") Collection idList);    int updateById(@Param("et") T entity);    int update(@Param("et") T entity, @Param("ew") Wrapper updateWrapper);    T selectById(Serializable id);    List selectBatchIds(@Param("coll") Collection idList);    List selectByMap(@Param("cm") Map columnMap);    T selectOne(@Param("ew") Wrapper queryWrapper);    Integer selectCount(@Param("ew") Wrapper queryWrapper);    List selectList(@Param("ew") Wrapper queryWrapper);    List> selectMaps(@Param("ew") Wrapper queryWrapper);    List selectObjs(@Param("ew") Wrapper queryWrapper);    > E selectPage(E page, @Param("ew") Wrapper queryWrapper);    >> E selectMapsPage(E page, @Param("ew") Wrapper queryWrapper);}

启动类:

package com.kaven.mybatisplus;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan(basePackages = "com.kaven.mybatisplus.dao")public class AppRun {    public static void main(String[] args) {        SpringApplication.run(AppRun.class , args);    }}

@MapperScan(basePackages = "com.kaven.mybatisplus.dao")这个一定要加上。

这样就构建好了项目,使用MyBatis-Plus可以不用写Mapper.xml配置文件,是不是贼方便。

我们先在数据库中添加几行数据,方便演示。

我们来演示几个基本的查询方法

package com.kaven.mybatisplus.dao;import com.kaven.mybatisplus.entity.User;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.util.Map;@RunWith(SpringRunner.class)@SpringBootTestpublic class UserMapperTest {    @Autowired    private UserMapper userMapper;    @Test    public void selectList(){        // 条件设置为null , 就是没有条件,即查询所有数据        List userList = userMapper.selectList(null);        userList.forEach(System.out::println);    }    @Test    public void selectById(){        // 根据Id查询        User user = userMapper.selectById("1");        System.out.println(user);    }    @Test    public void selectBatchIds(){        // 根据Id列表进行批查询        List idList = Arrays.asList("1" , "2" , "3");        List userList = userMapper.selectBatchIds(idList);        userList.forEach(System.out::println);    }    @Test    public void selectByMap(){        // 根据<属性 , 值>来进行匹配查询 , 多个<属性 , 值>会通过and方式来查询        Map map = new HashMap<>();        // 这里是数据库的列名 , 而不是实体类的属性名        map.put("username" , "kaven");        map.put("password" , "kaven");        List userList = userMapper.selectByMap(map);        userList.forEach(System.out::println);    }}

运行结果:

再演示更新方法。

package com.kaven.mybatisplus.dao;import com.kaven.mybatisplus.entity.User;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class UserMapperUpdateTest {    @Autowired    private UserMapper userMapper;    @Test    public void updateById(){        // 根据Id进行更新        User user = userMapper.selectById("1");        user.setPassword("itkaven");        int rows = userMapper.updateById(user);        System.out.println(userMapper.selectById(user.getId()));    }}

其实还有一个update方法,但它需要一个条件,条件也可以设置为null,但这样会更新所有的数据,这里先不演示,之后的博客再进行演示说明,两个更新方法的定义如下。

/**     * 根据 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 updateWrapper);

再演示几个删除方法

package com.kaven.mybatisplus.dao;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.transaction.annotation.Transactional;import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.util.Map;@RunWith(SpringRunner.class)@SpringBootTestpublic class UserMapperDeleteTest {    @Autowired    private UserMapper userMapper;    @Test    @Transactional    public void deleteById(){        // 根据Id进行删除        int rows = userMapper.deleteById("1");        Assert.assertEquals(rows , 1);    }    @Test    @Transactional    public void deleteByMap(){        // 根据<属性 , 值>进行匹配删除        Map map = new HashMap<>();        map.put("username" , "607");        map.put("password" , "607");        int rows = userMapper.deleteByMap(map);        Assert.assertEquals(rows , 1);    }    @Test    @Transactional    public void deleteBatchIds(){        // 根据Id列表进行批删除        List idList = Arrays.asList("1" , "2" , "3");        int rows = userMapper.deleteBatchIds(idList);        Assert.assertEquals(rows , 3);    }}

结果如下:

这里也还有一个delete方法,也需要一个条件,所以也不进行演示了。

再演示插入方法

package com.kaven.mybatisplus.dao;import com.kaven.mybatisplus.entity.User;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class UserMapperInsertTest {    @Autowired    private UserMapper userMapper;    @Test    public void insert(){        // 直接实体插入        User user = new User();        user.setId("4");        user.setUsername("hn");        user.setPassword("hn");        userMapper.insert(user);    }}

结果如下:

以上是"mybatis-plus如何实现查询传入参数Map,返回List方式"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

查询 数据 演示 方法 条件 实体 数据库 更新 参数 方式 对象 属性 接口 结果 内容 就是 篇文章 项目 配置 不够 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 阿里云服务器价格配置 什么是域名服务器名和ip地址 女生做网络安全销售好做吗 服务器sit测试案例 鸿蒙os软件开发 广州停车系统软件开发怎么样 阴阳师转服务器需要多少钱 嘉定区网络技术服务承诺守信 两年软件开发经验薪资待遇 怎么改变刺激战场服务器 手机网络安全最好的 天龙八部所有服务器开区时间 为什么叶子运用网络技术 国外服务器免费ip地址游戏 苏州得物网络技术有限公司 手机浏览器服务器超时 甘肃智慧养老软件开发电话 攀枝花学院网络安全学院 数据库软件学习视频 漳平手机软件开发定制 智能控制招聘软件开发工程师 数据库报错20595解决方法 数据库文件只有一个怎么还原 参与华为高斯数据库的企业 网络安全考试题多选 前端怎么实行网络安全 软件开发最难的点是什么 怎么租用云服务器并使用 山西浩炜网络技术有限公司 数据库属于什么类型的信息源
0