Mybatis使用map参数查询

在这里插入图片描述

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">
		  	SELECT
			customer_id AS customerId
			FROM
			main_order 
			WHERE 1=1
		<if test="begTime != null and begTime != '' and endgTime != null and endgTime != ''">
			and unix_timestamp(CHARGE_END_TIME) between
			unix_timestamp(#{begoTime}) and
			unix_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" />
		FROM
		membershipcardinfo
		WHERE 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) between
			unix_timestamp('${page.paramMap.begTime}') and
			unix_timestamp('${page.paramMap.endTime}')
		</if>
	</select>

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

结束,欢迎反馈。

### 回答1: Mybatis是一种流行的Java ORM框架,它提供了几种查询结果返回类型,其中之一就是MapMybatis查询返回Map的方式可以通过在SQL映射文件中使用resultType属性指定为Map来实现。通常,我们可以使用类似于以下示例代码的方式: <select id="selectUser" resultType="map"> select * from user where id=#{id} </select> 当我们执行此查询时,Mybatis将返回一个包含查询结果数据的Map对象。在这个Map对象中,键是列名,值是对应的列的值。 我们可以通过以下代码将查询结果转换为Map对象: Map<String, Object> result = sqlSession.selectOne("selectUser", 1); 在这个例子中,我们将查询结果转化为一个包含键和值的Map对象。这个Map对象的键是每个列的名称,值是对应的行数据。这样,我们就可以通过这个Map对象来实现数据的操作。 总的来说,Mybatis查询返回Map是一种非常方便的方式,可以通过映射文件中的简单指定来实现。然后,我们可以通过处理这些Map对象来实现复杂的数据操作。 ### 回答2: Mybatis查询返回`Map`是一种常见的数据返回方式,它将查询结果以`Map`的形式返回,以便于快速访问和操作。在使用Mybatis进行查询时,我们可以通过以下步骤返回`Map`对象: 1.在Mapper.xml中编写SQL语句,并选取返回值类型为`Map` ``` <select id="selectUser" parameterType="int" resultType="java.util.Map"> select * from user where id = #{id} </select> ``` 2.在Java代码中执行查询,并获取返回的Map对象 ``` SqlSession sqlSession = sqlSessionFactory.openSession(); Map<String, Object> userMap = sqlSession.selectOne("selectUser", 1); ``` 在返回的`Map`中,键值对的形式为列名和对应的值,我们可以根据需要获取相应的值。例如,获取用户的名字和年龄可以这样写: ``` String name = (String)userMap.get("name"); int age = (int)userMap.get("age"); ``` 需要注意的是,当查询结果含有多条记录时,返回的`Map`对象只包含第一条记录的数据。此时需要使用`selectList`方法返回一个`List<Map>`来获取所有记录的数据。同时,在查询结果中,如果存在名字相同的列,Mybatis会自动将后面的列值覆盖前面的列值,因此建议在查询时尽量避免列名重复。 总而言之,Mybatis查询返回`Map`是一种方便快捷的数据返回方式,在需要快速获取数据时特别适用。但需要根据实际情况来判断是否适合使用,并注意使用过程中的一些细节。 ### 回答3: MyBatis 是一个流行的 ORM(对象关系映射)框架,用于简化数据库访问。MyBatis 查询返回 Map 是一种常见的查询结果格式,其主要优势在于可以根据查询结果的键名来访问对应的数据值。 在 MyBatis 中,我们可以使用 @MapKey 注解将查询结果转换为 Map。这个注解可以用于指定 Map 的键名,例如: ```java @MapKey("id") List<Map<String, Object>> selectUsersById(List<Integer> ids); ``` 这个示例中,我们使用 @MapKey 注解将查询结果转换为一个 Map,其中键名为 id,值为对应的 Map 对象。这样,我们就可以通过键名 id 来快速访问对应的用户信息。 当然,我们也可以使用自定义的键名。例如: ```java @MapKey("userName") List<Map<String, Object>> selectAllUsers(); ``` 这个示例中,我们将查询结果转换为一个 Map,其中键名为 userName,值为对应的 Map 对象。这样,我们就可以根据用户名来快速访问对应的用户信息。 除了使用 @MapKey 注解,我们还可以使用 ResultMap 自定义查询结果映射。例如: ```xml <resultMap id="userMap" type="java.util.HashMap"> <id column="id" property="id"/> <result column="user_name" property="userName"/> <result column="email" property="email"/> </resultMap> <select id="selectAllUsers" resultMap="userMap"> select id, user_name, email from users </select> ``` 这个示例中,我们定义了一个 resultMap,将查询结果映射为一个 HashMap。通过 column 和 property 属性可以指定键名和值的映射关系。 总的来说,MyBatis 查询返回 Map 是一种非常实用的查询结果格式,可以方便快捷地访问查询结果。同时,我们也可以通过 @MapKey 和 ResultMap 来自定义键名和值的映射关系,使查询结果更加灵活。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值