- Spring 5.0 Projects
- Nilang Patel
- 351字
- 2021-07-02 12:34:56
Defining the data access layer – Spring JDBC Template
We have the model classes that reflect the structure of the data in the database that we obtained from the World Bank API. Now we need to develop a data access layer that interacts with our MySQL and populates the data stored in the database into instances of the model classes. We will use the Spring JDBC Template to achieve the required interaction with the database.
First, we need the JDBC driver to connect any Java application with MySQL. This can be obtained by adding the following dependency and version property to our pom.xml:
<properties>
<java.version>1.8</java.version>
<lombok.version>1.16.18</lombok.version>
<hibernate.validator.version>6.0.2.Final</hibernate.validator.version>
<mysql.jdbc.driver.version>5.1.44</mysql.jdbc.driver.version>
</properties>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.jdbc.driver.version}</version>
</dependency>
Anything that comes as <dependency></dependency> goes within the <dependencies></dependencies> list.
Now we need to add a dependency to the Spring core APIs, as well as the Spring JDBC APIs (which contain the JDBC Template) to our pom.xml. A brief intro about these two dependencies is as follows:
- Spring core APIs: It provides us with core Spring features such as dependency injection and configuration model
- Spring JDBC APIs: It provides us with the APIs required to create the DataSource instance and interact with the database
The following code shows the dependency information for the two libraries:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
Along with the preceding two dependencies, we need to add a few more Spring dependencies to assist us in setting up Java-based configurations using annotations (such as @bean, @Service, @Configuration, @ComponentScan, and so on) and dependency injection using annotations (@Autowired). For this, we will be adding further dependencies as follows:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
- Modern JavaScript Web Development Cookbook
- 物聯(lián)網(wǎng)安全:理論、實(shí)踐與創(chuàng)新
- 物聯(lián)網(wǎng)時(shí)代
- 物聯(lián)網(wǎng)與無(wú)線傳感器網(wǎng)絡(luò)
- Yii Application Development Cookbook(Second Edition)
- C/C++串口通信:典型應(yīng)用實(shí)例編程實(shí)踐
- 通信原理及MATLAB/Simulink仿真
- 網(wǎng)絡(luò)AI+:2030后的未來(lái)網(wǎng)絡(luò)
- 中國(guó)互聯(lián)網(wǎng)發(fā)展報(bào)告2021
- 物聯(lián)網(wǎng),So Easy!
- 想象的互動(dòng):網(wǎng)絡(luò)人際傳播中的印象形成
- 從物聯(lián)到萬(wàn)聯(lián):Node.js與樹莓派萬(wàn)維物聯(lián)網(wǎng)構(gòu)建實(shí)戰(zhàn)
- 網(wǎng)絡(luò)信息安全工程技術(shù)與應(yīng)用分析
- 新IP:面向泛在全場(chǎng)景的未來(lái)數(shù)據(jù)網(wǎng)絡(luò)
- ReasonML Quick Start Guide