Mybatis使用map参数查询
Mybatis使用map参数查询,本文介绍两种方式:
以Map为参数直接查询。
以分页对象中的Map作为参数查询。
- 第一种: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参数查询相关推荐
- mybatis处理map参数取值
mybatis处理map参数取值 <select id="selectOne" parameterType="java.util.HashMap" res ...
- IDEA第一个mybatis程序 mybatis增删查改操作 mybatis的map模糊查询
连接数据库: 端口号: 打开IDEA: 注意这个URL:jdbc:mysql://localhost:3308 对应mybatis核心配置的url: 新建IDEA的MAVEN项目 目录: 数据库: 打 ...
- mybatis传入map参数
初学mybatis产生两个问题: mybatis传入参数执行SQL语句好像并不是很智能?xml文件中指定parameterType只能是一种数据类型.假设我现在需要执行一条SQL语句,里面有两个参数: ...
- mybatis传入map参数parameterType
基本数据类型:包含int,String,Date等.基本数据类型作为传参,只能传入一个.通过#{参数名} 即可获取传入的值 复杂数据类型:包含JAVA实体类.Map.通过#{属性名}或#{map的Ke ...
- MyBatis批量操作和多参数查询
批量操作 1.批量添加元素session.insert(String string,Object o) [plain] view plaincopy public void batchInsertSt ...
- Mybatis的Mapper方法中传递map参数的写法
Mybatis的Mapper方法中传递map参数的写法 当我们在进行数据分页查询时,如果想让Mybatis的分页查询返回Map对象的集合,我们需要在查询的方法上使用单个参数或者使用Map对象作为参数进 ...
- mybatis按datetime条件查询,参数为时间戳时
mybatis按datetime条件查询,参数为时间戳时,如果数据库为2018-1-1 20:22:10, 你的时间戳也为2018-1-1 20:22:10,但却没找到数据.可能是时差导致的.百度修正 ...
- Mybatis中如何传入map参数呢?
转自: Mybatis中如何传入map参数呢? 下文笔者讲述Mybatis的xml中使用map参数的方法分享,如下所示 实现思路:1.将参数类型设置为map2.在xml中直接使用map的键值获取参数即 ...
- MyBatis之传入参数——parameterType
在MyBatis的select.insert.update.delete这些元素中都提到了parameterType这个属性.MyBatis现在可以使用的parameterType有基本数据类型和JA ...
- MyBatis之传入参数
在MyBatis的select.insert.update.delete这些元素中都提到了parameterType这个属性.MyBatis现在可以使用的parameterType有基本数据类型和Ja ...
最新文章
- Oracle数据库对象,同义词、序列、视图、索引
- 亚马逊:区块链模块化,造币流水线化
- 谁来护航储能电池安全?
- MySQL查看SQL语句执行效率和mysql几种性能测试的工具
- mac下pip install 安装只能选择python2.7的问题
- 软件测试方法的分类细谈
- unittest----assert断言的使用
- noip2009 普及组
- JavaScript模拟终端输出
- Everything搜索_使用方法
- Ubuntu中超级终端-minicom
- 6163. 给定条件下构造矩阵——每日一难(phase2_day1)
- 查询数据库各种历史记录
- 抖音上热门的好处有哪些?
- 内网外网服务器时间同步解决方案
- layui数据表格与后台交互进行渲染
- 计算机色温调整,如何调节计算机屏幕的色温
- 作用域和自由变量的介绍
- Linux 日志查看 | cat 命令
- 底层之旅——Android显示驱动(framebuffer)的分析