多平台统一管理软件接口,如何实现多平台统一管理软件接口
309
2022-10-08
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.主配置文件
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
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 = agemDlobMgQGz;
}
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
public Integer[] getIds() {
return ids;
}
public void setIds(Integer[] ids) {
this.ids = ids;
}
public List
return idList;
}
public void setIdList(List
this.idList = idList;
}
}
二、遍历数组和集合的映射文件和对应的接口
1.mapper映射文件
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from t_user where uid in
#{item}
select * from t_user where uid in
#{item}
select * fmDlobMgQGzrom t_user where uid in
#{item}
select * from t_user where uid in
#{item}
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
//用数组封装条件
public List
//用包装类中的数组封装条件
public List
//用包装类中的list封装条件
public List
}
三、测试代码
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
idhttp://List.add(5);
idList.add(3);
idList.add(123);
idList.add(19);
UserVo userVo = new UserVo();
userVo.setIdList(idList);
List
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
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
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.add(5);
list.add(3);
list.add(123);
list.add(19);
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的情况一样
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~