SpringBoot Quick Start---Four---Connect and query the database

Database connection and operation
simple demo code has been placed GitHub https://github.com/dmhsq/easy-Spring-Boot-demo
recommended to the chiefs blog learning Jiangnan little rain
database visualization tool Navicat for MySQL

Configure database and Jpa

Insert picture description here

Insert picture description here

New car category

Insert picture description here

@Entity indicates that the class is an entity class and the
corresponding database table is named car

@Entity
public class Car {
    
    

    @Id
    private String carId;

    private String carName;

    private Integer carPrice;
}

Press and hold the ALT INS key in the code to quickly add a Getter Setter

package com.bk.demo.domain;

import javax.persistence.Entity;
import javax.persistence.Id;

/**
 * @Author: 张灿
 * @Time: 2021/2/7 9:48
 */

@Entity
public class Car {
    
    

    @Id
    private String carId;

    private String carName;

    private Integer carPrice;

    public String getCarId() {
    
    
        return carId;
    }

    public void setCarId(String carId) {
    
    
        this.carId = carId;
    }

    public String getCarName() {
    
    
        return carName;
    }

    public void setCarName(String carName) {
    
    
        this.carName = carName;
    }

    public Integer getCarPrice() {
    
    
        return carPrice;
    }

    public void setCarPrice(Integer carPrice) {
    
    
        this.carPrice = carPrice;
    }

    @Override
    public String toString() {
    
    
        return "Car{" +
                "carId='" + carId + '\'' +
                ", carName='" + carName + '\'' +
                ", carPrice=" + carPrice +
                '}';
    }
}

Run the project

Found that our database has one more table
Insert picture description here
Insert picture description here

Because we have configured Jpa, the database will be updated every time it is started.

Write database access interface dao layer

Insert picture description here

We manually add a data
Insert picture description here

Here inherited JpaRepository first parameter Car represents the class name String primary key type


public interface CarRepository extends JpaRepository<Car,String> {
    
    

}

Write a test interface

@RestController
public class DemosController {
    
    

    @Autowired
    private CarRepository carRepository;

    @GetMapping("hello")
    public Object mysqlTest(){
    
    
        return carRepository.findAll();
    }


}

Test browser input interface address to request
Insert picture description here




Friends, I’m not telling you, I want to give you a sincere New Year’s gift. At the beginning of the new year, come to Tencent Cloud + community and share technology with me. Here I have not only received a lot of gifts, but also A sense of identity and accomplishment. Technology needs to spread salary, and we are all spreaders of fire. See you on the top of the mountain, friends, and look forward to seeing you in the clouds!
https://cloud.tencent.com/developer/support-plan?invite_code=guxjsio9ud3l
will be launched later

Front end: js entry vue entry vue development applet, etc.
Back end: java entry springboot entry, etc.
Server: mysql entry server simple instructions cloud server to run the project
python: recommended not to warm up the fire, be sure to see
the use of some plug-ins, etc.

The way of university is also in oneself, study hard, youth
with passion. If you are interested in programming, you can join our qq group to communicate together: 974178910
Insert picture description here

If you have any questions, you can leave a message below, I will reply if you see it

Guess you like

Origin blog.csdn.net/qq_42027681/article/details/113735796