Академический Документы
Профессиональный Документы
Культура Документы
This time I will show you how to use JDBC template in a Spring Boot application. Spring
JdbcTemplate is a powerful mechanism to connect to a relational database and execute SQL
queries.
Let’s start creating a new Spring Boot project with web and jdbc dependencies:
buildscript {
ext {
springBootVersion = '1.5.12.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
group = 'com.jos.dem.springboot.jdbc'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter'
compile 'org.springframework.boot:spring-boot-starter-jdbc'
compile 'org.codehaus.groovy:groovy'
testCompile 'org.springframework.boot:spring-boot-starter-test'
}
compile 'mysql:mysql-connector-java:5.1.34'
Then we will create a person model object that represent person table in a MySQL database.
package com.jos.dem.springboot.jdbc.model
class Person {
Long id
String nickname
String email
Integer ranking
Let’s take a look to the MySQL database called spring_boot_jdbc_template with a person
table.
+----+----------+-----------------------------+---------+
| id | nickname | email | ranking |
+----+----------+-----------------------------+---------+
| 1 | josdem | joseluis.delacruz@gmail.com | 5|
| 2 | erich | eric@email.com | 5|
| 3 | martinv | martin@email.com | 4|
+----+----------+-----------------------------+---------+
Spring provides a template class called JdbcTemplate that makes it easy to work with SQL
relational databases and JDBC, so let’s create a PersonRepository to use it.
package com.jos.dem.springboot.jdbc.repository
import org.springframework.stereotype.Repository
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.core.BeanPropertyRowMapper
import org.springframework.beans.factory.annotation.Autowired
import com.jos.dem.springboot.jdbc.model.Person
@Repository
class PersonRepository {
@Autowired
JdbcTemplate jdbcTemplate
List<Person> findAll() {
jdbcTemplate.query(
'SELECT * FROM person',
BeanPropertyRowMapper.newInstance(Person.class)
)
}
package com.jos.dem.springboot.jdbc.service
import com.jos.dem.springboot.jdbc.model.Person
interface PersonService {
List<Person> getPersons()
package com.jos.dem.springboot.jdbc.service.impl
import org.springframework.stereotype.Service
import org.springframework.beans.factory.annotation.Autowired
import com.jos.dem.springboot.jdbc.model.Person
import com.jos.dem.springboot.jdbc.service.PersonService
import com.jos.dem.springboot.jdbc.repository.PersonRepository
import org.slf4j.Logger
import org.slf4j.LoggerFactory
@Service
class PersonServiceImpl implements PersonService {
@Autowired
PersonRepository personRepository
List<Person> getPersons() {
log.info 'Querying for getting persons'
personRepository.findAll()
}
Finally we get PersonService bean from the spring application context and execute the
getPersons() method:
package com.jos.dem.springboot.jdbc
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.context.ConfigurableApplicationContext
import com.jos.dem.springboot.jdbc.model.Person
import com.jos.dem.springboot.jdbc.service.PersonService
@SpringBootApplication
class JdbcApplication {
spring.datasource.url=jdbc:mysql://localhost/spring_boot_jdbc_template
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
gradle bootRun