How to connect to database in Spring JDBC without using XML?

You can use Spring Boot autoconfiguration for Spring JDBC. Spring boot by default uses Java configuration to configure Spring JDBC in Spring-based applications.

How to connect to the database in Spring JDBC without using XML?

In order to use Spring JDBC, add the following dependency:

That's it. Spring boot will auto-configure Spring JDBC using Java without using XML configuration.
Now you are good to use Spring JDBC JdbcTemplate to perform CRUD operations.

For example: Here we are using JdbcTemplate to talk to the database:
package net.guides.springboot2.jdbc.repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import net.guides.springboot2.jdbc.model.Employee;

public class EmployeeJDBCRepository {
    JdbcTemplate jdbcTemplate;

    class EmployeeRowMapper implements RowMapper < Employee > {
        public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
            Employee employee = new Employee();
            return employee;

    public List < Employee > findAll() {
        return jdbcTemplate.query("select * from employees", new EmployeeRowMapper());

    public Optional < Employee > findById(long id) {
        return Optional.of(jdbcTemplate.queryForObject("select * from employees where id=?", new Object[] {
            new BeanPropertyRowMapper < Employee > (Employee.class)));

    public int deleteById(long id) {
        return jdbcTemplate.update("delete from employees where id=?", new Object[] {

    public int insert(Employee employee) {
        return jdbcTemplate.update("insert into employees (id, first_name, last_name, email_address) " + "values(?, ?, ?, ?)",
            new Object[] {
                employee.getId(), employee.getFirstName(), employee.getLastName(), employee.getEmailId()

    public int update(Employee employee) {
        return jdbcTemplate.update("update employees " + " set first_name = ?, last_name = ?, email_address = ? " + " where id = ?",
            new Object[] {
                employee.getFirstName(), employee.getLastName(), employee.getEmailId(), employee.getId()
You can check out the complete example at Spring Boot + Spring JDBC + MySQL CRUD Example