Home >Database >Mysql Tutorial >How to use Spring boot to operate mysql database
macOS Sierra
java 1.8.0_65
maven 3.5 .0
idea 2017.1.5
<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. propertiesIn 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=trueSpring.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 jpaspring 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 classThe 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 implementationCreate 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 databaseCreate 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 databaseAs shown in the above configuration information, create database dbvcms 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.javaIn order to facilitate query, we add 2 records, as shown below TestEverything is ready, start the project, enter
http://localhost:8080/livein 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!