高中教做网站的软件,惠州市网站开发,wordpress明文密码,做影视网站需要多大硬盘最近在做项目的时候#xff0c;有些明细数据#xff0c;一条一条的插入太费资源和时间#xff0c;所以得需要批量插入#xff0c;今晚闲来无事写个小demo。
新建工程 dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis…最近在做项目的时候有些明细数据一条一条的插入太费资源和时间所以得需要批量插入今晚闲来无事写个小demo。
新建工程 dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.3.1/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency
工程目录 application.properties配置
spring.datasource.urljdbc:mysql://127.0.0.1:3306/dblog
spring.datasource.usernameroot
spring.datasource.password666666
spring.datasource.driver-class-namecom.mysql.cj.jdbc.Driver
mybatis.mapper-locationsclasspath:config/*Mapper.xml
mybatis.type-aliases-packagecom.zy.entityteacher表结构 mybatis语句
insert idinsertTeacherinsert into teacher (name, age, sex) valuesforeach collectionlist separator, itemitem(#{item.name}, #{item.age}, #{item.sex})/foreach/insert接口定义
Repository
public interface TeacherMapper {public int insertTeacher(ListTeacher teacher);
}测试代码
RestController
public class MybatisController {Autowiredpublic TeacherMapper teacherMapper;GetMapping(teachers)public String teachers() {Long a System.currentTimeMillis();for (int t0;t100;t) {ListTeacher teachers new ArrayList();for (int n0;n1000;n) {Teacher teacher new Teacher();teacher.setName(李继tn);teacher.setAge(35);teacher.setSex(男);teachers.add(teacher);}teacherMapper.insertTeacher(teachers);}long s (System.currentTimeMillis()-a)/1000;System.out.println(s);return success:s;}}启动类 SpringBootApplication
MapperScan(basePackages com.zy.dao, annotationClass Repository.class)
public class MybatisDemoApplication {public static void main(String[] args) {SpringApplication.run(MybatisDemoApplication.class, args);}}测试结果
插入1万条数据用时1秒插入10万条数据用时10秒。