Skip to content

Commit ca553dd

Browse files
authored
Merge pull request #4 from kento-kotlin-sandbox/future/Add_file_output_logic
Add csv output logic
2 parents d0359ea + 4a2137a commit ca553dd

File tree

6 files changed

+112
-5
lines changed

6 files changed

+112
-5
lines changed

sample.csv

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[email protected],password,Kento75,1993-07-05,25,FALSE,ROLE_ADMIN

src/main/java/com/example/demo/domain/model/repository/jdbc/UserDaoJdbcImpl.java

+7
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ public int deleteOne(String userId) throws DataAccessException {
151151
// Userテーブルの全データをCSVに出力
152152
@Override
153153
public void userCsvOut() throws DataAccessException {
154+
// m_userテーブルのデータを全件取得
155+
String sql = "SELECT * FROM m_user";
156+
157+
// ResultSetExtractorの生成
158+
UserRowCallbackHandler handler = new UserRowCallbackHandler();
154159

160+
// SQLの実行&CSV出力
161+
jdbc.query(sql, handler);
155162
}
156163
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.example.demo.domain.model.repository.jdbc;
2+
3+
import java.io.BufferedWriter;
4+
import java.io.File;
5+
import java.io.FileWriter;
6+
import java.io.IOException;
7+
import java.sql.ResultSet;
8+
import java.sql.SQLException;
9+
10+
import org.springframework.jdbc.core.RowCallbackHandler;
11+
12+
13+
public class UserRowCallbackHandler implements RowCallbackHandler {
14+
15+
@Override
16+
public void processRow(ResultSet rs) throws SQLException {
17+
try {
18+
// ファイル書き込みの準備
19+
File file = new File("sample.csv");
20+
FileWriter fw = new FileWriter(file.getAbsoluteFile());
21+
BufferedWriter bw = new BufferedWriter(fw);
22+
23+
// 取得件数分loop
24+
do {
25+
// ResultSetから値を取得してStringにセット
26+
String str = rs.getString("user_id") + ","
27+
+ rs.getString("password") + ","
28+
+ rs.getString("user_name") + ","
29+
+ rs.getString("birthday") + ","
30+
+ rs.getString("age") + ","
31+
+ rs.getString("marriage") + ","
32+
+ rs.getString("role");
33+
34+
// ファイルに書き込み&改行
35+
bw.write(str);
36+
bw.newLine();
37+
38+
} while(rs.next());
39+
40+
// 書き込み
41+
bw.flush();
42+
// ファイルを閉じる
43+
bw.close();
44+
} catch(IOException e) {
45+
e.printStackTrace();
46+
throw new SQLException(e);
47+
}
48+
}
49+
}

src/main/java/com/example/demo/domain/service/UserService.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package com.example.demo.domain.service;
22

3+
import java.io.IOException;
4+
import java.nio.file.FileSystem;
5+
import java.nio.file.FileSystems;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
38
import java.util.List;
49

510
import org.springframework.beans.factory.annotation.Autowired;
611
import org.springframework.beans.factory.annotation.Qualifier;
12+
import org.springframework.dao.DataAccessException;
713
import org.springframework.stereotype.Service;
814

915
import com.example.demo.domain.model.User;
@@ -13,7 +19,7 @@
1319
@Service
1420
public class UserService {
1521
@Autowired
16-
@Qualifier("UserDaoJdbcImpl4")
22+
@Qualifier("UserDaoJdbcImpl")
1723
UserDao dao;
1824

1925
// insert用メソッド
@@ -79,4 +85,24 @@ public boolean deleteOne(String userId) {
7985

8086
return result;
8187
}
88+
89+
// ユーザー一覧をCSV出力
90+
public void userCsvOut() throws DataAccessException {
91+
// CSV出力
92+
dao.userCsvOut();
93+
}
94+
95+
// サーバーに保存されたファイルを取得する(Byte配列に変換)
96+
public byte[] getFile(String fileName) throws IOException {
97+
// ファイルシステムの取得
98+
FileSystem fs = FileSystems.getDefault();
99+
100+
// ファイル取得
101+
Path p = fs.getPath(fileName);
102+
103+
// ファイルをByte配列に変換
104+
byte[] bytes = Files.readAllBytes(p);
105+
106+
return bytes;
107+
}
82108
}

src/main/java/com/example/demo/login/controller/HomeController.java

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.example.demo.login.controller;
22

3+
import java.io.IOException;
34
import java.util.LinkedHashMap;
45
import java.util.List;
56
import java.util.Map;
67

78
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.http.HttpHeaders;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.http.ResponseEntity;
812
import org.springframework.stereotype.Controller;
913
import org.springframework.ui.Model;
1014
import org.springframework.web.bind.annotation.GetMapping;
@@ -153,8 +157,28 @@ public String postLogout() {
153157

154158
// ユーザー一覧のCSV出力
155159
@GetMapping("/userList/csv")
156-
public String getUserListCsv(Model model) {
157-
// TODO:あとで実装する
158-
return getUserList(model);
160+
public ResponseEntity<byte[]> getUserListCsv(Model model) {
161+
162+
//ユーザーを全件取得して、CSVをサーバーに保存する
163+
userService.userCsvOut();
164+
165+
byte[] bytes = null;
166+
167+
try {
168+
169+
//サーバーに保存されているsample.csvファイルをbyteで取得する
170+
bytes = userService.getFile("sample.csv");
171+
172+
} catch (IOException e) {
173+
e.printStackTrace();
174+
}
175+
176+
//HTTPヘッダーの設定
177+
HttpHeaders header = new HttpHeaders();
178+
header.add("Content-Type", "text/csv; charset=UTF-8");
179+
header.setContentDispositionFormData("filename", "sample.csv");
180+
181+
//sample.csvを戻す
182+
return new ResponseEntity<>(bytes, header, HttpStatus.OK);
159183
}
160184
}

src/main/resources/templates/login/userList.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ <h1>ユーザー一覧</h1>
3434
<!-- 更新・削除処理の結果表示用 -->
3535
<label class="text-info" th:text="${result}">結果表示</label><br/>
3636
<!-- CSV出力用のリンク -->
37-
<label class="btn btn-primary" th:href="@{'/userList/csv'}">CSV出力</label>
37+
<a class="btn btn-primary" th:href="@{'/userList/csv'}">CSV出力</a>
3838
</div>
3939
</body>
4040
</html>

0 commit comments

Comments
 (0)