Spring MVC and JDBC CRUD with zero XML

In my other tutorials Spring MVC CRUD example with JDBC Spring MVC and Spring JDBC ExampleSpring MVC and JDBC CRUD Example  I have used XML configurations but in this tutorial I have used only Annotation, so it means it uses zero XML configurations. This example shows how MVC(Model, View, Controller) works in Spring 4.x. In this tutorial you will also find how JDBC works with Spring MVC. We will also see how annotation like @Autowired works in Spring. You will also see how DispatcherServlet, DataSource and ViewResolver are configured in Spring using only Annotation. This example also shows how to integrate Spring 4 with Maven 3.

You may also read https://www.jeejava.com/spring-boot-mvc-with-maven/

For this tutorial we will create maven based web project in Eclipse.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

Prerequisites

The following configurations are required in order to run the application

Eclipse Kepler
JDK 1.8
Tomcat 8
Have maven 3 installed and configured
Spring 4 dependencies in pom.xml

Now we will see the below steps how to create a maven based project in Eclipse Continue reading “Spring MVC and JDBC CRUD with zero XML”

Spring MVC and JDBC CRUD Example

This tutorial Spring MVC CRUD example with JDBC shows how MVC(Model, View, Controller) works in Spring 4.x. In this tutorial you will also find how JDBC works with Spring MVC. We will also see how annotation like @Autowired works in Spring. You will also see how datasource is configured in Spring. This example shows how to integrate Spring 4 with Maven 3. You may also read Spring MVC and Spring JDBC Example

Spring MVC and JDBC CRUD with zero XML

For this tutorial we will create maven based web project in Eclipse.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

Prerequisites

The following configurations are required in order to run the application

Eclipse Kepler
JDK 1.8
Tomcat 8
Have maven 3 installed and configured
Spring 4 dependencies in pom.xml

Now we will see the below steps how to create a maven based project in Eclipse Continue reading “Spring MVC and JDBC CRUD Example”

Spring NamedParameterJdbcTemplate and Collections.singletonMap Example

In this post I will show you how to use NamedParameterJdbcTemplate and Collections.singletonMap to execute query. The NamedParameterJdbcTemplate class adds support for programming JDBC statements using named parameters, as opposed to programming JDBC statements using only classic placeholder (‘?’) arguments. The NamedParameterJdbcTemplate class wraps a JdbcTemplate, and delegates to the wrapped JdbcTemplate to do much of its work. An SqlParameterSource is a source of named parameter values to a NamedParameterJdbcTemplate. The Collections.singletonMap returns an immutable map, mapping only the specified key to the specified value. The returned map is serializable.

For this tutorial we will create a standalone maven project in Eclipse. If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse. Continue reading “Spring NamedParameterJdbcTemplate and Collections.singletonMap Example”

Spring NamedParameterJdbcTemplate and BeanPropertySqlParameterSource Example

In this post I will show you how to use NamedParameterJdbcTemplate and BeanPropertySqlParameterSource to execute query. The NamedParameterJdbcTemplate class adds support for programming JDBC statements using named parameters, as opposed to programming JDBC statements using only classic placeholder (‘?’) arguments. The NamedParameterJdbcTemplate class wraps a JdbcTemplate, and delegates to the wrapped JdbcTemplate to do much of its work. An SqlParameterSource is a source of named parameter values to a NamedParameterJdbcTemplate. The BeanPropertySqlParameterSource class is an implementation of SqlParameterSource interface. This class wraps an arbitrary JavaBean (that is, an instance of a class that adheres to the JavaBean conventions), and uses the properties of the wrapped JavaBean as the source of named parameter values.

For this tutorial we will create a standalone maven project in Eclipse. If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse. Continue reading “Spring NamedParameterJdbcTemplate and BeanPropertySqlParameterSource Example”

Spring NamedParameterJdbcTemplate and MapSqlParameterSource Example

In this post I will show you how to use NamedParameterJdbcTemplate and MapSqlParameterSource to execute query. The NamedParameterJdbcTemplate class adds support for programming JDBC statements using named parameters, as opposed to programming JDBC statements using only classic placeholder (‘?’) arguments. The NamedParameterJdbcTemplate class wraps a JdbcTemplate, and delegates to the wrapped JdbcTemplate to do much of its work. An SqlParameterSource is a source of named parameter values to a NamedParameterJdbcTemplate. The MapSqlParameterSource class is an implementation of SqlParameterSource interface. The MapSqlParameterSource class is a very simple implementation that is simply an adapter around a java.util.Map, where the keys are the parameter names and the values are the parameter values.

For this tutorial we will create a standalone maven project in Eclipse. If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse. Continue reading “Spring NamedParameterJdbcTemplate and MapSqlParameterSource Example”

Get auto-generated id in Spring JDBC using SimpleJdbcInsert

In this post I will show you how to get auto generated id of a newly inserted row in table. Spring provides an easy way to get this auto-generated key using executeAndReturnKey method of SimpleJdbcInsert which is supported Spring JDBC 3 onwards.

For this tutorial we will create a standalone maven project in Eclipse. If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse. Continue reading “Get auto-generated id in Spring JDBC using SimpleJdbcInsert”

Get auto-generated id in Spring JDBC using PreparedStatement

In this post I will show you how to get auto generated id of a newly inserted row in table. Spring provides an easy way to get this auto-generated key using KeyHolder which is supported Spring JDBC 3 onwards.

For this tutorial we will create a standalone maven project in Eclipse. If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

Prerequisites

The following configurations are required in order to run the application

Eclipse Kepler
JDK 1.8
Have maven installed and configured
Spring and MySQL dependencies in pom.xml

Now we will see the below steps how to create a maven based spring project in Eclipse Continue reading “Get auto-generated id in Spring JDBC using PreparedStatement”

Spring @PostConstruct and @PreDestroy Example

This tutorial will show you how we can use @PostConstruct and @PreDestroy in Spring. @PostConstruct and @PreDestroy annotations are generally considered best practice for receiving lifecycle callbacks in a modern Spring application. Using these annotations means that our beans are not coupled to Spring specific interfaces. For more information please refer to http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/beans.html#beans-postconstruct-and-predestroy-annotations

Sometimes we need to load some data or perform initialization work after all necessary properties on the bean have been set by the container. In this example we will load data from database during application startup and set that value to a System property so that the value will be set only once and that value can be used later after retrieving from the System property. Continue reading “Spring @PostConstruct and @PreDestroy Example”

Select example using Spring JdbcTemplate

This tutorial will show you how we can select or fetch data from a database using Spring JdbcTemplate. For this tutorial we will create a standalone maven project in Eclipse.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

Sometimes we need to retrieve one or few records from the database. We can retrieve one or multiple records from database with the help of JdbcTemplate.

Prerequisites

The following configurations are required in order to run the application

Eclipse Kepler
JDK 1.8
Have maven installed and configured
Spring dependencies in pom.xml Continue reading “Select example using Spring JdbcTemplate”

Insert example using Spring JdbcTemplate

This tutorial will show you how we can insert an object or multiple objects into a database using Spring JdbcTemplate. For this tutorial we will create a standalone maven project in Eclipse.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

Sometimes we need to insert or update one or few records in the database. We can insert one or multiple records into database one by one in a traditional approach. Spring provides  insert operation with the help of JdbcTemplate, it inserts or updates records into database in one shot. Continue reading “Insert example using Spring JdbcTemplate”