【MyBatis】动态SQL==><foreach>实现批量插入

这篇博客展示了如何使用MyBatis的动态SQL接口EmployeeMapperDynamicSQL实现批量插入Employee对象,通过SqlSessionFactory和Mapper接口完成操作,并附带了相关XML映射文件。
public interface EmployeeMapperDynamicSQL {
	
	public void addEmps(@Param("emps")List<Employee> emps);
}
 
public class MyBatisTest {
	

	

	
	@Test
	public void testBatchSave() throws IOException{
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
		SqlSession openSession = sqlSessionFactory.openSession();
		try{
			EmployeeMapperDynamicSQL mapper = openSession.getMapper(EmployeeMapperDynamicSQL.class);
			List<Employee> emps = new ArrayList<>();
			emps.add(new Employee(null, "smith0x1", "smith0x1@atguigu.com", "1",new Department(1)));
			emps.add(new Employee(null, "allen0x1", "allen0x1@atguigu.com", "0",new Department(1)));
			mapper.addEmps(emps);
			openSession.commit();
		}finally{
			openSession.close();
		}
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.mybatis.dao.EmployeeMapperDynamicSQL">
	<insert id="addEmps">
	 	insert into tbl_employee(last_name,email,gender,d_id) 
		values

		<foreach collection="emps" item="emp" separator=",">
			(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
		</foreach>
	 </insert>
</mapper>
根据以上帮我补充jdbctype:<if test="remaining_capacity != null">#{remaining_capacity, jdbcType= }</if><if test="remaining_capacity == null">NULL</if>, <if test="extreme_temp_duration != null">#{extreme_temp_duration, jdbcType= }</if><if test="extreme_temp_duration == null">NULL</if>, <if test="extreme_temp_charge_time != null">#{extreme_temp_charge_time, jdbcType= }</if><if test="extreme_temp_charge_time == null">NULL</if>, <if test="remaining_energy != null">#{remaining_energy, jdbcType= }</if><if test="remaining_energy == null">NULL</if>, <if test="battery_system_status != null">#{battery_system_status, jdbcType= }</if><if test="battery_system_status == null">NULL</if>, <if test="capacity_throughput != null">#{capacity_throughput, jdbcType= }</if><if test="capacity_throughput == null">NULL</if>, <if test="current_max_output_power != null">#{current_max_output_power, jdbcType= }</if><if test="current_max_output_power == null">NULL</if>, <if test="total_discharge_energy != null">#{total_discharge_energy, jdbcType= }</if><if test="total_discharge_energy == null">NULL</if>, <if test="severe_undervoltage_count != null">#{severe_undervoltage_count, jdbcType= }</if><if test="severe_undervoltage_count == null">NULL</if>, <if test="severe_overvoltage_count != null">#{severe_overvoltage_count, jdbcType= }</if><if test="severe_overvoltage_count == null">NULL</if>, <if test="production_date != null">#{production_date, jdbcType= }</if><if test="production_date == null">NULL</if>, <if test="energy_round_trip_efficiency != null">#{energy_round_trip_efficiency, jdbcType= }</if><if test="energy_round_trip_efficiency == null">NULL</if>, <if test="energy_throughput != null">#{energy_throughput, jdbcType= }</if><if test="energy_throughput == null">NULL</if>
最新发布
12-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值