Skip to main content

Spring Boot Gradle MVC sample CRUD project

This is a sample Spring Boot Application that uses 
  1. JDBC
  2. Mysql
  3. JPA
  4. MVC
  5. Gradle

Create Basic CRUD for Person Entity with JPA and Mysql

Project Structure 

build.gradle


View Maven Dependencies (pom.xml) here.

buildscript {
ext {
springBootVersion = '1.2.5.RELEASE'
}
repositories {
maven { url "http://repo.spring.io/libs-milestone" }
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("io.spring.gradle:dependency-management-plugin:0.5.1.RELEASE")
}
}

apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'


war {
baseName = 'springboot'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.7
targetCompatibility = 1.7

repositories {
mavenCentral()
maven { url "http://repo.spring.io/libs-milestone" }
}

configurations {
providedRuntime
}

dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("javax.servlet:jstl:1.2")
runtime("mysql:mysql-connector-java")
compile("org.springframework.boot:spring-boot-starter-jdbc")
compile ("org.springframework.boot:spring-boot-starter-web")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper'
}


eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7'
}
}

task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}

AppConfig.java

package com.ekiras.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

/**
* Created by ekansh on 12/7/15.
*/
@Configuration
public class AppConfiguration extends WebMvcConfigurerAdapter{

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(org.springframework.web.servlet.view.JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}

}

Person.java

package com.ekiras.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

/**
* Created by ekansh on 12/7/15.
*/
@Entity
public class Person {

@Override
public String toString(){
return this.name;
}

@Id
@GeneratedValue
@Column(name = "id")
private Long id;

@Column(name = "name")
private String name;

@Column(name = "email", nullable = false, unique = true)
private String email;

@Column(name = "password")
private String password;

@Column(name = "mobile")
private String mobile;

private transient String confirmPassword;

// Getters and Setters
}

PersonRepository.java

package com.ekiras.repository;

import com.ekiras.domain.Person;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

/**
* Created by ekansh on 14/7/15.
*/
@Repository
public interface PersonRepository extends CrudRepository<Person,Long> {

}

PersonService.java

package com.ekiras.service;

import com.ekiras.dao.PersonDAO;
import com.ekiras.domain.Person;
import com.ekiras.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* Created by ekansh on 13/7/15.
*/
@Service
public class PersonService {

@Autowired
private PersonDAO personDAO;

@Autowired
private PersonRepository personRepository;

public Object findAll(){
return personRepository.findAll();
}

public Person findById(Long id){
return personRepository.findOne(id);
}

public Person save(Person person){
return personRepository.save(person);
}

}

PersonController.java

package com.ekiras.controller;

import com.ekiras.domain.Person;
import com.ekiras.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
* Created by ekansh on 12/7/15.
*/

@Controller
@RequestMapping(value = {"","/person"})
public class PersonController {

@Autowired
private PersonService personService;


@RequestMapping(value = {"/","index"})
public String index(Model model){
System.out.println(personService.findAll());
model.addAttribute("persons", personService.findAll());
return "person/index";
}

@RequestMapping(value = "create")
public String create(){
return "person/create";
}

@RequestMapping(value = "save", method = RequestMethod.POST)
public String save(Person person){
personService.save(person);
return "redirect:index";
}

@RequestMapping(value = "edit/{id}")
public String edit(@PathVariable Long id, Model model){
model.addAttribute("person",personService.findById(id));
return "person/edit";
}


@RequestMapping(value = "update",method = RequestMethod.POST)
public String update(Person person){
personService.save(person);
return "redirect:index";
}


@RequestMapping(value = "/test")
public String test(){
return " hello world";
}


}

With all the above configurations your code is ready to do the CRUD on Person Entity.

Run the following command to run the project.

gradle bootRun



Comments