mybatis使用foreach遍历list集合或者array数组方式

一、准备工作

1.db.properties文件(记得修改自己的数据库和用户名、密码)

dataSource.driver=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8
dataSource.username=blog
dataSource.password=blog

2.主配置文件





	
	
	
		
		
	 	
	 	
	 

  
  
    
    
      
      
      
        
        
        
        
      
    
  
  
  
  
   
    
    
    
    
    
   
    
  

3.创建User类和包装类UserVo

User.java

package com.mybatis_demo.domain;
public class User {
	private Integer uid;
	private String uname;
	private Integer age;
	private String address;
	public Integer getUid() {
		return uid;
	}
	public void setUid(Integer uid) {
		this.uid = uid;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	@Override
	public String toString() {
		return "User [uid=" + uid + ", uname=" + uname + ", age=" + age + ", address=" + address + "]";
	}
}

UserVo.java

package com.mybatis_demo.domain;
import java.util.List;
public class UserVo extends User {
	private Integer[] ids;
	private List idList;
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public List getIdList() {
		return idList;
	}
	public void setIdList(List idList) {
		this.idList = idList;
	}
	
}

二、遍历数组和集合的映射文件和对应的接口

1.mapper映射文件













2.mapper接口

UserMapper.interface

package com.mybatis_demo.mapper;
import java.util.List;
import java.util.Map;
import com.mybatis_demo.domain.User;
import com.mybatis_demo.domain.UserVo;
public interface UserMapper {
	//mybatis使用mapper动态代理
	
	//4大原则,一个注意
	//1.接口中的方法名需要与对应mapper.xml的id一致
	//2.接口中的返回值需要与对应mapper.xml的返回值类型保持一致
	//3.接口中的参数需要与对应mapper.xml的参数类型、个数、参数名保持一致
	//4.对应mapper.xml的名字空间需要修改成对应接口的全包名
	//注意:mapper动态代理根据返回值类型,mybatis会自动选择调用selectone还是selectlist....
	//用list封装条件
	public List selectByList(List testlist);
	//用数组封装条件
	public List selectByArray(Integer[] ids);
	//用包装类中的数组封装条件
	public List selectUserVoByArray(UserVo userVo);
	//用包装类中的list封装条件
	public List selectUserVoByList(UserVo userVo);	
}

三、测试代码

package com.mybatis_demo.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
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.mybatis_demo.domain.User;
import com.mybatis_demo.domain.UserVo;
import com.mybatis_demo.mapper.UserMapper;
public class TestMapper {
	
	//用包装类中的list封装条件,传递参数是一个包装类
	@Test
	public void test_selectUserVoByList() {
		try {
			 //读取配置文件
			InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
			//创建SqlSessionFactoryBuilder对象,用来获取SqlSessionFactory对象
			SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
			//利用SqlSessionFactoryBuilder对象build一个SqlSessionFactory对象
			SqlSessionFactory build = builder.build(in);
			//利用sqlSessionFactory获取session对象
			SqlSession session = build.openSession();
			//通过session对象获取对应mapper接口
			UserMapper mapper = session.getMapper(UserMapper.class);
			List idList = new ArrayList();
			idList.add(5);
			idList.add(3);
			idList.add(123);
			idList.add(19);
			UserVo userVo = new UserVo();
			userVo.setIdList(idList);
			List users = mapper.selectUserVoByList(userVo);
			for (User user : users) {
				System.out.println(user);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//用包装类中的array封装条件,传递参数是一个包装类
	@Test
	public void test_selectUserVoByArray() {
		try {
			 //读取配置文件
			InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
			//创建SqlSessionFactoryBuilder对象,用来获取SqlSessionFactory对象
			SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
			//利用SqlSessionFactoryBuilder对象build一个SqlSessionFactory对象
			SqlSessionFactory build = builder.build(in);
			//利用sqlSessionFactory获取session对象
			SqlSession session = build.openSession();
			//通过session对象获取对应mapper接口
			UserMapper mapper = session.getMapper(UserMapper.class);
			Integer[] ids = new Integer[]{5,9,30};
			UserVo userVo = new UserVo();
			userVo.setIds(ids);
			List users = mapper.selectUserVoByArray(userVo);
			for (User user : users) {
				System.out.println(user);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//用数组封装条件,传递参数是一个数组
	@Test
	public void test_selectByArray() {
		try {
			 //读取配置文件
			InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
			//创建SqlSessionFactoryBuilder对象,用来获取SqlSessionFactory对象
			SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
			//利用SqlSessionFactoryBuilder对象build一个SqlSessionFactory对象
			SqlSessionFactory build = builder.build(in);
			//利用sqlSessionFactory获取session对象
			SqlSession session = build.openSession();
			//通过session对象获取对应mapper接口
			UserMapper mapper = session.getMapper(UserMapper.class);
			Integer[] ids = new Integer[]{5,9,30};
			List users = mapper.selectByArray(ids);
			for (User user : users) {
				System.out.println(user);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//用list封装条件,传递参数是一个list集合
	@Test
	public void test_selectByList() {
		try {
			 //读取配置文件
			InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
			//创建SqlSessionFactoryBuilder对象,用来获取SqlSessionFactory对象
			SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
			//利用SqlSessionFactoryBuilder对象build一个SqlSessionFactory对象
			SqlSessionFactory build = builder.build(in);
			//利用sqlSessionFactory获取session对象
			SqlSession session = build.openSession();
			//通过session对象获取对应mapper接口
			UserMapper mapper = session.getMapper(UserMapper.class);
			List list = new ArrayList();
			list.add(5);
			list.add(3);
			list.add(123);
			list.add(19);
			List users = mapper.selectByList(list);
			for (User user : users) {
				System.out.println(user);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

四、总结

1.如果你传参的时候直接传一个数组,那么使用foreach遍历时collection=“array”,这里是固定写法,即这里的array与你的实参名无关

2.如果你传参的时候直接传一list集合,那么使用foreach遍历时collection=“list”,这里是固定写法,即这里的list与你的实参名无关

3.如果你传参的时候直接传一个含有数组成员变量的类,那么使用foreach遍历时collection=“你的变量名”,这里不再是固定写法,即这里的命名取决于成员变量的变量名,例如:成员变量名是test,那么就是collection=“test”

4.如果你传参的时候直接传一个含有list集合成员变量的类,跟3的情况一样

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的:(mybatis使用foreach遍历list集合或者array数组方式)