Mapper文件代码
<!--批量插入 如果主键自增,想要返回自增主键,添加 useGeneratedKeys="true"-->
<insert id="multiplyInsertSelective" parameterType="java.util.List" >
insert into ep_chapter
<foreach collection="chapterList" separator="," index="index" item="item">
<if test="index == 0">
<trim prefix="(" suffix=") values " suffixOverrides="," >
<if test="#{item.id} != null" >
id,
</if>
<if test="#{item.chapterName} != null" >
chapter_name,
</if>
<if test="#{item.courseId} != null" >
course_id,
</if>
<if test="#{item.updateTime} != null" >
update_time,
</if>
<if test="#{item.duration} != null" >
duration,
</if>
<if test="#{item.status} != null" >
status,
</if>
<if test="#{item.intro} != null" >
intro,
</if>
<if test="#{item.url} != null" >
url,
</if>
<if test="#{item.root} != null" >
root,
</if>
<if test="#{item.free} != null" >
free,
</if>
<if test="#{item.createTime} != null" >
create_time,
</if>
</trim>
</if>
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="#{item.id} != null" >
#{item.id,jdbcType=BIGINT},
</if>
<if test="#{item.chapterName} != null" >
#{item.chapterName,jdbcType=VARCHAR},
</if>
<if test="#{item.courseId} != null" >
#{item.courseId,jdbcType=BIGINT},
</if>
<if test="#{item.updateTime} != null" >
#{item.updateTime,jdbcType=TIMESTAMP},
</if>
<if test="#{item.duration} != null" >
#{item.duration,jdbcType=VARCHAR},
</if>
<if test="#{item.status} != null" >
#{item.status,jdbcType=INTEGER},
</if>
<if test="#{item.intro} != null" >
#{item.intro,jdbcType=BIGINT},
</if>
<if test="#{item.url} != null" >
#{item.url,jdbcType=VARCHAR},
</if>
<if test="#{item.root} != null" >
#{item.root,jdbcType=BIGINT},
</if>
<if test="#{item.free} != null" >
#{item.free,jdbcType=INTEGER},
</if>
<if test="#{item.createTime} != null" >
#{item.createTime,jdbcType=TIMESTAMP},
</if>
</trim>
</foreach>
</insert>
dao(mapper)层写法
//传入一个list,返回受影响数(插入数), @Param("chapterList")对应xml的collection="chapterList"
//这个方法如果传入的是null或list.size()==0,会运行时报错
int multiplyInsertSelective(@Param("chapterList") List<EpChapter> chapterList);
解释下几个标签
<foreach>
item:
循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。
具体说明:在list和数组中是其中的对象,在map中是value。
该参数为必选。
collection:
要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替
作为键,Map对象没有默认的键。当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,
list,array将会失效,如上面的 @Param("chapterList")对应xml的collection="chapterList"。
除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子:如果class有属性List students。
入参是User对象,那么这个collection = "students"如果class有属性student;其中
student是个对象,student有个属性List books;入参是User对象,那么collection ="student.books"
上面只是举例,具体collection等于什么,就看你想对那个元素做循环。
该参数为必选。
separator:
元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中
间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。
open:
foreach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选。
close:
foreach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。
index:
在list和数组中,index是元素的序号,在map中,index是元素的key,一般在判断的情境下使用,该参数可选。
<if>
test:
判断条件,如果真,执行标签里内容,例子如上
<trim>
prefix:
在标签内容前填充指定前缀
suffix:
在标签内容后填充指定后缀
prefixOverrides:
去除标签内容前指定前缀,可选
suffixOverrides:
去除标签内容后指定后缀,可选
如:<trim prefix="(" suffix=")" suffixOverrides="," perfixOverrides=",">,1,2,3,</trim>
执行的结果是(1,2,3)