Home >Database >Mysql Tutorial >How to use Spring boot to operate mysql database

How to use Spring boot to operate mysql database

一个新手
一个新手Original
2017-09-11 11:35:332626browse

Software and hardware environment

  • macOS Sierra

  • java 1.8.0_65

  • maven 3.5 .0

  • idea 2017.1.5

##Preface

The previous articlespring boot has been set up development environment and completed the first Hello world program. This article follows the content of the previous article and starts with database-related knowledge points.

Project related configuration

pom.xml
Add dependencies under the dependencies tag, one is spring data jpa, the other is mysql

<p style="margin-bottom: 7px;"><dependency><br/>    <groupId>org.springframework.boot</groupId><br/>    <artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><br/>    <groupId>mysql</groupId><br/>    <artifactId>mysql-connector-java</artifactId></dependency><br/></p>

application. properties
In this configuration file, you need to write the mysql driver, server address, port, database name, user name, password and other information.

spring.datasource.dbcp2.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://127.0.0.1:3306/dbvcms
spring.datasource.username=root
spring.datasource.password=djstava
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

Spring.jpa.hibernate.ddl-auto=update means that when operating the database, all operations are update operations. Here you can also take values ​​such as create and create-drop

spring data jpa

spring data jpa is very simple to operate mysql database, how simple is it? There is no need for you to create any tables and individual fields of the tables. This is a bit like ORM (Object Relational Mapping). Seeing is believing, let’s take a look at the specific implementation steps.

Create entity class
The entity class here refers to the object to be operated on, including its various attributes, which correspond to each field in the data.

LiveChannel.java

package com.xugaoxiang;import org.springframework.beans.factory.annotation.Autowired;import javax.persistence.Entity;import javax.persistence.Id;/**
 * Created by djstava on 10/09/2017.
 */@Entitypublic class LiveChannel {

    @Id
    @Autowired
    private Integer id;    // 频道名称中文
    private String name_chn;    // 频道名称英文
    private String name_eng;    // 频道url
    private String url;    // 频道是否需要播放广告
    private Boolean hasAds;    public String getName_chn() {        return name_chn;
    }    public void setName_chn(String name_chn) {        this.name_chn = name_chn;
    }    public String getName_eng() {        return name_eng;
    }    public void setName_eng(String name_eng) {        this.name_eng = name_eng;
    }    public String getUrl() {        return url;
    }    public void setUrl(String url) {        this.url = url;
    }    public Boolean getHasAds() {        return hasAds;
    }    public void setHasAds(Boolean hasAds) {        this.hasAds = hasAds;
    }
}

The above entity class contains some information about the live broadcast channel.

Interface implementation
Create LiveChannelRepository.java, inherited from JpaRepository, where Interger represents the data type of id

package com.xugaoxiang;
import org.springframework.data.jpa.repository.JpaRepository;/**
 * Created by djstava on 10/09/2017.
 */public interface LiveChannelRepository extends JpaRepository<LiveChannel,Integer> {}

Operation database
Create LiveController ,A RestfulAPI is designed here,/live, which returns all the live broadcast lists in the database.

package com.xugaoxiang;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.util.List;/**
 * Created by djstava on 10/09/2017.
 */@RestControllerpublic class LiveController {
    public LiveController() {
    }    @Autowired
    private LiveChannelRepository liveChannelRepository;    @GetMapping(value = "/live")    public List<LiveChannel> getLiveChannel() {        return liveChannelRepository.findAll();
    }
}

Create database

As shown in the above configuration information, create database dbvcms

How to use Spring boot to operate mysql database

Here we did not create the table structure, start For projects, jpa will automatically create the table for you. The information of each field comes from the file LiveChannel.java

In order to facilitate query, we add 2 records, as shown below

How to use Spring boot to operate mysql database

Test

Everything is ready, start the project, enter

http://localhost:8080/live

How to use Spring boot to operate mysql database

in the browser

##

The above is the detailed content of How to use Spring boot to operate mysql database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn