>  기사  >  데이터 베이스  >  Java를 사용하여 Excel에서 MySQL로 데이터를 가져오는 방법

Java를 사용하여 Excel에서 MySQL로 데이터를 가져오는 방법

王林
王林앞으로
2023-05-29 23:52:041532검색

1.pom 파일

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.1.7</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.62</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

2. 구성 파일

server.port=8080

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

spring.datasource.url=jdbc:mysql://localhost:3306/ddb_resources?serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root

4. Entity 클래스(데이터베이스 해당)

@Mapper
public interface DdbBookNewWordMapper extends BaseMapper<DdbBookNewWord> {
}

6. 7. 청취자

@Service
public class DemoDAO {
    @Autowired
    DdbBookNewWordMapper mapper;
    
    public void save(List<DataDemo001> list) {
        // 如果是mybatis,尽量别直接调用多次insert,自己写一个mapper里面新增一个方法batchInsert,所有数据一次性插入
        DdbBookNewWord newWord = new DdbBookNewWord();
        System.out.println("插入数据开始===============================");
        for (DataDemo001 info : list) {
            newWord.setAppType(0);
            newWord.setFkBookId(info.getFkBookId());
            newWord.setWord(info.getWord());
            newWord.setSimpleExplain(info.getSimpleExplain());
            newWord.setImgUrl("/incoming/ddb/wordImg/"+info.getFkBookId()+"/"+info.getImgUrl());
            newWord.setSoundUrl("/incoming/ddb/wordAudio/bookStudyMp3/"+info.getFkBookId()+"/"+info.getSoundUrl());
            newWord.setCreateTimeInMs(1620983400709L);
            newWord.setUpdateTimeInMs(1620983400709L);
            System.out.println(newWord);
            mapper.insert(newWord);
        }
        System.out.println("结束=========================");
    }
}

8. 테스트 수업

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class DdbBookNewWord {
    private int id;
    private String fkBookId;
    private String word;
    private String simpleExplain;
    private String imgUrl;
    private long createTimeInMs;
    private long updateTimeInMs;
    private int appType;
    private String soundUrl;
}

9. 창업 수업 (쓸모 없음)

@Data
public class DataDemo001 {
    private String fkBookId;
    private String bookeName;
    private String moudle;
    private String unit;
    private String word;
    private String soundUrl;
    private String imgUrl;
    private String simpleExplain;
}

위 내용은 Java를 사용하여 Excel에서 MySQL로 데이터를 가져오는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 yisu.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제