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层:
1 /**
2 * 根据时间和商品型号查询会员id集合
3 *
4 * @param paramMap
5 * @return
6 */
7 List<String> selectMemberCard(Map<String, Object> paramMap);
Mapper.xml:
1 <select id="selectMemberCard" parameterType="Map" resultType="String">
2 SELECT
3 customer_id AS customerId
4 FROM
5 main_order
6 WHERE 1=1
7 <if test="begTime != null and begTime != '' and endgTime != null and endgTime != ''">
8 and unix_timestamp(CHARGE_END_TIME) between
9 unix_timestamp(#{begoTime}) and
10 unix_timestamp(#{endgoTime})
11 </if>
12 <if test="goodsname != null and goodsname !='' ">
13 and goodsname like CONCAT('%','${goodsname}','%')
14 </if>
15 </select>
由于Mapper层没有使用@Param注解,所以在XMl直接去map里的参数名称即可取到值
第二种 分页对象中的Map作为参数查询:
查询参数 (所有的查询参数都放到分页对象的Map集合中) :
1 Map<String, Object> paramMap = new HashMap<String, Object>();
2 paramMap.put("begTime", begTime);
3 paramMap.put("endTime", endTime);
4
5 //分页查询会员信息
6 Page<MembershipCardInfo> pages = new Page<MembershipCardInfo>();
7 pages.setLimit(exportTemplateService.getMaxExportCount());
8 pages.setOffset(0);
9 pages.setParamMap(paramMap);
10
11 List<MembershipCardInfo> cardList = cardInfoMapper.selectcardListByValue(pages);
Pages分页实体对象:
1 public class Page<T> implements Serializable {
2
3 private static final long serialVersionUID = -3323321457300243220L;
4 /** 总记录数 */
5 private long total;
6
7 /** 当前页对应的记录列表 */
8 private List<T> rows;
9
10 /** 分页查询条件对应的参数Map */
11 private Map<String, Object> paramMap;
12
13 /** 排序字符串 */
14 private String orderBy;
15 /**一次取多少条**/
16 private int limit;
17 /**从第几条开始去**/
18 private int offset;
Mapper层:
1 /**
2 *
3 * 根据会员开卡时间查询会员卡信息
4 * @param page
5 * @return
6 */
7 List<MembershipCardInfo> selectExcelListByValue(
8 @Param("page") Page<MembershipCardInfo> page);
Mapper.xml:
1 <select id="selectExcelListByValue" parameterType="map" resultMap="BaseResultMap">
2 SELECT
3 <include refid="Base_Column_List" />
4 FROM
5 membershipcardinfo
6 WHERE 1 = 1
7 <if test="page.paramMap.begTime != null and page.paramMap.begTime != '' and page.paramMap.endTime != null and page.paramMap.endTime != ''">
8 and unix_timestamp(CREATE_TIME) between
9 unix_timestamp('${page.paramMap.begTime}') and
10 unix_timestamp('${page.paramMap.endTime}')
11 </if>
12 </select>
由于使用@Param注解,所以接收参数时先要找到注解的参数名page,然后找到page对象里的map集合,然后取集合中的某个参数。即page.paramMap.begTime
以上借鉴于:https://blog.csdn.net/Forever_Devil/article/details/107516626
详情借鉴请点击以上链接查看

浙公网安备 33010602011771号