mybatics 之<foreach></foreach>

                       mybatics 之<foreach></foreach>     
通俗的讲<foreach>标签就是用来遍历list,map集合的,常用于sql语句的in关键词后。
来看看它的参数都有什么
   iteam:表示每个元素在遍历时候的别名。
   index:表示遍历时候的位置。
   collection:表示要遍历的集合。
   open:表示以什么开始。
   separator:表示遍历以什么符号。
   close:表示以什么结束。
注意:collection的值通常有三种情况,
(1)传入参数是一个list集合时候,collection="list"。
(2)传入参数是一个array数组时候,collection="array"。
(3)传入参数是一个map的时候,collection的值为map的key值。

来看一个例子:这是一个根据分类(分类有很多现在用list存)查询出商品的语句:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.tortuousroad.groupon.deal.entity.DealCategoryMapper">
   <resultMap id="BaseResultMap" type="com.tortuousroad.groupon.deal.entity.DealCategory">
      <id column="id" property="id" jdbcType="BIGINT" />
      <result column="parent_id" property="parentId" jdbcType="BIGINT" />
      <result column="name" property="name" jdbcType="VARCHAR" />
      <result column="url_name" property="urlName" jdbcType="VARCHAR" />
      <result column="publish_status" property="publishStatus" jdbcType="INTEGER" />
      <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
      <result column="order_num" property="orderNum" jdbcType="INTEGER" />
      <result column="deep" property="deep" jdbcType="INTEGER" />
   </resultMap>
   <sql id="Base_Column_List">
      id, parent_id, name, url_name, publish_status, create_time, order_num, deep
   </sql>
   <select id="selectDealsForIndex" resultMap="BaseResultMap" parameterType="java.util.Map">
      select
      <include refid="Base_Column_List" />
      from deal
      where publish_status = #{publishStatus,jdbcType=INTEGER}
      and end_time >= #{nowTime,jdbcType=TIMESTAMP}
      and area_id = #{areaId, jdbcType=VARCHAR}
      <if test="categoryIds != null">
         and category_id in
         <foreach item="item" index="index" collection="categoryIds" open="(" separator="," close=")">
         #{item}
         </foreach>
      </if>
      order by deal_level desc
   </select>
</mapper>

Dao层
package com.tortuousroad.groupon.deal.dao;

import com.tortuousroad.framework.common.page.PagingResult;
import com.tortuousroad.framework.common.persistence.BaseMybatisDAO;
import com.tortuousroad.framework.common.search.Search;
import com.tortuousroad.framework.util.DateUtil;
import com.tortuousroad.groupon.deal.constant.DealConstant;
import com.tortuousroad.groupon.deal.entity.Deal;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Deal DAO
 */
@Repository
public class DealDAO extends BaseMybatisDAO {

    private final String MAPPER_NAMESPACE = "com.tortuousroad.groupon.deal.entity.DealMapper";


    /*********************************网站**********************************/


    //FIXME 只查8个就够了,如何实现?
    public List<Deal> getDealsForIndex(Long areaId, List<Long> categoryIds, int publishStatus) {
        Map<String, Object> params = new HashMap<>();
        params.put("categoryIds", categoryIds);//分类id
        params.put("areaId", areaId);//地区
        params.put("nowTime", new Date());//当前时间
        params.put("publishStatus", publishStatus);//发布状态
        return findAll(MAPPER_NAMESPACE + ".selectDealsForIndex", params);
    }

}

package com.tortuousroad.framework.common.persistence;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import com.tortuousroad.framework.base.entity.BaseEntity;
import com.tortuousroad.framework.common.page.PagingResult;
import com.tortuousroad.framework.common.search.Condition;
import com.tortuousroad.framework.common.search.Search;

/**
 * Mybatis基类
 */
public class BaseMybatisDAO {
	
	@Autowired private SqlSessionTemplate template;
	
	/**
	 * 查询指定SQL语句的所有记录
	 * @param sqlId	SQL语句ID
	 * @return	查询到的结果集合
	 */
	public <T extends BaseEntity> List<T> findAll(String sqlId) {
		return template.selectList(sqlId);
	}
	
	/**
	 * 查询指定SQL语句的所有记录
	 * @param sqlId	SQL语句ID
	 * @param params	条件参数
	 * @return	查询到的结果集合
	 */
	public <T extends BaseEntity> List<T> findAll(String sqlId, Map<String, Object> params) {
		return template.selectList(sqlId, params);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

醒悟wjn

打赏可获取源码

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值