MyBatis批量插入数据
作者:互联网
一、假设有实体phone:
@Data
public class Phone implements Serializable {
// 数据库中已设置自增
private Integer id;
private String name;
private String type;
private Date time;
private static final long serialVersionUID = 1L;
}
二、Dao接口(PhoneDao)
@Mapper
@Component
public interface PhoneDao {
/**
* 批量插入数据
*
* @param phoneList
* @return
*/
int insertBatch(List<Phone> phoneList);
// 注意参数名称phoneList,
// 需跟xml文件中collection=“ ”中的参数名相同。
}
三、xml文件
<insert id="insertBatch">
insert into phone(name,type ,time) values
<foreach collection="phoneList" item="phone" separator=",">
(#{phone.name},#{phone.type},#{phone.time})
</foreach>
</insert>
四、service层调用
@Service
public class PhoneService {
@Resource
private PhoneDao phoneDao;
/**
* 批量插入
* @param phoneList
*/
public int insertBatch(List<Phone> phoneList){
return phoneDao.insertBatch(phoneList);
}
}
标签:insertBatch,批量,phone,private,插入,phoneList,MyBatis,PhoneDao,public 来源: https://blog.csdn.net/qq_42182034/article/details/118345581