上篇Spring Boot實戰(一):只需兩步!Eclipse+Maven快速構建第一個Spring Boot項目已經構建了一個Spring Boot項目,本文在此基礎上使用Hibernate進行鏈接MySQL數據庫的操做。html
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
複製代碼
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」,設置下時區,解決。java
設置驅動,spring.datasource.driver-class-name=com.mysql.jdbc.Driver會有下面紅色的警告信息。說的是com.mysql.jdbc.Driver
被棄用了,要使用新的驅動com.mysql.cj.jdbc.Driver1
,改爲com.mysql.cj.jdbc.Driver
之後一切正常。mysql
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.web
@Entity表明這是一個實體類,@Table(name=」user」)用來對應數據庫中的use表,@Id用來表達主鍵,@Column(name=」id」)代表一個id屬性。spring
@GeneratedValue使主鍵自增,若是還有疑問,可參考@GeneratedValue源碼解析。sql
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;
@Id
@GeneratedValue
private 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;
}
}
複製代碼
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);
}
複製代碼
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;
@RestController
@RequestMapping("user")
public class UserController {
@Autowired
private UserRepository userRepository;
@RequestMapping("/getAllUser")
@ResponseBody
public List<User> findAll() {
List<User> list = new ArrayList<User>();
list = userRepository.findAll();
return list;
}
@RequestMapping("/getByUserName")
@ResponseBody
public User getByUserName(String userName) {
User user = userRepository.findByUserName(userName);
return user;
}
}
複製代碼
工程添加文件後工程結構圖:app
新建數據庫mysql://localhost:3306/spring_boot ,必須的一個步驟。hibernate雖然會自動新建表,可是數據庫仍是要手動建好的。dom
使用Navicat新建本地數據庫,鏈接名上面右鍵- >新建數據庫 ->填寫數據庫信息 - > 肯定。spring-boot
在user表中,插入兩條測試數據:
啓動項目。用Postman發送請求進行測試:
http://localhost:8080//user/getAllUser :
http://localhost:8080//user/getByUserName?userName=Turing :