Spring Boot 中使用 MySQL

引入依赖

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

JPA( Java Persistence API) 资源库,就是为 POJO (Plain Ordinary Java Object)提供持久化的标准规范,然后将 POJO 通过 ORM(Object-Relational Mapping)持久化到数据库中。

配置文件

spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/spring_boot?characterEncoding=utf8
username: root
password:
jpa:
hibernate:
ddl-auto: update
show-sql: true
database-platform: spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/spring_boot?characterEncoding=utf8
username: root
password:
jpa:
hibernate:
ddl-auto: update
show-sql: true
database-platform: org.hibernate.dialect.MySQL5Dialect

实体建模

这个实体用来与数据库中的表建立映射关系

注解 作用
@Table 指定关联的数据库的表名
@Id 唯一标识
@GeneratedValue 自动生成
@Entity 表明这是一个实体类,如果表名和实体类名相同的话,@Table可以省略

映射

Java中的对象:

package top.baimoc.mocdb.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {
@Id
@GeneratedValue
private Integer id;
private String name;
private Integer age;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
package top.baimoc.mocdb.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class User {
@Id
@GeneratedValue
private Integer id;
private String name;
private Integer age;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}
}

数据库中的User表

engine

实体的持久化

Java中用接口来实现:

package top.baimoc.mocdb.repository;

import top.baimoc.mocdb.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface UserRepository extends JpaRepository<User, Integer>{
public List<User> findByAgepackage top.baimoc.mocdb.repository;

import top.baimoc.mocdb.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface UserRepository extends JpaRepository<User, Integer>{
public List<User> findByAge(Integer age);
}

新建Controller实现增删改查

package top.baimoc.mocdb.controller;

import top.baimoc.mocdb.domain.User;
import top.baimoc.mocdb.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SearchController {

@Autowired
private UserRepository userRepository;

/**
* 查询全部
* @return
*/
@GetMapping("/search")
public List<User> getAll() {
return userRepository.findAll();
}

/**
* 查询单条数据
* @param id
* @return
*/
@GetMapping("/search/{id}")
public User getOne(@PathVariable("id") Integer id) {
return userRepository.findOne(id);
}

/**
* 插入一条数据
* @param name
* @param age
* @return
*/
@PostMapping("/insert")
public User insertOne(@RequestParam("name") String name,
@RequestParam("age") Integer age) {
User user = new User();
user.setName(name);
user.setAge(age);
return userRepository.save(user);
}

/**
* 更新一条数据
* @param id
* @param name
* @param age
* @return
*/
@PutMapping("/updata/{id}")
public User insertOne(@PathVariable("id") Integer id,
@RequestParam("name") String name,
@RequestParam("age") Integer age) {
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);
return userRepository.save(user);
}

/**
* 删除一条数据
* @param id
*/
@DeleteMapping("/delete/{id}")
public void deleteOne(@PathVariable("id") Integer id) {
userRepository.delete(id);
}

/**
* 根据Age查询多条数据
* @param age
* @return
*/
@GetMapping("searchbyage/{age}")
public List<User> searchByAge(@PathVariable("age") Integer age){
package top.baimoc.mocdb.controller;

import top.baimoc.mocdb.domain.User;
import top.baimoc.mocdb.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class SearchController {

@Autowired
private UserRepository userRepository;

/**
* 查询全部
* @return
*/
@GetMapping("/search")
public List<User> getAll() {
return userRepository.findAll();
}

/**
* 查询单条数据
* @param id
* @return
*/
@GetMapping("/search/{id}")
public User getOne(@PathVariable("id") Integer id) {
return userRepository.findOne(id);
}

/**
* 插入一条数据
* @param name
* @param age
* @return
*/
@PostMapping("/insert")
public User insertOne(@RequestParam("name") String name,
@RequestParam("age") Integer age) {
User user = new User();
user.setName(name);
user.setAge(age);
return userRepository.save(user);
}

/**
* 更新一条数据
* @param id
* @param name
* @param age
* @return
*/
@PutMapping("/updata/{id}")
public User insertOne(@PathVariable("id") Integer id,
@RequestParam("name") String name,
@RequestParam("age") Integer age) {
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);
return userRepository.save(user);
}

/**
* 删除一条数据
* @param id
*/
@DeleteMapping("/delete/{id}")
public void deleteOne(@PathVariable("id") Integer id) {
userRepository.delete(id);
}

/**
* 根据Age查询多条数据
* @param age
* @return
*/
@GetMapping("searchbyage/{age}")
public List<User> searchByAge(@PathVariable("age") Integer age){
return userRepository.findByAge(age);
}
}