mybatis中的三种 批量操作数据的方法
2018-03-27 22:33
393 查看
方法1:
使用for循环在java代码中insert (不推荐)
方法2:
使用 在Mapper.xml当中使用 foreach循环的方式进行insert
PersonDao.java文件
public interface PersonDao {
//这个是使用 foreach方式的mybatis 批量操作
public void batchInsert(@Param("list")List<Person>list);
}
PersonDao.xml
<insert id="batchInsert" >
insert into person (
id,
person_name,
birthday,
address,
age,
gender
)
values
<foreach collection="list" item="list" index="index" separator="," >
(
#{list.id},
#{list.person_name},
#{list.birthday},
#{list.address},
#{list.age},
#{list.gender}
)
</foreach>
</insert>
主测试函数:
public static void main5(String[] args) throws Exception {
SqlSession session = getSqlSession();
PersonDao pd = session.getMapper( PersonDao.class );
List<Person>pl = new ArrayList<Person>();
Person p1 = new Person(); p1.setPerson_name("哈哈哈吧"); p1.setAddress("深圳"); p1.setBirthday(new Date());
Person p2 = new Person(); p2.setPerson_name("您好"); p2.setAddress("上海"); p2.setBirthday(new Date());
Person p3 = new Person(); p3.setPerson_name("我是张伟"); p3.setAddress("广州"); p3.setBirthday(new Date());
pl.add(p1);
pl.add(p2);
pl.add(p3);
pd.batchInsert(pl);
System.out.println("完成batchInsert");
session.commit();
session.close();
//pd.batchInsert( pl );
}
方法3:
Mybatis内置的 ExecutorType有三种,默认是Simple,该模式下它为每个语句的执行创建一个新的预处理语句,
单条提交sql,而batch模式 重复使用已经预处理的语句,并且批量执行所有更新语句,显然 batch的性能更优,
中间在提交的过程中还可以设置等待时间,避免数据库压力过大。
public static void main(String[] args) throws Exception {
InputStream in = new FileInputStream( "F:\\myeclipse_workspace\\mybatisGeneratortest\\src\\test\\resources\\mybatis-config.xml");
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = ssfb.build(in);
SqlSession session = factory.openSession( ExecutorType.BATCH,false );
PersonDao pd = session.getMapper( PersonDao.class );
int size = 100000;
try {
for(int i=0;i<size;i++){
Person p = new Person();
p.setPerson_name("小明"); p.setBirthday(new Date());
pd.insertPe
4000
ron(p);
if( i % 100 == 0 || i == size - 1 ){ //加上这样一段代码有好处,比如有100000条记录,每超过 100 条提交一次,中间等待10ms,可以避免一次性提交过多数据库压力过大
session.commit();
session.clearCache();
Thread.sleep(10);
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
使用for循环在java代码中insert (不推荐)
方法2:
使用 在Mapper.xml当中使用 foreach循环的方式进行insert
PersonDao.java文件
public interface PersonDao {
//这个是使用 foreach方式的mybatis 批量操作
public void batchInsert(@Param("list")List<Person>list);
}
PersonDao.xml
<insert id="batchInsert" >
insert into person (
id,
person_name,
birthday,
address,
age,
gender
)
values
<foreach collection="list" item="list" index="index" separator="," >
(
#{list.id},
#{list.person_name},
#{list.birthday},
#{list.address},
#{list.age},
#{list.gender}
)
</foreach>
</insert>
主测试函数:
public static void main5(String[] args) throws Exception {
SqlSession session = getSqlSession();
PersonDao pd = session.getMapper( PersonDao.class );
List<Person>pl = new ArrayList<Person>();
Person p1 = new Person(); p1.setPerson_name("哈哈哈吧"); p1.setAddress("深圳"); p1.setBirthday(new Date());
Person p2 = new Person(); p2.setPerson_name("您好"); p2.setAddress("上海"); p2.setBirthday(new Date());
Person p3 = new Person(); p3.setPerson_name("我是张伟"); p3.setAddress("广州"); p3.setBirthday(new Date());
pl.add(p1);
pl.add(p2);
pl.add(p3);
pd.batchInsert(pl);
System.out.println("完成batchInsert");
session.commit();
session.close();
//pd.batchInsert( pl );
}
方法3:
Mybatis内置的 ExecutorType有三种,默认是Simple,该模式下它为每个语句的执行创建一个新的预处理语句,
单条提交sql,而batch模式 重复使用已经预处理的语句,并且批量执行所有更新语句,显然 batch的性能更优,
中间在提交的过程中还可以设置等待时间,避免数据库压力过大。
public static void main(String[] args) throws Exception {
InputStream in = new FileInputStream( "F:\\myeclipse_workspace\\mybatisGeneratortest\\src\\test\\resources\\mybatis-config.xml");
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = ssfb.build(in);
SqlSession session = factory.openSession( ExecutorType.BATCH,false );
PersonDao pd = session.getMapper( PersonDao.class );
int size = 100000;
try {
for(int i=0;i<size;i++){
Person p = new Person();
p.setPerson_name("小明"); p.setBirthday(new Date());
pd.insertPe
4000
ron(p);
if( i % 100 == 0 || i == size - 1 ){ //加上这样一段代码有好处,比如有100000条记录,每超过 100 条提交一次,中间等待10ms,可以避免一次性提交过多数据库压力过大
session.commit();
session.clearCache();
Thread.sleep(10);
}
}
} catch (Exception e) {
// TODO: handle exception
}
}
相关文章推荐
- sql下三种批量插入数据的方法
- ASP.NET对无序列表批量操作的三种方法小结
- ASP.NET批量操作基于原生html标签的无序列表的三种方法
- ASP.NET对无序列表批量操作的三种方法
- DataTable数据批量写入数据库三种方法比较
- sql下三种批量插入数据的方法
- DataTable数据批量写入数据库三种方法比较 以及方法介绍
- 数据库--MyBatis的(insert,update,delete)三种批量操作
- mybatis以map的方式批量插入list集合数据操作步骤
- Mybatis Oracle 批量插入数据的方法
- mybatis返回map类型数据空值字段不显示(三种解决方法)
- mybatis返回map类型数据空值字段不显示(三种解决方法)
- mybatis批量操作Oracle数据
- sqlserver 下三种批量插入数据的方法
- ASP.NET对无序列表批量操作的三种方法小结
- 批量操作数据方法(实例)
- 三种批量插入数据的方法
- MyBatis的关于批量数据操作的测试
- 三种批量插入数据的方法
- ASP.NET批量操作基于原生html标签的无序列表的三种方法