mybatis使用foreach遍歷list集合或者array數(shù)組方式
dataSource.driver=com.mysql.jdbc.DriverdataSource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8dataSource.username=blogdataSource.password=blog2.主配置文件
<?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><!-- 別名設(shè)置,不設(shè)置時(shí)引用要使用全包名,設(shè)置后可以使用自定義別名,更加簡(jiǎn)潔 --><typeAliases><!-- 別名設(shè)置有兩種,一種是一個(gè)一個(gè)設(shè)置,另外一種是設(shè)置某個(gè)包,默認(rèn)別名為類名(大小寫都可以,建議小寫) --><!-- 第一種設(shè)置 <typeAlias type='com.mybatis_demo.domain.User' alias='user'/>--> <!-- 第二種設(shè)置,整個(gè)包下面的類都進(jìn)行別名設(shè)置,推薦第二種 --> <package name='com.mybatis_demo.domain'/> </typeAliases><!-- 環(huán)境模式:development開發(fā)模式 work工作模式 --> <environments default='development'> <!-- 環(huán)境變量 --> <environment id='development'> <!-- 使用jdbc的事務(wù)管理 --> <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.相對(duì)路徑引入--> <!-- <mapper resource='mapper/UserMapper.xml'/> --> <!-- 2.絕對(duì)路徑引入 --> <!-- <mapper url='file:D:sts-bundleworkplacemybatis_demosrcmainresourcesmapperUserMapper.xml'/> --> <!-- 3.對(duì)應(yīng)mapper接口全包名引入,需要對(duì)應(yīng)的mapper.xml與接口mapper處于同一包下才可以,且xml文件名與接口名要相同,xml文件中的namespace必須是對(duì)應(yīng)接口的全包名 --> <!-- <mapper /> --> <!-- 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;}@Overridepublic 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ù)組和集合的映射文件和對(duì)應(yīng)的接口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',如果你傳參的時(shí)候是直接傳遞list集合,那么這里只能填list,不能填參數(shù)名 --><select resultType='User'>select * from t_user where uid in<foreach collection='list' item='item' open='(' separator=',' close=')'>#{item}</foreach></select><!-- 遍歷數(shù)組 ,collection='array',如果你傳參的時(shí)候是直接傳遞數(shù)組,那么這里只能填array,不能填參數(shù)名--><select resultType='User'>select * from t_user where uid in<foreach collection='array' item='item' open='(' separator=',' close=')'>#{item}</foreach></select><!-- 遍歷包裝類中的數(shù)組,collection='ids',這里不再是array,而是包裝類中對(duì)應(yīng)的變量名,因?yàn)槟銈鬟f的參數(shù)是一個(gè)包裝類,mybatis是通過(guò)get方法獲取包裝類中的數(shù)組 --><select 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,而是包裝類中對(duì)應(yīng)的變量名,因?yàn)槟銈鬟f的參數(shù)是一個(gè)包裝類,mybatis是通過(guò)get方法獲取包裝類中的list集合 --><select 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動(dòng)態(tài)代理//4大原則,一個(gè)注意//1.接口中的方法名需要與對(duì)應(yīng)mapper.xml的id一致//2.接口中的返回值需要與對(duì)應(yīng)mapper.xml的返回值類型保持一致//3.接口中的參數(shù)需要與對(duì)應(yīng)mapper.xml的參數(shù)類型、個(gè)數(shù)、參數(shù)名保持一致//4.對(duì)應(yīng)mapper.xml的名字空間需要修改成對(duì)應(yīng)接口的全包名//注意:mapper動(dòng)態(tài)代理根據(jù)返回值類型,mybatis會(huì)自動(dòng)選擇調(diào)用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);}三、測(cè)試代碼
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ù)是一個(gè)包裝類@Testpublic void test_selectUserVoByList() {try { //讀取配置文件InputStream in = Resources.getResourceAsStream('sqlMapConfig.xml');//創(chuàng)建SqlSessionFactoryBuilder對(duì)象,用來(lái)獲取SqlSessionFactory對(duì)象SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();//利用SqlSessionFactoryBuilder對(duì)象build一個(gè)SqlSessionFactory對(duì)象SqlSessionFactory build = builder.build(in);//利用sqlSessionFactory獲取session對(duì)象SqlSession session = build.openSession();//通過(guò)session對(duì)象獲取對(duì)應(yīng)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ù)是一個(gè)包裝類@Testpublic void test_selectUserVoByArray() {try { //讀取配置文件InputStream in = Resources.getResourceAsStream('sqlMapConfig.xml');//創(chuàng)建SqlSessionFactoryBuilder對(duì)象,用來(lái)獲取SqlSessionFactory對(duì)象SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();//利用SqlSessionFactoryBuilder對(duì)象build一個(gè)SqlSessionFactory對(duì)象SqlSessionFactory build = builder.build(in);//利用sqlSessionFactory獲取session對(duì)象SqlSession session = build.openSession();//通過(guò)session對(duì)象獲取對(duì)應(yīng)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ù)是一個(gè)數(shù)組@Testpublic void test_selectByArray() {try { //讀取配置文件InputStream in = Resources.getResourceAsStream('sqlMapConfig.xml');//創(chuàng)建SqlSessionFactoryBuilder對(duì)象,用來(lái)獲取SqlSessionFactory對(duì)象SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();//利用SqlSessionFactoryBuilder對(duì)象build一個(gè)SqlSessionFactory對(duì)象SqlSessionFactory build = builder.build(in);//利用sqlSessionFactory獲取session對(duì)象SqlSession session = build.openSession();//通過(guò)session對(duì)象獲取對(duì)應(yīng)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ù)是一個(gè)list集合@Testpublic void test_selectByList() {try { //讀取配置文件InputStream in = Resources.getResourceAsStream('sqlMapConfig.xml');//創(chuàng)建SqlSessionFactoryBuilder對(duì)象,用來(lái)獲取SqlSessionFactory對(duì)象SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();//利用SqlSessionFactoryBuilder對(duì)象build一個(gè)SqlSessionFactory對(duì)象SqlSessionFactory build = builder.build(in);//利用sqlSessionFactory獲取session對(duì)象SqlSession session = build.openSession();//通過(guò)session對(duì)象獲取對(duì)應(yīng)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();}}}四、總結(jié)
1.如果你傳參的時(shí)候直接傳一個(gè)數(shù)組,那么使用foreach遍歷時(shí)collection=“array”,這里是固定寫法,即這里的array與你的實(shí)參名無(wú)關(guān)
2.如果你傳參的時(shí)候直接傳一list集合,那么使用foreach遍歷時(shí)collection=“l(fā)ist”,這里是固定寫法,即這里的list與你的實(shí)參名無(wú)關(guān)
3.如果你傳參的時(shí)候直接傳一個(gè)含有數(shù)組成員變量的類,那么使用foreach遍歷時(shí)collection=“你的變量名”,這里不再是固定寫法,即這里的命名取決于成員變量的變量名,例如:成員變量名是test,那么就是collection=“test”
4.如果你傳參的時(shí)候直接傳一個(gè)含有l(wèi)ist集合成員變量的類,跟3的情況一樣
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. MySQL 字符串函數(shù):字符串截取2. MySQL 8.0 之索引跳躍掃描(Index Skip Scan)3. Microsoft Office Access修改代碼字體大小的方法4. Mysql 用戶權(quán)限管理實(shí)現(xiàn)5. mysql數(shù)據(jù)存放的位置在哪6. Mysql入門系列:安排預(yù)防性的維護(hù)MYSQL數(shù)據(jù)庫(kù)服務(wù)器7. 恢復(fù)從 Access 2000、 Access 2002 或 Access 2003 中數(shù)據(jù)庫(kù)刪除表的方法8. MySQL中InnoDB和MyISAM類型的差別9. SQLServer的內(nèi)存管理架構(gòu)詳解10. 在SQL Server中用XQuery分解XML數(shù)據(jù)
