亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

mybatis使用foreach遍歷list集合或者array數(shù)組方式

 更新時間:2021年07月30日 15:20:01   作者:ACodeBird  
這篇文章主要介紹了mybatis使用foreach遍歷list集合或者array數(shù)組方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

一、準備工作

1.db.properties文件(記得修改自己的數(shù)據(jù)庫和用戶名、密碼)

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

2.主配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--  引入外部配置文件-->
	<properties resource="db.properties"></properties>
	<!-- 別名設置,不設置時引用要使用全包名,設置后可以使用自定義別名,更加簡潔 -->
	<typeAliases>
		<!-- 別名設置有兩種,一種是一個一個設置,另外一種是設置某個包,默認別名為類名(大小寫都可以,建議小寫) -->
		<!-- 第一種設置 
	 	<typeAlias type="com.mybatis_demo.domain.User" alias="user"/>-->
	 	<!-- 第二種設置,整個包下面的類都進行別名設置,推薦第二種 -->
	 	<package name="com.mybatis_demo.domain"/>
	 </typeAliases>
<!-- 環(huán)境模式:development開發(fā)模式 work工作模式 -->
  <environments default="development">
  <!-- 環(huán)境變量 -->
    <environment id="development">
    <!-- 使用jdbc的事務管理 -->
      <transactionManager type="JDBC"/>
      <!-- 使用連接池 -->
      <dataSource type="POOLED">
        <property name="driver" value="${dataSource.driver}"/>
        <property name="url" value="${dataSource.url}"/>
        <property name="username" value="${dataSource.username}"/>
        <property name="password" value="${dataSource.password}"/>
      </dataSource>
    </environment>
  </environments>
  <!-- 引入mapper映射文件 -->
  <mappers>
  <!--  1.相對路徑引入-->
   <!--  <mapper resource="mapper/UserMapper.xml"/> -->
    <!-- 2.絕對路徑引入 -->
    <!-- <mapper url="file:\\\D:\sts-bundle\workplace\mybatis_demo\src\main\resources\mapper\UserMapper.xml"/> -->
    <!-- 3.對應mapper接口全包名引入,需要對應的mapper.xml與接口mapper處于同一包下才可以,且xml文件名與接口名要相同,xml文件中的namespace必須是對應接口的全包名 -->
    <!-- <mapper class="com.mybatis_demo.mapper.UserMapper"/> -->
    <!-- 4.包引入,要求跟接口引入一樣 -->
   <!--  <mapper resource="mapper/UserMapper2.xml"/> -->
    <package name="com.mybatis_demo.mapper"/>
  </mappers>
</configuration>

3.創(chuàng)建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<Integer> idList;
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public List<Integer> getIdList() {
		return idList;
	}
	public void setIdList(List<Integer> idList) {
		this.idList = idList;
	}
	
}

二、遍歷數(shù)組和集合的映射文件和對應的接口

1.mapper映射文件

<?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.mybatis_demo.mapper.UserMapper">
<!-- 遍歷list集合,collection="list",如果你傳參的時候是直接傳遞list集合,那么這里只能填list,不能填參數(shù)名 -->
<select id="selectByList" resultType="User">
	select * from t_user where uid in
	<foreach collection="list" item="item" open="(" separator="," close=")">
		#{item}
	</foreach>
</select>
<!-- 遍歷數(shù)組 ,collection="array",如果你傳參的時候是直接傳遞數(shù)組,那么這里只能填array,不能填參數(shù)名-->
<select id="selectByArray" resultType="User">
	select * from t_user where uid in
	<foreach collection="array" item="item" open="(" separator="," close=")">
		#{item}
	</foreach>
</select>
<!-- 遍歷包裝類中的數(shù)組,collection="ids",這里不再是array,而是包裝類中對應的變量名,因為你傳遞的參數(shù)是一個包裝類,mybatis是通過get方法獲取包裝類中的數(shù)組 -->
<select id="selectUserVoByArray" parameterType="UserVo" resultType="User">
	select * from t_user where uid in
	<foreach collection="ids" item="item" open="(" separator="," close=")">
		#{item}
	</foreach>
</select>
<!-- 遍歷包裝類中的list集合,collection="idList",這里不再是list,而是包裝類中對應的變量名,因為你傳遞的參數(shù)是一個包裝類,mybatis是通過get方法獲取包裝類中的list集合 -->
<select id="selectUserVoByList" parameterType="UserVo" resultType="User">
	select * from t_user where uid in
	<foreach collection="idList" item="item" open="(" separator="," close=")">
		#{item}
	</foreach>
</select>
</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動態(tài)代理
	
	//4大原則,一個注意
	//1.接口中的方法名需要與對應mapper.xml的id一致
	//2.接口中的返回值需要與對應mapper.xml的返回值類型保持一致
	//3.接口中的參數(shù)需要與對應mapper.xml的參數(shù)類型、個數(shù)、參數(shù)名保持一致
	//4.對應mapper.xml的名字空間需要修改成對應接口的全包名
	//注意:mapper動態(tài)代理根據(jù)返回值類型,mybatis會自動選擇調用selectone還是selectlist....
	//用list封裝條件
	public List<User> selectByList(List<Integer> testlist);
	//用數(shù)組封裝條件
	public List<User> selectByArray(Integer[] ids);
	//用包裝類中的數(shù)組封裝條件
	public List<User> selectUserVoByArray(UserVo userVo);
	//用包裝類中的list封裝條件
	public List<User> 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封裝條件,傳遞參數(shù)是一個包裝類
	@Test
	public void test_selectUserVoByList() {
		try {
			 //讀取配置文件
			InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
			//創(chuàng)建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<Integer> idList = new ArrayList<Integer>();
			idList.add(5);
			idList.add(3);
			idList.add(123);
			idList.add(19);
			UserVo userVo = new UserVo();
			userVo.setIdList(idList);
			List<User> users = mapper.selectUserVoByList(userVo);
			for (User user : users) {
				System.out.println(user);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//用包裝類中的array封裝條件,傳遞參數(shù)是一個包裝類
	@Test
	public void test_selectUserVoByArray() {
		try {
			 //讀取配置文件
			InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
			//創(chuàng)建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<User> users = mapper.selectUserVoByArray(userVo);
			for (User user : users) {
				System.out.println(user);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//用數(shù)組封裝條件,傳遞參數(shù)是一個數(shù)組
	@Test
	public void test_selectByArray() {
		try {
			 //讀取配置文件
			InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
			//創(chuàng)建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<User> users = mapper.selectByArray(ids);
			for (User user : users) {
				System.out.println(user);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//用list封裝條件,傳遞參數(shù)是一個list集合
	@Test
	public void test_selectByList() {
		try {
			 //讀取配置文件
			InputStream in = Resources.getResourceAsStream("sqlMapConfig.xml");
			//創(chuàng)建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<Integer> list = new ArrayList<Integer>();
			list.add(5);
			list.add(3);
			list.add(123);
			list.add(19);
			List<User> users = mapper.selectByList(list);
			for (User user : users) {
				System.out.println(user);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

四、總結

1.如果你傳參的時候直接傳一個數(shù)組,那么使用foreach遍歷時collection=“array”,這里是固定寫法,即這里的array與你的實參名無關

2.如果你傳參的時候直接傳一list集合,那么使用foreach遍歷時collection=“l(fā)ist”,這里是固定寫法,即這里的list與你的實參名無關

3.如果你傳參的時候直接傳一個含有數(shù)組成員變量的類,那么使用foreach遍歷時collection=“你的變量名”,這里不再是固定寫法,即這里的命名取決于成員變量的變量名,例如:成員變量名是test,那么就是collection=“test”

4.如果你傳參的時候直接傳一個含有l(wèi)ist集合成員變量的類,跟3的情況一樣

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • mybatis的foreach標簽語法報錯的解決

    mybatis的foreach標簽語法報錯的解決

    這篇文章主要介紹了mybatis的foreach標簽語法報錯的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • SpringBoot結合Redis實現(xiàn)緩存管理功能

    SpringBoot結合Redis實現(xiàn)緩存管理功能

    本篇文章主要介紹spring boot緩存管理機制及相關概念,以及如何結合Redis實現(xiàn)緩存管理,文中通過代碼示例給大家介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下
    2024-01-01
  • Java對象傳遞與返回的細節(jié)問題詳析

    Java對象傳遞與返回的細節(jié)問題詳析

    我們知道這是一個核心概念,在Java中總是按值傳遞而不是按引用傳遞,下面這篇文章主要給大家介紹了關于Java對象傳遞與返回的細節(jié)問題的相關資料,需要的朋友可以參考下
    2022-11-11
  • Feign如何解決服務之間調用傳遞token

    Feign如何解決服務之間調用傳遞token

    這篇文章主要介紹了Feign如何解決服務之間調用傳遞token,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Spring中使用atomikos+druid實現(xiàn)經(jīng)典分布式事務的方法

    Spring中使用atomikos+druid實現(xiàn)經(jīng)典分布式事務的方法

    這篇文章主要介紹了Spring中使用atomikos+druid實現(xiàn)經(jīng)典分布式事務的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-06-06
  • java中的日期時間類Date和SimpleDateFormat

    java中的日期時間類Date和SimpleDateFormat

    這篇文章主要介紹了java中的日期時間類Date和SimpleDateFormat,Date類的對象在Java中代表的是當前所在系統(tǒng)的此刻日期時間,說白了就是你計算機上現(xiàn)實的時間,需要的朋友可以參考下
    2023-09-09
  • 新版idea創(chuàng)建spring boot項目的詳細教程

    新版idea創(chuàng)建spring boot項目的詳細教程

    這篇文章給大家介紹了新版idea創(chuàng)建spring boot項目的詳細教程,本教程對新手小白友好,若根據(jù)教程創(chuàng)建出現(xiàn)問題導致失敗可下載我提供的源碼,在文章最后,本教程較新,文中通過圖文給大家介紹的非常詳細,感興趣的朋友可以參考下
    2024-01-01
  • java中獲取hashmap中的所有key方式

    java中獲取hashmap中的所有key方式

    這篇文章主要介紹了java中獲取hashmap中的所有key方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Java中i++與++i的區(qū)別和使用

    Java中i++與++i的區(qū)別和使用

    這篇文章主要介紹了Java中i++與++i的區(qū)別和使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • 微信公眾平臺(測試接口)準備工作

    微信公眾平臺(測試接口)準備工作

    想要微信開發(fā),首先要有個服務器,但是自己沒有。這時候可以用花生殼,將內網(wǎng)映射到公網(wǎng)上,這樣就可以在公網(wǎng)訪問自己的網(wǎng)站了。
    2016-05-05

最新評論