码迷,mamicode.com
首页 > 其他好文 > 详细

批量执行插入操作-帮同事做

时间:2018-12-26 10:34:03      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:amr   puts   boot   @Value   leo   print   ble   tco   encoding   

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.*;

/**
 * 执行大批量插入文本
 */
@Service
public class TxtServiceImpl implements TxtSevice{

    public  static int runningCount = 0; //正在执行的个数
    public  static int sucCount = 0; //成功个数
    public static int failCount = 0; //失败个数3

    @Value("${srcfile}")
    public String srcFile;

    @Value("${desfile}")
    public String desfile;

    @Value("${perprint}")
    public int perprint;

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    @PostConstruct//启动后自己执行
    public  void insertTxt() throws IOException {

            String name = srcFile;
            File file = new File(name);
            InputStreamReader inputReader = new InputStreamReader(new FileInputStream(file));
            BufferedReader bf = new BufferedReader(inputReader);
            // 按行读取字符串
            String str = "";
            while ((str = bf.readLine()) != null) {
                //str = new String(str.getBytes("gb2312"), "UTF-8");//将读取出来的GBK格式的代码转换成UTF-8
                try{
                    jdbcTemplate.update(str);
                    sucCount++;
                    printRuningCount();
                }catch (Exception e) {
                    writeFile(str);
                    failCount++;
                    printRuningCount();
                }
                //System.out.println(str);
            }
            bf.close();
            inputReader.close();
            System.out.println("执行总条数:"+(sucCount+failCount)+",  成功"+sucCount+"次  ,"+"失败"+failCount+"次  ,");

    }


    /**
     * 每xxx打印一次
     */
    private void printRuningCount(){
        runningCount++;
        if(runningCount%perprint==0){//每1000打印一次
            System.out.println("当前已经执行 "+runningCount+" 条");
        }
    }

    /**
     * 失败写文件
     * @param str
     * @throws IOException
     */
    public void writeFile(String str) throws IOException {
        File file = new File(desfile);
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw =null;
        try{
            if (!file.exists()) {
                boolean hasFile = file.createNewFile();
                if(hasFile){
                    //System.out.println("文件不存在,创建新文件");
                }
                fos = new FileOutputStream(file);
            } else {
                //System.out.println("文件已经存在,正在进行追加数据");
                fos = new FileOutputStream(file, true);
            }
            osw = new OutputStreamWriter(fos, "UTF-8");
            bw = new BufferedWriter(osw);
            bw.write(str);
            bw.newLine();//换行
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            bw.close();
            osw.close();
            fos.close();
        }
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <!-- plugins -->

    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>



        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.29</version>
        </dependency>

    <!--    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>-->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <repositories>
        <repository>
            <id>myRepository</id>
            <name>Repository for me</name>
            <url>http://192.168.100.10/nexus/content/groups/public/</url>
        </repository>
        <repository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>Central Repository</name>
            <url>http://repo.maven.apache.org/maven2</url>
        </repository>
    </repositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>



</project>
package com.example.demo;

import com.example.demo.service.TxtSevice;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.io.IOException;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {

    @Resource
    TxtSevice txtService;

    @Test
    public void contextLoads() throws IOException {

        txtService.insertTxt();
    }

    @Test
    public void contextLoads1() throws IOException {
        System.out.println("吞吞吐吐");
    }


}
spring.datasource.url=xxx
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver


spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

srcfile=C:/All_Files/sql.txt
desfile=C:/All_Files/failsql.txt
perprint=1000

 

批量执行插入操作-帮同事做

标签:amr   puts   boot   @Value   leo   print   ble   tco   encoding   

原文地址:https://www.cnblogs.com/bee-home/p/10177458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!