Getting Started Guides
成果图:
原文:
https://spring.io/guides/gs/relational-data-access/#scratch
中文版:https://github.com/SpringForAll/spring-guides-translation/blob/master/translated/accessing-relational-data-using-jdbc-with-spring.md
Accessing Relational Data using JDBC with Spring
This guide walks you through the process of accessing relational data with Spring.
第一步:
自己选的Spring Initializr环境,配置好了导进VS Code 就可以
详细见图:
依赖选择:
第二步:
package com.example.relationaldataaccess;
public class Customer {
private long id;
private String firstName, lastName;
public Customer(long id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%d, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
// getters & setters omitted for brevity
}
第三步:
package com.example.relationaldataaccess;
public class Customer {
private long id;
private String firstName, lastName;
public Customer(long id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%d, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
// getters & setters omitted for brevity
}
@SpringBootApplication
is a convenience annotation that adds all of the following:
-
@Configuration
: Tags the class as a source of bean definitions for the application context. -
@EnableAutoConfiguration
: Tells Spring Boot to start adding beans, based on classpath settings, other beans, and various property settings. -
@ComponentScan
: Tells Spring to look for other components, configurations, and services in thecom.example.relationaldataaccess
package. In this case, there are none.
The main()
method uses Spring Boot’s SpringApplication.run()
method to launch an application.
Spring Boot supports H2 (an in-memory relational database engine) and automatically creates a connection. Because we use spring-jdbc
, Spring Boot automatically creates a JdbcTemplate
. The @Autowired JdbcTemplate
field automatically loads it and makes it available.
This Application
class implements Spring Boot’s CommandLineRunner
, which means it will execute the run()
method after the application context is loaded.
First, install some DDL by using the execute
method of JdbcTemplate
.
Second, take a list of strings and, by using Java 8 streams, split them into firstname/lastname pairs in a Java array.
Then install some records in your newly created table by using the batchUpdate
method of JdbcTemplate
. The first argument to the method call is the query string. The last argument (the array of Object
instances) holds the variables to be substituted into the query where the ?
characters are.
For single insert statements, the insert method of JdbcTemplate is good. However, for multiple inserts, it is better to use batchUpdate . |
Use ? for arguments to avoid SQL injection attacks by instructing JDBC to bind variables. |
Finally, use the query
method to search your table for records that match the criteria. You again use the ?
arguments to create parameters for the query, passing in the actual values when you make the call. The last argument is a Java 8 lambda that is used to convert each result row into a new Customer
object.
Java 8 lambdas map nicely onto single method interfaces, such as Spring’s RowMapper . If you use Java 7 or earlier, you can plug in an anonymous interface implementation and have the method body be the same as the lambda expression’s body. |
第四步:
运行:ctrl + ` 键 弹出 terminal 后 输入: ./mvnw spring-boot:run
就会在terminal里显示结果