mybatis批量插入及@param的作用

本文介绍MyBatis框架中使用foreach标签实现批量插入的方法,包括如何设置item、index等属性,以及如何通过ExecutorType.BATCH提高批量插入效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mybatis在多个参数时要加上@Param()注解,只要是数据基本类型全部要加上,这个注解。方法取参数也是优先在注解中取

  • 基本类型的参数或者String类型,需要加上
  • 引用类型不需要加
  • 如果只有一个基本类型的话,可以忽略,但是建议大家都加上!
  • 我们在SQL中引用的就是我们这里的 @Param() 中设定的属性名!

1.插入方式

mybiats foreach标签
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合。
foreach元素的属性主要有 item,index,collection,open,separator,close。
item表示集合中每一个元素进行迭代时的别名,index指定一个名字,用于表示在迭代过程中,每次迭代到的位置,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符,close表示以什么结束,在使用foreach的时候最关键的也是最容易出错的就是collection属性,该属性是必须指定的,但是在不同情况 下,该属性的值是不一样的,
1.如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2.如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3.如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以封装成map

<insert id="insertBatch" parameterType="List">
INSERT INTO TStudent(name,age)
    <foreach collection="list" item="item" index="index" open="("close=")"separator="union all">
    	SELECT #{item.name} as a, #{item.age} as b FROM DUAL
    </foreach>
</insert>

此种方法等价于

 insert into redeem_code (batch_id, code, type, facevalue,create_user,create_time)
 values
 (?,?,?,?,?,? ),(?,?,?,?,?,? ),(?,?,?,?,?,? ),(?,?,?,?,?,? )

2.补充:mybatis ExecutorType.BATCH 在SSM框架中的用法

(1)在全局配置文件applcationContext.xml中加入

  <!-- 配置一个可以批量执行的sqlSession -->
     <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
       <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
        <constructor-arg name="executorType" value="BATCH"></constructor-arg>
     </bean>

(2)在serviceImpl中加入

@Autowired
private SqlSession sqlSession;



//批量保存员工
@Override
public Integer batchEmp() {
  // TODO Auto-generated method stub//批量保存执行前时间long start=System.currentTimeMillis();

​       EmployeeMapper mapper=   sqlSession.getMapper(EmployeeMapper.class);for (int i = 0; i < 10000; i++) {
​         mapper.addEmp(new Employee(UUID.randomUUID().toString().substring(0,5),"b","1"));}long end= System.currentTimeMillis();long time2= end-start;//批量保存执行后的时间
​       System.out.println("执行时长"+time2);
​       
​      
​     return (int) time2;}

3.Orcale中使用myBatis进行批量插入

​ ps:由于项目较老此处写法也相对较老!

dao:

public int insertXinZhuangExcelLog(@Param("list") List<Map<String, String>> list) {
		return this.getSqlSession().insert("timingThan.insertXinZhuangExcelLog", list);
}

xml:

<insert id="insertXiaoBaiExcelLog" useGeneratedKeys="false" parameterType="java.util.Map">
   		insert into T_BH_XIAOBAI_EXCEL_LOG
   		(ORDER_ID,PHONE,PROVINCE,HOME_LOCAL,ORDER_TIME)
   		<foreach collection="list" item="itm" index="index"  separator="UNION ALL">
	     	select  
	     	#{itm.ORDER_ID},#{itm.PHONE},#{itm.PROVINCE},#{itm.HOME_LOCAL},#{itm.ORDER_TIME}
			from dual
   		</foreach>
   	</insert>

注意insert,一定要添加: useGeneratedKeys=“false” ,否者会报错。 和mysql略有不同

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值