Spring Boot连接MySQL数据库
上篇 只需两步!Eclipse+Maven 快速构建第一个 Spring Boot 项目 已经构建了一个 Spring Boot 项目,本文在此基础上进行连接 MySQL 数据库的操作。
1. pom.xml 添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency><dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </pre>
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.Driverspring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
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 {</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> <span style="color: rgba(0, 0, 255, 1)">long</span> serialVersionUID = 1L<span style="color: rgba(0, 0, 0, 1)">; @Id @GeneratedValue </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Long id; @Column(name </span>= "username"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String userName; @Column(name </span>= "password"<span style="color: rgba(0, 0, 0, 1)">) </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> String passWord; </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> User() { </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">(); } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> User(String userName, String passWord) { </span><span style="color: rgba(0, 0, 255, 1)">super</span><span style="color: rgba(0, 0, 0, 1)">(); </span><span style="color: rgba(0, 0, 255, 1)">this</span>.userName =<span style="color: rgba(0, 0, 0, 1)"> userName; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.passWord =<span style="color: rgba(0, 0, 0, 1)"> passWord; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> Long getId() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> id; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setId(Long id) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.id =<span style="color: rgba(0, 0, 0, 1)"> id; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getUserName() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> userName; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setUserName(String userName) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.userName =<span style="color: rgba(0, 0, 0, 1)"> userName; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getPassWord() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> passWord; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> setPassWord(String passWord) { </span><span style="color: rgba(0, 0, 255, 1)">this</span>.passWord =<span style="color: rgba(0, 0, 0, 1)"> passWord; }
}
4. 添加 Dao
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;@RestController
@RequestMapping("user")
public class UserController {@Autowired </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> UserRepository userRepository; @RequestMapping(</span>"/getAllUser"<span style="color: rgba(0, 0, 0, 1)">) @ResponseBody </span><span style="color: rgba(0, 0, 255, 1)">public</span> List<User><span style="color: rgba(0, 0, 0, 1)"> findAll() { List</span><User> list = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList<User><span style="color: rgba(0, 0, 0, 1)">(); list </span>=<span style="color: rgba(0, 0, 0, 1)"> userRepository.findAll(); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> list; } @RequestMapping(</span>"/getByUserName"<span style="color: rgba(0, 0, 0, 1)">) @ResponseBody </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> User getByUserName(String userName) { User user </span>=<span style="color: rgba(0, 0, 0, 1)"> userRepository.findByUserName(userName); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> user; }
}
工程添加文件后工程结构图:
6. 新建数据库
在 user 表中,插入两条测试数据:
7. 测试
http://localhost:8080//user/getByUserName?userName=Turing :