Mybatis使用map参数查询,本文介绍两种方式:

以Map为参数直接查询。

以分页对象中的Map作为参数查询。

  1. 第一种:Map为参数直接查询

查询参数 :

 Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("begTime", begTime);paramMap.put("endTime", endTime);paramMap.put("goodsname", allgoodsname);//查询方法
List<String> idList = mainMapper.selectMemberCard(paramMap);

Mapper层:

/*** 根据时间和商品型号查询会员id集合* * @param paramMap* @return*/List<String> selectMemberCard(Map<String, Object> paramMap);

Mapper.xml

<select id="selectMemberCard" parameterType="Map" resultType="String">SELECTcustomer_id AS customerIdFROMmain_order WHERE 1=1<if test="begTime != null and begTime != '' and endgTime != null and endgTime != ''">and unix_timestamp(CHARGE_END_TIME) betweenunix_timestamp(#{begoTime}) andunix_timestamp(#{endgoTime})</if><if test="goodsname != null and goodsname !='' ">and goodsname like CONCAT('%','${goodsname}','%')</if></select>

由于Mapper层没有使用@Param注解,所以在XMl直接去map里的参数名称即可取到值

2. 第二种 分页对象中的Map作为参数查询:

查询参数 (所有的查询参数都放到分页对象的Map集合中) :

Map<String, Object> paramMap = new HashMap<String, Object>(); paramMap.put("begTime", begTime);paramMap.put("endTime", endTime);//分页查询会员信息Page<MembershipCardInfo> pages = new Page<MembershipCardInfo>();pages.setLimit(exportTemplateService.getMaxExportCount());pages.setOffset(0);pages.setParamMap(paramMap);List<MembershipCardInfo> cardList = cardInfoMapper.selectcardListByValue(pages);

Pages分页实体对象

public class Page<T> implements Serializable {private static final long serialVersionUID = -3323321457300243220L;/** 总记录数 */private long total;/** 当前页对应的记录列表 */private List<T> rows;/** 分页查询条件对应的参数Map */private Map<String, Object> paramMap;/** 排序字符串 */private String orderBy;/**一次取多少条**/private int limit;/**从第几条开始去**/private int offset;

Mapper层:

/*** * 根据会员开卡时间查询会员卡信息* @param page* @return*/List<MembershipCardInfo> selectExcelListByValue(@Param("page") Page<MembershipCardInfo> page);

Mapper.xml

<select id="selectExcelListByValue" parameterType="map" resultMap="BaseResultMap">SELECT<include refid="Base_Column_List" />FROMmembershipcardinfoWHERE 1 = 1 <if test="page.paramMap.begTime != null and page.paramMap.begTime != '' and page.paramMap.endTime != null and page.paramMap.endTime != ''">and unix_timestamp(CREATE_TIME) betweenunix_timestamp('${page.paramMap.begTime}') andunix_timestamp('${page.paramMap.endTime}')</if></select>

由于使用@Param注解,所以接收参数时先要找到注解的参数名page,然后找到page对象里的map集合,然后取集合中的某个参数。即page.paramMap.begTime

结束,欢迎反馈。

Mybatis使用map参数查询相关推荐

  1. mybatis处理map参数取值

    mybatis处理map参数取值 <select id="selectOne" parameterType="java.util.HashMap" res ...

  2. IDEA第一个mybatis程序 mybatis增删查改操作 mybatis的map模糊查询

    连接数据库: 端口号: 打开IDEA: 注意这个URL:jdbc:mysql://localhost:3308 对应mybatis核心配置的url: 新建IDEA的MAVEN项目 目录: 数据库: 打 ...

  3. mybatis传入map参数

    初学mybatis产生两个问题: mybatis传入参数执行SQL语句好像并不是很智能?xml文件中指定parameterType只能是一种数据类型.假设我现在需要执行一条SQL语句,里面有两个参数: ...

  4. mybatis传入map参数parameterType

    基本数据类型:包含int,String,Date等.基本数据类型作为传参,只能传入一个.通过#{参数名} 即可获取传入的值 复杂数据类型:包含JAVA实体类.Map.通过#{属性名}或#{map的Ke ...

  5. MyBatis批量操作和多参数查询

    批量操作 1.批量添加元素session.insert(String string,Object o) [plain] view plaincopy public void batchInsertSt ...

  6. Mybatis的Mapper方法中传递map参数的写法

    Mybatis的Mapper方法中传递map参数的写法 当我们在进行数据分页查询时,如果想让Mybatis的分页查询返回Map对象的集合,我们需要在查询的方法上使用单个参数或者使用Map对象作为参数进 ...

  7. mybatis按datetime条件查询,参数为时间戳时

    mybatis按datetime条件查询,参数为时间戳时,如果数据库为2018-1-1 20:22:10, 你的时间戳也为2018-1-1 20:22:10,但却没找到数据.可能是时差导致的.百度修正 ...

  8. Mybatis中如何传入map参数呢?

    转自: Mybatis中如何传入map参数呢? 下文笔者讲述Mybatis的xml中使用map参数的方法分享,如下所示 实现思路:1.将参数类型设置为map2.在xml中直接使用map的键值获取参数即 ...

  9. MyBatis之传入参数——parameterType

    在MyBatis的select.insert.update.delete这些元素中都提到了parameterType这个属性.MyBatis现在可以使用的parameterType有基本数据类型和JA ...

  10. MyBatis之传入参数

    在MyBatis的select.insert.update.delete这些元素中都提到了parameterType这个属性.MyBatis现在可以使用的parameterType有基本数据类型和Ja ...

最新文章

  1. Oracle数据库对象,同义词、序列、视图、索引
  2. 亚马逊:区块链模块化,造币流水线化
  3. 谁来护航储能电池安全?
  4. MySQL查看SQL语句执行效率和mysql几种性能测试的工具
  5. mac下pip install 安装只能选择python2.7的问题
  6. 软件测试方法的分类细谈
  7. unittest----assert断言的使用
  8. noip2009 普及组
  9. JavaScript模拟终端输出
  10. Everything搜索_使用方法
  11. Ubuntu中超级终端-minicom
  12. 6163. 给定条件下构造矩阵——每日一难(phase2_day1)
  13. 查询数据库各种历史记录
  14. 抖音上热门的好处有哪些?
  15. 内网外网服务器时间同步解决方案
  16. layui数据表格与后台交互进行渲染
  17. 计算机色温调整,如何调节计算机屏幕的色温
  18. 作用域和自由变量的介绍
  19. Linux 日志查看 | cat 命令
  20. 底层之旅——Android显示驱动(framebuffer)的分析

热门文章

  1. 如何创建一个微信小程序
  2. 学习3D建模电脑配置要求
  3. 「MRC干货」如何快速构建一台高精度AGV
  4. 微信小程序:音乐播放器(附源码)
  5. 404 not found是什么意思
  6. html div 字体向左自动,在css中怎样设置字体靠左?
  7. XPS文件怎么转成Word呢
  8. Stream之Collectors(toList()
  9. 如何测身高未来身高计算机,父母身高测孩子身高公式
  10. 【BP回归预测】基于matlab改进的鲸鱼算法优化BP神经网络回归预测(多输入单输出)【含Matlab源码 2184期】