一)Mybatis <foreach>标签
foreach元素的属性主要有 item,index,collection,open,separator,close。
item:表示集合中每一个元素进行迭代时的别名
index:指定一个名字,用于表示在迭代过程中,每次迭代到的位置
collection:
如果传入的参数类型是一个List时,collection属性值为list
如果传入的参数类型是一个array数组时,collection的属性值为array
如果传入的参数是多个时,可把参数封装成一个Map
open:表示该语句以什么开始
separator:表示该语句在每次进行迭代之间以什么符号作为分隔符
close:表示该语句以什么结束
二)Mybatis批量插入
方式一:单条新增,批次提交
xml代码:
<insert id="addEmployee" parameterType="com.oysept.entity.EmployeeEntity">
insert into tab_employee(emp_id,emp_name,emp_no,create_date)
values(#{empId}, #{empName}, #{empNO}, #{createDate})
</insert>
DAO接口:
int addEmployee(EmployeeEntity entity);
insert代码:
package com.oysept.test;
import java.util.Date;
import org.apache.ibatis.session.SqlSession;
import com.oysept.dao.EmployeeDAO;
import com.oysept.entity.EmployeeEntity;
import com.oysept.utils.SessionUtils;
public class AddEmployeeTest {
public static void main(String[] args) {
SqlSession sqlSession = null;
try {
long beginTime = System.currentTimeMillis();
sqlSession = SessionUtils.getSqlSessionFactory().openSession();
// 初始化DAO接口
EmployeeDAO employeeDAO = sqlSession.getMapper(EmployeeDAO.class);
EmployeeEntity entity = null;
for (int i = 0; i < 500; i++) {
entity = new EmployeeEntity();
entity.setEmpId(100000 + i);
entity.setEmpName("ouyangjun");
entity.setEmpNO("333333");
entity.setCreateDate(new Date().getTime());
// 持久