天天看點

eclipse如何連接配接mysql_Spring Boot如何連接配接MySQL資料庫詳解!

1. pom.xml添加依賴

org.springframework.bootspring-boot-starter-data-jpamysqlmysql-connector-java   
           

2. application.properties添加資料庫配置

spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
           

如果資料庫連接配接寫成spring.datasource.url=jdbc:mysql://localhost:3306/spring_boot ,由于MySQL版本的問題,可能會有以下的錯誤,在後面加上“?serverTimezone=GMT%2B8”,設定下時區,解決。

eclipse如何連接配接mysql_Spring Boot如何連接配接MySQL資料庫詳解!

設定驅動,spring.datasource.driver-class-name=com.mysql.jdbc.Driver會有下面紅色的警告資訊。說的是

com.mysql.jdbc.Driver

被棄用了,要使用新的驅動

com.mysql.cj.jdbc.Driver1

,改成

com.mysql.cj.jdbc.Driver

以後一切正常。

Loading class com.mysql.jdbc.Driver’. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

eclipse如何連接配接mysql_Spring Boot如何連接配接MySQL資料庫詳解!

3. 添加實體類

@Entity代表這是一個實體類,@Table(name=”user”)用來對應資料庫中的use表,@Id用來表達主鍵,@Column(name=”id”)表明一個id屬性。

@GeneratedValue使主鍵自增,如果還有疑問,可參考@GeneratedValue源碼解析。

package com.example.demo.domain;import java.io.Serializable;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name = "user")public class User implements Serializable {private static final long serialVersionUID = 1L;@[email protected] Long id;@Column(name = "username")private String userName;@Column(name = "password")private String passWord;public User() {super();}public User(String userName, String passWord) {super();this.userName = userName;this.passWord = passWord;}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 getPassWord() {return passWord;}public void setPassWord(String passWord) {this.passWord = passWord;}}
           

4. 添加Dao

Dao層主要用來實作對資料庫的增、删、查、改。 dao隻要繼承JpaRepository類就可以,幾乎可以不用寫方法,可以根據方法名來自動的生産SQL,比如findByUserName 會自動生産一個以 userName 為參數的查詢方法。

package com.example.demo.dao;import org.springframework.data.jpa.repository.JpaRepository;import com.example.demo.domain.User;public interface UserRepository extends JpaRepository<User, Long> {User findByUserName(String userName);}
           

5. 添加Controller

package com.example.demo.controller;import java.util.ArrayList;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import com.example.demo.dao.UserRepository;import com.example.demo.domain.User;@Re[email protected]("user")public class UserController {@Autowiredprivate UserRepository userRepository;@RequestMapping("/getAllUser")@ResponseBodypublic List<User> findAll() {List<User> list = new ArrayList<User>();
       list = userRepository.findAll();return list;}@RequestMapping("/getByUserName")@ResponseBodypublic User getByUserName(String userName) {User user = userRepository.findByUserName(userName);return user;}}
           

工程添加檔案後工程結構圖:

eclipse如何連接配接mysql_Spring Boot如何連接配接MySQL資料庫詳解!

6. 建立資料庫

建立資料庫mysql://localhost:3306/spring_boot ,必須的一個步驟。hibernate雖然會自動建立表,但是資料庫還是要手動建好的。

使用Navicat建立本地資料庫,連接配接名上面右鍵- >建立資料庫 ->填寫資料庫資訊 – > 确定。

eclipse如何連接配接mysql_Spring Boot如何連接配接MySQL資料庫詳解!

在user表中,插入兩條測試資料:

eclipse如何連接配接mysql_Spring Boot如何連接配接MySQL資料庫詳解!

7. 測試

啟動項目。用Postman發送請求進行測試:

http://localhost:8080//user/getAllUser :

eclipse如何連接配接mysql_Spring Boot如何連接配接MySQL資料庫詳解!

http://localhost:8080//user/getByUserName?userName=Turing :

eclipse如何連接配接mysql_Spring Boot如何連接配接MySQL資料庫詳解!