Using the previous DAO and service layer, let us implement a Spring Boot 2.0 application by doing the following steps:
- Open pom.xml and add the following starter for the Spring Boot application:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
Since this starter uses HikariCP as a default connection pooling plugin, including Maven dependencies on HikariCP is now not recommended.
- Add the MySQL connector for the JDBC connectivity:
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency>
- Open the application.properties and add the following properties for the org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration class:
spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/hrs?autoReconnect=true&useSSL=true&serverSslCert=classpath:config/spring5packt.crt spring.datasource.username=root spring.datasource.password=spring5mysql spring.datasource.hikari.connection-timeout=60000 spring.datasource.hikari.maximum-pool-size=5
- Create a new package org.packt.spring.boot.model.data and copy into it all data models from Chapter 8, Reactive Web Applications.
- Copy to org.packt.spring.boot.dao all the DAO interfaces from the previous chapter.
- Copy the DAO implementation classes from Chapter 8, Reactive Web Applications, to a new package org.packt.spring.boot.dao.impl.
- Since Spring Boot does no longer uses lots of annotations, there will be some modifications in the DAO implementation classes. Instead of auto-wiring objects, Spring Boot is capable of injecting @Bean to the constructor of any component. Thus the auto-configured java.sql.DataSource will be automatically injected to a constructor of any DAO implementation with a parameter as such. Follow the modifications of EmplayeeDaoImpl and DepartmentDaoImpl as follows:
@Repository public class DepartmentDaoImpl implements DepartmentDao{ private JdbcTemplate jdbcTemplate; private SimpleJdbcInsert jdbcInsert; public DepartmentDaoImpl(DataSource dataSource) { jdbcTemplate = new JdbcTemplate(dataSource); jdbcInsert = new SimpleJdbcInsert(jdbcTemplate); } // refer to sources } @Repository public class EmployeeDaoImpl implements EmployeeDao { private JdbcTemplate jdbcTemplate; private SimpleJdbcInsert jdbcInsert; public EmployeeDaoImpl(DataSource dataSource) { jdbcTemplate = new JdbcTemplate(dataSource); jdbcInsert = new SimpleJdbcInsert(jdbcTemplate); } // refer to sources }
- Save all files. Then clean and install. Generate the WAR file.