macOS Sierra
java 1.8.0_65
maven 3.5 .0
idea 2017.1.5
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
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
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; } }
package com.xugaoxiang; import org.springframework.data.jpa.repository.JpaRepository;/** * Created by djstava on 10/09/2017. */public interface LiveChannelRepository extends JpaRepository{}
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 ListgetLiveChannel() { return liveChannelRepository.findAll(); } }
http://localhost:8080/live
##
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!