-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRelationalDataAccessApplication.java
85 lines (66 loc) · 2.97 KB
/
RelationalDataAccessApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.example.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.jdbc.core.JdbcTemplate;
import io.github.tivrfoa.mapresultset.api.Query;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@SpringBootApplication
public class RelationalDataAccessApplication implements CommandLineRunner {
private static final Logger log = LoggerFactory.getLogger(RelationalDataAccessApplication.class);
@Autowired
JdbcTemplate jdbcTemplate;
@Query
final String listByFirstName = """
SELECT id, first_name, last_name
FROM customers
WHERE first_name = ?
""";
@Override
public void run(String... strings) throws Exception {
createTables();
insertRecords();
queryUsingJdbcTemplate();
System.out.println("------------- Using MapResultSet ----------------------");
Connection connection = jdbcTemplate.getDataSource().getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(listByFirstName);
preparedStatement.setString(1, "Josh");
ResultSet resultSet = preparedStatement.executeQuery();
List<Customer> customers = MapResultSet.listByFirstName(resultSet);
System.out.println(customers);
}
private void queryUsingJdbcTemplate() {
log.info("Querying for customer records where first_name = 'Josh':");
jdbcTemplate.query(
"SELECT id, first_name, last_name FROM customers WHERE first_name = ?", new Object[] { "Josh" },
(rs, rowNum) -> new Customer(rs.getLong("id"), rs.getString("first_name"), rs.getString("last_name"))
).forEach(customer -> log.info(customer.toString()));
}
private void insertRecords() {
// Split up the array of whole names into an array of first/last names
List<Object[]> splitUpNames = Arrays.asList("John Woo", "Jeff Dean", "Josh Bloch", "Josh Long").stream()
.map(name -> name.split(" "))
.collect(Collectors.toList());
// Use a Java 8 stream to print out each tuple of the list
splitUpNames.forEach(name -> log.info(String.format("Inserting customer record for %s %s", name[0], name[1])));
// Uses JdbcTemplate's batchUpdate operation to bulk load data
jdbcTemplate.batchUpdate("INSERT INTO customers(first_name, last_name) VALUES (?,?)", splitUpNames);
}
public void createTables() {
log.info("Creating tables");
jdbcTemplate.execute("DROP TABLE customers IF EXISTS");
jdbcTemplate.execute("CREATE TABLE customers(" +
"id SERIAL, first_name VARCHAR(255), last_name VARCHAR(255))");
}
public static void main(String args[]) {
SpringApplication.run(RelationalDataAccessApplication.class, args);
}
}