Spring Boot can auto-configure H2 database browser-based console for us. To enable the console we need to set property spring.h2.console.enabled to true (default is false, Spring Boot version 2.0.2).
By default the console can be accessed at URI /h2-console which can be changed by spring.h2.console.path property.
Example
src/main/resources/application.propertiesspring.h2.console.enabled=true
Controller
@Controller
public class PersonController {
@Autowired
private PersonDao dao;
@PostMapping("/person")
public String handlePostRequest(Person person) {
dao.save(person);
return "redirect:/person";
}
@GetMapping("/person")
public String handleGetRequest(Model model) {
model.addAttribute("persons", dao.loadAll());
return "person-view";
}
}
The DAO
@Repository
public class PersonDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void save(Person person) {
String sql = "insert into Person (first_Name, Last_Name, Address) values (?, ?, ?)";
jdbcTemplate.update(sql, person.getFirstName(), person.getLastName(),
person.getAddress());
}
public List<Person> loadAll() {
return jdbcTemplate.query("select * from Person", (resultSet, i) -> {
return toPerson(resultSet);
});
}
private Person toPerson(ResultSet resultSet) throws SQLException {
Person person = new Person();
person.setId(resultSet.getLong("ID"));
person.setFirstName(resultSet.getString("FIRST_NAME"));
person.setLastName(resultSet.getString("LAST_NAME"));
person.setAddress(resultSet.getString("ADDRESS"));
return person;
}
}
public class Person {
private long id;
private String firstName;
private String lastName;
private String address;
.............
}
src/main/resources/schema.sqlCREATE TABLE PERSON(
ID BIGINT PRIMARY KEY AUTO_INCREMENT,
FIRST_NAME VARCHAR(255),
LAST_NAME VARCHAR(255),
ADDRESS VARCHAR(255)
);
Thymeleaf view
src/main/resources/templates/person-view.html<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<h2>Persons</h2>
<h3>Enter new Person:</h3>
<form action="/person" method="post">
<pre>
First Name: <input name="firstName"/> <br/>
Last Name: <input name="lastName"/><br/>
Address: <input th:name="address"/><br/>
<input type="submit" value="Save">
</pre>
</form>
<br>
<h3>Saved Persons</h3>
<table style="width:100%;text-align:center">
<tr><th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
</tr>
<tr th:each="person : ${persons}">
<td th:text="${person.id}"></td>
<td th:text="${person.firstName}"></td>
<td th:text="${person.lastName}"></td>
<td th:text="${person.address}"></td>
</tr>
</table>
</body>
</html>
Main class
@SpringBootApplication
public class SpringBootApplicationMain {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplicationMain.class, args);
}
}
Running application
To try examples, run spring-boot maven plugin (configured in pom.xml of example project below):
mvn spring-boot:run Or run the main method class from IDE.
Output
Access application at /person and submitting various Persons via form:
Accessing H2 web console at /h2-console
By default Spring Boot creates in-memory database with name 'testdb' (see DataSourceProperties#determineDatabaseName() ). Also the complete connection URL is configured in EmbeddedDatabaseConnection enum. H2's URL is set as:
H2(EmbeddedDatabaseType.H2, "org.h2.Driver", "jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"),
After entering values as shown in above screenshot, click on 'Connect'. Now we can see the schema and execute queries:
Example ProjectDependencies and Technologies Used: - Spring Boot 2.0.2.RELEASE
Corresponding Spring Version 5.0.6.RELEASE - spring-boot-starter-thymeleaf : Starter for building MVC web applications using Thymeleaf views.
Uses org.thymeleaf:thymeleaf-spring5 version 3.0.9.RELEASE - spring-boot-starter-web : Starter for building web, including RESTful, applications using Spring
MVC. Uses Tomcat as the default embedded container.
- spring-boot-starter-jdbc : Starter for using JDBC with the HikariCP connection pool.
Uses org.springframework:spring-jdbc version 5.0.6.RELEASE Uses com.zaxxer:HikariCP version 2.7.9 - h2 1.4.197: H2 Database Engine.
- JDK 1.8
- Maven 3.3.9
|