桃城区网站制作公司,全响应式网站用什么做的,aso排名优化知识,仿中国化妆品网站模板1.foreach简单介绍#xff1a;foreach的主要用在构建in条件中#xff0c;它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有item#xff0c;index#xff0c;collection#xff0c;open#xff0c;separator#xff0c;close。item表示集合中每一个元素进行迭… 1.foreach简单介绍foreach的主要用在构建in条件中它可以在SQL语句中进行迭代一个集合。foreach元素的属性主要有itemindexcollectionopenseparatorclose。item表示集合中每一个元素进行迭代时的别名index指定一个名字用于表示在迭代过程中每次迭代到的位置open表示该语句以什么开始separator表示在每次进行迭代之间以什么符号作为分隔符close表示以什么结束collection属性是在使用foreach的时候最关键的也是最容易出错的该属性是必须指定的但是在不同情况下该属性的值是不一样的主要有一下3种情况 1如果传入的是单参数且参数类型是一个List的时候collection属性值为list .2如果传入的是单参数且参数类型是一个array数组的时候collection的属性值为array .3如果传入的参数是多个的时候我们就需要把它们封装成一个Map了当然单参数也可以封装成map实际上如果你在传入参数的时候在MyBatis里面也是会把它封装成一个Map的map的key就是参数名所以这个时候collection属性值就是传入的List或array对象在自己封装的map里面的key.4如果传入的是单个参数且参数类型为List的时候使用Param()注释参数名字那么collection属性值为参数名字。例[2]Dao:ListUser findListByIds(Param(idList)ListString idList);XMLselect idfindListByIds parameterTypejava.util.List resultTypeUserselect * from t_user where id inforeach itemitem indexindex collectionidList open( separator, close)#{item}/foreach
/select2.实践-实体类[java] view plaincopypublic class Employees { private Integer employeeId; private String firstName; private String lastName; private String email; private String phoneNumber; private Date hireDate; private String jobId; private BigDecimal salary; private BigDecimal commissionPct; private Integer managerId; private Short departmentId; } 3.实践-XML[html] view plaincopy!--List:forech中的collection属性类型是List,collection的值必须是:list,item的值可以随意,Dao接口中参数名字随意 -- select idgetEmployeesListParams resultTypeEmployees select * from EMPLOYEES e where e.EMPLOYEE_ID in foreach collectionlist itememployeeId indexindex open( close) separator, #{employeeId} /foreach /select !--Array:forech中的collection属性类型是array,collection的值必须是:list,item的值可以随意,Dao接口中参数名字随意 -- select idgetEmployeesArrayParams resultTypeEmployees select * from EMPLOYEES e where e.EMPLOYEE_ID in foreach collectionarray itememployeeId indexindex open( close) separator, #{employeeId} /foreach /select !--Map:不单单forech中的collection属性是map.key,其它所有属性都是map.key,比如下面的departmentId -- select idgetEmployeesMapParams resultTypeEmployees select * from EMPLOYEES e where if testdepartmentId!null and departmentId! e.DEPARTMENT_ID#{departmentId} /if if testemployeeIdsArray!null and employeeIdsArray.length!0 AND e.EMPLOYEE_ID in foreach collectionemployeeIdsArray itememployeeId indexindex open( close) separator, #{employeeId} /foreach /if /where /select 4.实践-Mapper[java] view plaincopypublic interface EmployeesMapper { ListEmployees getEmployeesListParams(ListString employeeIds); ListEmployees getEmployeesArrayParams(String[] employeeIds); ListEmployees getEmployeesMapParams(MapString,Object params); } 原文出处[1] zhangqifeng92, MyBatis传入参数为list、数组、map写法, https://blog.csdn.net/s592652578/article/details/52871884[2] 实践经验亲测可用 转载于:https://www.cnblogs.com/ryelqy/p/10104070.html