Using JPA in SpringBoot

In the last article, we used the JdbcTemplate to access the database. After all, we used the native SQL form. I'm sure that lazy people like me won't consider it..
This record uses JPA to greatly reduce our code volume
First, prepare the SQL file

DROP TABLE IF EXISTS users;

CREATE TABLE users ( 
id INT ( 11 ) PRIMARY KEY AUTO_INCREMENT, 
username VARCHAR ( 255 ) NOT NULL, 
passwd VARCHAR ( 255 ) 
) ENGINE = INNODB DEFAULT CHARSET = utf8;

INSERT users VALUES ( NULL, 'Cui Hua', '123' );
INSERT users VALUES ( NULL, 'Wei Guo Wang', '123' );
INSERT users VALUES ( NULL, 'Li Xiao Hua', '123' );
INSERT users VALUES ( NULL, 'Wang Er Zhu', '123' );
INSERT users VALUES ( NULL, 'Zhao tie Ye', '123' );

The dependence needed this time

    <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>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

User.java

package com.priv.gabriel.entity;

import javax.persistence.*;

/**
 * Created with Intellij IDEA.
 *
 * @Author: Gabriel
 * @Date: 2018-10-08
 * @Desciption:
 */
@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(nullable = false)
    private String username;

    @Column(nullable = false)
    private String passwd;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPasswd() {
        return passwd;
    }

    public void setPasswd(String passwd) {
        this.passwd = passwd;
    }
}

UserController.java

package com.priv.gabriel.controller;

import com.priv.gabriel.entity.User;
import com.priv.gabriel.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
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;

/**
 * Created by Administrator on 2018/10/9.
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public List<User> usersList(){
        return userRepository.findAll();
    }

    @RequestMapping(value = "/" ,method = RequestMethod.PUT)
    public String updateUser(User user){
        if(userRepository.save(user) != null){
            return "Modified success";
        }else{
            return "Modification failed";
        }
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public User selectUserById(@PathVariable long id){
        return userRepository.findById(id).get();
    }

    @RequestMapping(value ="/{id}",method = RequestMethod.DELETE)
    public String deleteUser(@PathVariable long id){
        userRepository.deleteById(id);
        return "Delete successful";
    }

    @RequestMapping(value = "/",method = RequestMethod.POST)
    public String saveUser(User user){
        System.out.println(userRepository);
        if(userRepository.save(user) != null){
            return "New success";
        }else{
            return "New failure";
        }
    }
}

I'm too lazy to write the service layer here. Children who want to study should not learn from me

UserRepository.java

package com.priv.gabriel.repository;

import com.priv.gabriel.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

/**
 * Created with Intellij IDEA.
 *
 * @Author: Gabriel
 * @Date: 2018-10-08
 * @Desciption:
 */
@Repository
public interface UserRepository extends JpaRepository<User,Long>{
}

The biggest advantage of using jpa is that you only need to build a JpaRepository interface, and the rest will be handled by jpa itself. We are only responsible for calling. Back to the topic of springboot

just run

Tags: Java Spring SQL MySQL

Posted on Sat, 07 Dec 2019 19:49:01 -0500 by alwoodman