springBoot+springDataJpa將Excel導入MySql數(shù)據(jù)庫

1、新建一個springBoot項目

pom.xml依賴配置

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-jpa</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>mysql</groupId>

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

<version>5.1.45</version>

</dependency>

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>druid</artifactId>

<version>1.1.12</version>

</dependency>

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.2.2</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

<exclusions>

<exclusion>

<groupId>org.junit.vintage</groupId>

<artifactId>junit-vintage-engine</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi</artifactId>

<version>3.15</version>

</dependency>

<dependency>

<groupId>org.apache.poi</groupId>

<artifactId>poi-ooxml</artifactId>

<version>3.15</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.2</version>

</dependency>

<dependency>

<groupId>org.projectlombok</groupId>

<artifactId>lombok</artifactId>

<version>1.18.12</version>

<scope>provided</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

2溜歪、數(shù)據(jù)庫配置

#mysql配置

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://47.92.245.9:3306/peo-test?&useUnicode=true&characterEncoding=UTF-8

spring.datasource.username=root

spring.datasource.password=Btkx1234!

spring.jpa.hibernate.ddl-auto=update

spring.jpa.hibernate.show-sql=true

spring.jpa.show-sql=true

#druid 數(shù)據(jù)源

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

#編碼格式設置

spring.thymeleaf.encoding=UTF-8

3、配置靜態(tài)頁面

在項目的resources/templates/excelImport.html下

頁面內容:

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>Excel導入</title></head>

<body>

<form enctype="multipart/form-data" method="post" action="/excelImport">選擇Excel表格:<input type="file" name="file"/><br/>

<input type="submit" value="上傳"/></form>

</body>

</html>

4脖含、DAO層

import com.java.excel.Entity.Excel;

import org.springframework.data.jpa.repository.JpaRepository;

public interface ExcelDaoextends JpaRepository {

}

5、service層

@Service

//事務控制

@Transactional

public class ExcelService {

@Autowired

private ExcelDaoexcelDao;

public void saveExcel(MultipartFile file)throws IOException {

//根據(jù)路徑獲取這個操作excel的實例

//? ? ? HSSFWorkbook wb = new HSSFWorkbook(file.getInputStream());

? ? ? ? XSSFWorkbook work =new XSSFWorkbook(file.getInputStream());

//根據(jù)頁面index 獲取sheet頁

//? ? ? HSSFSheet sheet = wb.getSheetAt(0);

? ? ? ? XSSFSheet sheet = work.getSheetAt(0);

//? ? ? ? HSSFSheet 和XSSFSheet 是有區(qū)別的投蝉,自行百度

? ? ? ? ?XSSFRow row =null;//接受每一行的數(shù)據(jù)????

? ??????for (int i =1; i < sheet.getPhysicalNumberOfRows(); i++) {

? ? ? ? ? ? ?//獲取每一行數(shù)據(jù)

? ? ? ? ? ? ?row = sheet.getRow(i);

? ? ? ? ? ? //要判斷每一行數(shù)據(jù)是否為空养葵,此處沒寫,自行百度

? ? ? ? ? ? ?row.getCell(0);//拿到每一列

? ? ? ? ? ? //拿到每一列后放入對應的實體中瘩缆,新增

????????}

}

6关拒、controller層

@Controller

public class ExcelController {

????@Autowired

? ? private ExcelServiceexcelService;

? ??@GetMapping("/toHtml")

????public String test(HttpServletRequest request) {

????????return "excelImport";

????}

????//處理文件上傳

????@ResponseBody//返回json數(shù)據(jù)

????@RequestMapping(value ="/excelImport", method = RequestMethod.POST)

????public String uploadImg(@RequestParam("file") MultipartFile file, HttpServletRequest request) {

????????String contentType = file.getContentType();

????????String fileName = file.getOriginalFilename();

????if (file.isEmpty()) {

????return "文件為空!";

????}

????try {

????????excelService.saveExcel(file);

????}catch (Exception e) {

????????e.printStackTrace();

????}

????????return "導入成功!";

????}

}

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末庸娱,一起剝皮案震驚了整個濱河市着绊,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌涌韩,老刑警劉巖畔柔,帶你破解...
    沈念sama閱讀 222,378評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件氯夷,死亡現(xiàn)場離奇詭異臣樱,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,970評論 3 399
  • 文/潘曉璐 我一進店門雇毫,熙熙樓的掌柜王于貴愁眉苦臉地迎上來玄捕,“玉大人,你說我怎么就攤上這事棚放∶墩常” “怎么了?”我有些...
    開封第一講書人閱讀 168,983評論 0 362
  • 文/不壞的土叔 我叫張陵飘蚯,是天一觀的道長馍迄。 經(jīng)常有香客問我,道長局骤,這世上最難降的妖魔是什么攀圈? 我笑而不...
    開封第一講書人閱讀 59,938評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮峦甩,結果婚禮上仁锯,老公的妹妹穿的比我還像新娘耐量。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 68,955評論 6 398
  • 文/花漫 我一把揭開白布摇锋。 她就那樣靜靜地躺著,像睡著了一般岸军。 火紅的嫁衣襯著肌膚如雪霎终。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,549評論 1 312
  • 那天诫欠,我揣著相機與錄音狮腿,去河邊找鬼。 笑死呕诉,一個胖子當著我的面吹牛缘厢,可吹牛的內容都是我干的。 我是一名探鬼主播甩挫,決...
    沈念sama閱讀 41,063評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼贴硫,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了伊者?” 一聲冷哼從身側響起英遭,我...
    開封第一講書人閱讀 39,991評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎亦渗,沒想到半個月后挖诸,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,522評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡法精,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,604評論 3 342
  • 正文 我和宋清朗相戀三年多律,在試婚紗的時候發(fā)現(xiàn)自己被綠了痴突。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,742評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡狼荞,死狀恐怖辽装,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情相味,我是刑警寧澤拾积,帶...
    沈念sama閱讀 36,413評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站丰涉,受9級特大地震影響拓巧,放射性物質發(fā)生泄漏。R本人自食惡果不足惜一死,卻給世界環(huán)境...
    茶點故事閱讀 42,094評論 3 335
  • 文/蒙蒙 一玲销、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧摘符,春花似錦贤斜、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,572評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至带族,卻和暖如春锁荔,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背蝙砌。 一陣腳步聲響...
    開封第一講書人閱讀 33,671評論 1 274
  • 我被黑心中介騙來泰國打工阳堕, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人择克。 一個月前我還...
    沈念sama閱讀 49,159評論 3 378
  • 正文 我出身青樓恬总,卻偏偏與公主長得像,于是被迫代替她去往敵國和親肚邢。 傳聞我的和親對象是個殘疾皇子壹堰,可洞房花燭夜當晚...
    茶點故事閱讀 45,747評論 2 361

推薦閱讀更多精彩內容