foreach一共有三种类型,分别为List,数组array,Map三种。
foreach属性
属性 | 描述 |
---|---|
item | 循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。 具体说明:在list和数组中是其中的对象,在map中是value。 该参数为必选。 |
collection | 要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象没有默认的键。 当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list,array将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子: 如果User有属性List ids。入参是User对象,那么这个collection = "ids" 如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id" 上面只是举例,具体collection等于什么,就看你想对那个元素做循环。 该参数为必选。 |
separator | 元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。 |
open | foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选。 |
close | foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。 |
index | 在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。 |
下面是总结的各种常见入参为各种类型的例子:
————————————————————————————
基础类型:例如:string
controller 或者 service:
String id;mapper:
<select id="testk" parameterType="java.lang.String" resultMap="info">
select s.id as stfid , s.glfqymc
from t_test st
where st.id = #{id,jdbcType=VARCHAR}
</select>
数组:
例:批量删除
String[] delId = new String[list.size()];mapper:
<delete id="deleteByIds" parameterType="java.lang.String">
delete from t_test where id in
<foreach collection="array" item="ckId" index="index"
open="(" close=")" separator=",">
#{ckId}
</foreach>
</delete>
这里要主要 collection是array
Map:
map中含基本类型,数组。
controller 或者 service:
String[] keysArray = new String[list.size()];
Map<String,Object> map = new HashMap<String,Object>();
map.put("zzjgdm", ”123456“);
map.put("keysArray", keysArray);
mapper:
<select id="testlist" parameterType="Map" resultMap="Info">
select s.id as stfid , s.glfqymc
from YFF_STFXX s
where s.ZZJG_DM = #{zzjgdm,jdbcType=VARCHAR} and s.xmbh in
<foreach item="xmbh" index="index" collection="keysArray" open="(" separator="," close=")">
#{xmbh}
</foreach>
</select>
</select>
mapper定义中parameterType="Map", zzjgdm为map中的key,
另外map中还有一个数组keysArray, 数组的处理:collection为也为map的key, item随便定义。
List:
1, 集合中存的是基本类型
controller 或者 service:
List<Integer> IntList 、 List<String> strList 集合中存的是基本类型的
mapper:
<select id="test" parameterType="java.util.List" resultMap="Users">
select id,name from T_TEST where id in
<foreach collection="list" index="index" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
item就是list中设置的String 值。
其中的传的参数是List集合所以collection 的值直接使用了list 来代替而index 就是这个集合遍历的索引值,
item 就是list当前索引位置的值 open 和close 成对出现,就是开始和结束的符号,而separator 就是分隔符。
2, 集合中存的是对象
例如: 批量插入
controller or service:
List<对象> list
mapper:
<insert id="batchInsert" parameterType="java.util.List">
insert into T_test (ID, SSQJ, YFRYBH, YXBZ)
<foreach collection="list" index="" item="item" separator="union all">
(select #{item.id,jdbcType=VARCHAR}, #{item.ssqj,jdbcType=VARCHAR}, #{item.yfrybh,jdbcType=VARCHAR}, #{item.yxbz,jdbcType=CHAR}
from dual )
</foreach>
</insert>
item就是一个别名, 循环中遍历item中的项目名 例:item.id。
批量更新:
controller 或 service:
List<Map<String,String>> updateParams = new ArrayList<Map<String,String>>();
Map<String,String> tempMap = new HashMap<String,String>();
tempMap.put("id",fj);
tempMap.put("sfywfl","xmjcxx");
uParams.add(tempMap);
mapper:
<update id="updateBatch" parameterType="java.util.List">
<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
update T_TEST
<set>
<if test="item.sfywfl != null">
SFYWFL = #{item.sfywfl,jdbcType=VARCHAR},
</if>
</set>
where ID = #{item.id,jdbcType=VARCHAR}
</foreach>
</update>
批量删除:
controller 或 service:
List<String> deleteParams = new ArrayList<String>();
mapper:
<!--批量删除-->
<delete id="deleteBatch" parameterType="java.util.List">
delete from YFF_FILEINFO
where ID IN
<foreach collection="list" item="id" separator="," open="(" close=")">
#{id,jdbcType=VARCHAR}
</foreach>
</delete>
这里的item中id, 可以随便定义, 因为list中是基本类型。
mybatis中的# 和$ 的区别,#的变量是带单引号的而$是不带单引号的, int型的所以不用#而是$,因为是数字而不是字符。
_________________________________________________________________________________
MyBatis之ResultMap标签
ResultMap标签基本作用:建立SQL查询结果字段与实体属性的映射关系信息在深入ResultMap标签前,我们需要了解从SQL查询结果集到JavaBean或POJO实体的过程。
1. 通过JDBC查询得到ResultSet对象
2. 遍历ResultSet对象并将每行数据暂存到HashMap实例中,以结果集的字段名或字段别名为键,以字段值为值
3. 根据ResultMap标签的type属性通过反射实例化领域模型
4. 根据ResultMap标签的type属性和id、result等标签信息将HashMap中的键值对,填充到领域模型实例中并返回
例:
<resultMap id="getStudentRM" type="EStudnet">
<id property="id" column="ID"/>
<result property="studentName" column="Name"/>
<result property="studentAge" column="Age"/>
</resultMap>
<select id="getStudent" resultMap="getStudentRM">
SELECT ID, Name, Age
FROM TStudent
</select>
标签说明:
主标签
id:该resultMap的标志
type:返回值的类名,此例中返回EStudnet类
子标签:
id:用于设置主键字段与领域模型属性的映射关系,此处主键为ID,对应id。
result:用于设置普通字段与领域模型属性的映射关系
具体例子:
Mapper定义:
<select id="getSelectList" resultMap="selectListMap"> SELECT LXMC ,LXCODE FROM TESTDB WHERE LXBM = #{dmlx,jdbcType=VARCHAR} </select> <resultMap id="selectListMap" type="map"> <id column="LXMC" jdbcType="VARCHAR" property="text" /> <id column="LXCODE" jdbcType="VARCHAR" property="value" /> </resultMap>
controller/service:
如果是集合形式返回可以定义成以下形式接受
List<HashMap<String, String>> selectList;