mybatis动态sql之foreach补充(三)

时间:2021-07-16 01:44:22

    public List<Employee> getEmpByForeach4(@Param("map") Map<String,Object> map);

如果传入的是Map,

    <select id="getEmpByForeach4" resultType="com.gong.mybatis.bean.Employee">
        select * from tbl_employee where id in
        <foreach collection="map" index="index" item="item" separator="," open="(" close=")">
            #{index}
        </foreach>
    </select>

这里需要注意index属性和item属性。index属性里的值是Map的键,item的值是Map的值。

如果我们在foreach中使用#{index}:

进行测试:

package com.gong.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.gong.mybatis.bean.Employee;
import com.gong.mybatis.mapper.EmployeeMapperDynamicSql;

public class TestMybatis3 {
    
    public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream is = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(is);
    }

    @Test
    public void test() throws IOException {
        SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
        SqlSession openSession = sqlSessionFactory.openSession();
        try {
            EmployeeMapperDynamicSql mapper = openSession.getMapper(EmployeeMapperDynamicSql.class);
            Map<String,Object> map = new HashMap<String, Object>();
            Employee e1 = new Employee();
            e1.setId(1);
            Employee e2 = new Employee();
            e2.setId(2);
            map.put("1", e1);
            map.put("2", e2);
            List<Employee> es = mapper.getEmpByForeach4(map);
            for(Employee e:es) {
                System.out.println(e);
            }
            openSession.commit();
        } finally {
            openSession.close();
        }
        
    }
    
}

输出:

DEBUG 01-21 16:59:08,912 ==>  Preparing: select * from tbl_employee where id in ( ? , ? )   (BaseJdbcLogger.java:145) 
DEBUG 01-21 16:59:08,966 ==> Parameters: 1(String), 2(String)  (BaseJdbcLogger.java:145) 
DEBUG 01-21 16:59:09,019 <==      Total: 2  (BaseJdbcLogger.java:145) 
Employee [id=1, lastName=dema, gender=1, email[email protected], dept=null]
Employee [id=2, lastName=jack, gender=1, email[email protected], dept=null]

是根据Map中的键进行查询,

如果更改为:

#{item.id}

结果:

DEBUG 01-21 17:00:23,650 ==>  Preparing: select * from tbl_employee where id in ( ? , ? )   (BaseJdbcLogger.java:145) 
DEBUG 01-21 17:00:23,710 ==> Parameters: 1(Integer), 2(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 01-21 17:00:23,762 <==      Total: 2  (BaseJdbcLogger.java:145) 
Employee [id=1, lastName=dema, gender=1, email[email protected], dept=null]
Employee [id=2, lastName=jack, gender=1, email[email protected], dept=null]

根据值进行查询时,需要根据Map值里面存储的数据类型进行相应的操作。此处里面存储的是Employee对象,因此item就代表Employee对象,通过item.id的形式取得里面存储的id的值,再传给foreach进行操作。

foreach传入的参数几种形式就大致如此了。