概述
1、步骤:
①创建相关的bean类mapper类mapper映射文件和相关mybatis配置
②在程序中创建SqlSessionFactory和SqlSession
③在SqlSessionFactory.openSession时,给openSession的入参赋值为ExecutorType.BATCH
④后面就是使用循环对mapper插入对应的需要插入的数据
⑤在循环完毕之后要嗲用SqlSession的commit()方法
2、作用:
batch是批量处理增删改,是先把数据存储到缓存中,然后批量处理完之后才提交,整个过程只提交一次,这样更加有效率更加方便
3、注意:
调用SqlSessionFactory.openSession时,openSession的入参要设置为ExecutorType.BATCH
4、mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="dbConfig.properties"></properties>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="InfoMapper.xml" />
</mappers>
</configuration>
5、InfoMapper.xml
<?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.demo.ssmtest.InfoMapper">
<insert id="add" parameterType="com.demo.ssmtest.Info" useGeneratedKeys="true" keyProperty="id">
insert into info (name,password,description) values (#{name},#{password},#{description})
</insert>
</mapper>
6、dbConfig.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/mybatis
jdbc.username=root
jdbc.password=
7、Info.java
package com.demo.ssmtest;
public class Info {
Integer id;
String name;
String password;
String description;
public Info() {
super();
}
public Info(Integer id, String name, String password, String description) {
super();
this.id = id;
this.name = name;
this.password = password;
this.description = description;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Info [id=" + id + ", name=" + name + ", password=" + password + ", description=" + description + "]";
}
}
8、InfoMapper.java
package com.demo.ssmtest;
public interface InfoMapper {
public Integer add(Info info);
}
9、TestMain.java
package com.demo.ssmtest;
import java.io.InputStream;
import java.util.UUID;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class TestMain {
public static void main(String[] args) throws Exception {
test();
}
/*
批量处理:
1、步骤:
①创建相关的bean类mapper类mapper映射文件和相关mybatis配置
②在程序中创建SqlSessionFactory和SqlSession
③在SqlSessionFactory.openSession时,给openSession的入参赋值为ExecutorType.BATCH
④后面就是使用循环对mapper插入对应的需要插入的数据
⑤在循环完毕之后要嗲用SqlSession的commit()方法
2、作用:
batch是批量处理增删改,是先把数据存储到缓存中,然后批量处理完之后才提交,整个过程只提交一次,这样更加有效率更加方便
3、注意:
调用SqlSessionFactory.openSession时,openSession的入参要设置为ExecutorType.BATCH
*/
public static void test() throws Exception {
String resource = "mybatis.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession openSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
try {
InfoMapper addMapper = openSession.getMapper(InfoMapper.class);
for(int i=0;i<100;i++){
addMapper.add(new Info(null,UUID.randomUUID().toString().substring(0, 5),"qweqwe","dddddddd"));
}
openSession.commit();
} finally {
openSession.close();
}
}
}
10、项目目录
11、demo
https://download.csdn.net/download/qq_22778717/10719641
最后
以上就是安详龙猫为你收集整理的SSM笔记-批量处理的全部内容,希望文章能够帮你解决SSM笔记-批量处理所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复