mybatis 返回list对象集合

user类

public class User implements  Serializable{
	
	private static final long serialVersionUID = 1L;

	private int  activeid;
	private String openid;
	private String name;
	private int sex;
	private String telphone;
	private String image_url;
	private int audit;
    ......
    set get

	
}

 返回就用resultMap,并且 自定义resultMap  

resultMap 里面的result 对应的要放回类的属性

 

	<resultMap type="org.ice.entity.User" id="users">
		<result column="openid" property="openid"/>
		<result column="sex" property="sex"/>
		<result column="telphone" property="telphone"/>
		<result column="image_url" property="image_url"/>
		<result column="audit" property="audit"/>
	</resultMap>	

   <select id="findAll" parameterType="map" resultMap="users" >
		select * from ice_user where 
		<if test="activeid !=null">
			activeid = #{activeid}
		</if>
		<if test="openid !=null">
			AND openid = #{openid}
		</if>
		<if test="audit !=null">
			AND audit = #{audit}
		</if>
	</select>

 

dao 层

 /**
   * 按条件查找users
   * @param where
   * @return
   */
  public List<User> findAll(Map<String, Object> where);
  

返回时list 对象。

 

 

如果dao是返回list,而mapp.xml 不是自定义resultMap 那么报错

 

 

转载于:https://my.oschina.net/jywm/blog/780028

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你的问题是如何在 MyBatis返回一个对象,该对象中包含一个 List 集合,请确认我的理解是否正确。如果是这样的话,你可以在 MyBatis 的映射文件中使用一个 resultMap 来实现这个需求。下面是一个示例: 假设你有两个类,一个是 Order 类,另一个是 OrderItem 类。Order 类中包含了一个 List<OrderItem> 属性,你希望在使用 MyBatis 查询数据时,可以返回一个 Order 对象,该对象中包含了一组 OrderItem 对象。 首先,你需要在映射文件中定义一个 resultMap,如下所示: ```xml <resultMap id="orderResultMap" type="Order"> <id column="id" property="id"/> <result column="order_no" property="orderNo"/> <result column="total_price" property="totalPrice"/> <collection property="orderItems" ofType="OrderItem"> <id column="item_id" property="itemId"/> <result column="item_name" property="itemName"/> <result column="item_price" property="itemPrice"/> </collection> </resultMap> ``` 在这个 resultMap 中,我们定义了一个 collection,它的 property 属性指定了 Order 类中的 List<OrderItem> 属性的名称,ofType 属性指定了集合中元素的类型。接下来,我们可以在查询语句中使用这个 resultMap,如下所示: ```xml <select id="getOrder" resultMap="orderResultMap"> SELECT id, order_no, total_price, item_id, item_name, item_price FROM orders JOIN order_items ON orders.id = order_items.order_id WHERE orders.id = #{orderId} </select> ``` 在这个查询语句中,我们使用了上面定义的 orderResultMap,查询结果会被映射成一个 Order 对象,其中的 orderItems 属性就是一个 List<OrderItem> 集合。注意,我们需要在查询语句中使用 JOIN 操作,将 orders 和 order_items 两张表进行连接,以便获取所有的数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值