在写insert子句的时候,由于不知道需要插入多少字段,mybatis通过prefix,suffix,suffixOverrides很好的解决了该问题,实现了动态insert语句。
用这种动态插入时<if test=""></test>这里test的字段一定不要写错,本来直接写字段名就可以了,写错为#字段名 就不起作用了
<insert >
#{gmtCreate,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
顺便记一下批量插入
Mapper接口中提供
public void batchSave(List<Emp> empList);
Mapper.xml提供
<insert >
(#{emp.empName}, #{emp.empEmail}, #{emp.deptId})
</foreach>
</insert>