mybatis动态sql之foreach补充

Posted 西西嘛呦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis动态sql之foreach补充相关的知识,希望对你有一定的参考价值。

    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=dema@qq.com, dept=null]
Employee [id=2, lastName=jack, gender=1, email=675544321@qq.com, 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=dema@qq.com, dept=null]
Employee [id=2, lastName=jack, gender=1, email=675544321@qq.com, dept=null]

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

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

以上是关于mybatis动态sql之foreach补充的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis参数传入集合之foreach动态sql

Mybatis -- 动态Sql概述动态Sql之<if>(包含<where>)动态Sql之<foreach>sql片段抽取

MyBatis参数传入集合之foreach动态sql

mybatis动态sql之foreach标签

Mybatis_9动态sql之foreach

mybatis动态sql之使用foreach进行批量插入的两种方式