SpringBoot CRUD Operation

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;


@SpringBootApplication
public class SpringBootJpaApplication {
      public static void main(String[] args) {
        SpringApplication.run(SpringBootJpaApplication.class, args);
    } 
     
}

----------------------------------------------------------------------------------------------------------------------
import javax.persistence.Column;
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table;

@Entity 
@Table(name="new1")
public class UserRecord { 
    @Id 
    private int id;
    @Column
    private String name;
    @Column
    private String email; 
    public UserRecord(){} 
    public int getId() { 
        return id; 
    } 
    public void setId(int id) { 
        this.id = id; 
    } 
    public String getName() { 
        return name; 
    } 
    public void setName(String name) { 
        this.name = name; 
    } 
    public String getEmail() { 
        return email; 
    } 
    public void setEmail(String email) { 
        this.email = email; 
    } 
}

------------------------------------------------------------------------------------------------------------------------------


import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 
import java.util.List; 
import java.util.Optional; 
import org.springframework.web.bind.annotation.DeleteMapping;

@RestController 
public class UserController { 
    @Autowired 
    private UserService userService;  
   
     
    @RequestMapping("/getalluser") 
    public List<UserRecord> getAllUser(){ 
        return userService.getAllUsers(); 
    }    
    @RequestMapping(value="/add-user", method=RequestMethod.POST) 
    public void addUser(@RequestBody UserRecord userRecord){ 
        userService.addUser(userRecord); 
    } 
    @RequestMapping(value="/user/{id}", method=RequestMethod.GET) 
    public Optional<UserRecord> getUser(@PathVariable String id){ 
        System.out.println("=========================="+id);
      
        return userService.getUser(Integer.parseInt(id)); 
    } 

   


--------------------------------------------------------------------------------------------------------------------------

import org.springframework.data.repository.CrudRepository; 
public interface UserRepository extends CrudRepository<UserRecord, Integer> { 
}


----------------------------------------------------------------------------------------------------------------------------


import java.util.List; 
import java.util.Optional; 
import java.util.ArrayList; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
@Service 
public class UserService { 
    @Autowired 
    private UserRepository userRepository; 
    public List<UserRecord> getAllUsers(){ 
        List<UserRecord>userRecords = new ArrayList<>(); 
        userRepository.findAll().forEach(userRecords::add); 
        return userRecords; 
    } 
    public Optional<UserRecord> getUser(Integer id){ 
    
        return userRepository.findById(id);
    } 
    public void addUser(UserRecord userRecord){ 
        userRepository.save(userRecord); 
    } 

    void deleteById(Integer id) {
        userRepository.deleteById(id);
    }
   

------------------------------------------------------------------------------------------------------------------------


src/main/resources :       applicatio.properties:
---------------------------------

# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.

# create and drop tables and sequences, loads import.sql
# DataSource settings: set here your own configurations for the database
# connection. In this example we have "netgloo_blog" as database name and
# "root" as username and password.
spring.datasource.url = jdbc:mysql://localhost:3306/demo1
spring.datasource.username = root
spring.datasource.password = root

# Keep the connection alive if idle for a long time (needed in production)
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# Show or not log for each sql query
spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update

# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# Use spring.jpa.properties.* for Hibernate native properties (the prefix is
# stripped before adding them to the entity manager)

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

----------------------------------------------------------------------------------------------------------------------------------


pom.xml
--------------




<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>spring-boot-jpa</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <parent> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-parent</artifactId> 
        <version>2.0.0.BUILD-SNAPSHOT</version> 
        <relativePath/><!-- lookup parent from repository --> 
    </parent> 
 

    <dependencies> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-data-jpa</artifactId> 
        </dependency> 
        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-web</artifactId> 
        </dependency> 
        <dependency> 
            <groupId>org.apache.derby</groupId> 
            <artifactId>derby</artifactId> 
            <scope>runtime</scope> 
        </dependency> 
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
</dependency>

        <dependency> 
            <groupId>org.springframework.boot</groupId> 
            <artifactId>spring-boot-starter-test</artifactId> 
            <scope>test</scope> 
        </dependency> 
            <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency> 
    </dependencies> 
    <build> 
        <plugins> 
            <plugin> 
                <groupId>org.springframework.boot</groupId> 
                <artifactId>spring-boot-maven-plugin</artifactId> 
            </plugin> 
        </plugins> 
    </build> 
    <repositories> 
        <repository> 
            <id>spring-snapshots</id> 
            <name>Spring Snapshots</name> 
            <url>https://repo.spring.io/snapshot</url> 
            <snapshots> 
                <enabled>true</enabled> 
            </snapshots> 
        </repository> 
        <repository> 
            <id>spring-milestones</id> 
            <name>Spring Milestones</name> 
            <url>https://repo.spring.io/milestone</url> 
            <snapshots> 
                <enabled>false</enabled> 
            </snapshots> 
        </repository> 
    </repositories> 
    <pluginRepositories> 
        <pluginRepository> 
            <id>spring-snapshots</id> 
            <name>Spring Snapshots</name> 
            <url>https://repo.spring.io/snapshot</url> 
            <snapshots> 
                <enabled>true</enabled> 
            </snapshots> 
        </pluginRepository> 
        <pluginRepository> 
            <id>spring-milestones</id> 
            <name>Spring Milestones</name> 
            <url>https://repo.spring.io/milestone</url> 
            <snapshots> 
                <enabled>false</enabled> 
            </snapshots> 
        </pluginRepository> 
    </pluginRepositories> 
</project>

Comments

Popular posts from this blog

Filter In Javafx

Kotlin with StandAlone Complier