1.项目结构

image-20210612144110219

2.添加依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.fizzyelf</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>JPATest</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- JPA依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>

<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<!-- 测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

3.application.properties

1
2
3
4
5
6
7
8
9
10
11
12
server.port=8443

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/hibernatetest?serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=Good18882336322

spring.jpa.hibernate.ddl-auto=update
# 开启控制台SQL打印
spring.jpa.show_sql = true
# 开启SQL格式化
spring.jpa.properties.hibernate.format_sql = true

4.新建一个user表

image-20210612144304513

5.idea连接数据库

image-20210612144659439

然后在弹出框输入数据库相关信息,记得加上时区,最后点击尝试连接,连接成功后,点击确定,这样idea就和数据库连接上了。

image-20210612144911231

image-20210612145110973

6.编写实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.fizzyelf.demo.Entity;

import lombok.Data;

import javax.persistence.*;

@Data
@Entity
@Table(name="user")
public class User {
@Id // 主键
@GeneratedValue(strategy=GenerationType.IDENTITY)//主键生成策略
@Column(name="id")//数据库字段名
private Integer id;

@Column(name="user_name")//数据库字段名
private String userName;

@Column(name="password")//数据库字段名
private String password;
}

此时idea还解析不到数据库,我们点击“分配数据源”

image-20210612145458631

将数据源选择刚刚我们添加的数据库,这样idea就可以解析到数据库中的表,和字段了。

image-20210612145518670

7.repository 数据层

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.fizzyelf.demo.Dao;

import com.fizzyelf.demo.Entity.User;
import org.springframework.data.jpa.repository.JpaRepository;


/**
* 参数一 T :当前需要映射的实体
* 参数二 ID :当前映射的实体中的ID的类型
*
*/
public interface UserRepository extends JpaRepository<User,Integer> {
}

JpaRepository接口中定义了许多方法,其中就包括了基本的增删改查的方法。

8.Service业务层

接口UserService

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.fizzyelf.demo.Service;

import com.fizzyelf.demo.Entity.User;

import java.util.List;

public interface UserService {
/**
* 插入一个用户
*/
User insertUser(User user);

/**
* 删除用户
*/
void deleteUser(int id);

/**
* 通过id查询用户
*/
User findUserById(int id);

/**
* 查询所有用户
*/
List<User> findAllUser();

/**
* 修改用户
*/
User updateUser(User user);
}

实现类UserServiceImpl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.fizzyelf.demo.Service.impl;

import com.fizzyelf.demo.Dao.UserRepository;
import com.fizzyelf.demo.Entity.User;
import com.fizzyelf.demo.Service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
@Resource
private UserRepository userRepository;

@Override
public User insertUser(User user){
return userRepository.save(user);
}

@Override
public void deleteUser(int id) {
userRepository.deleteById(id);
}

@Override
public User findUserById(int id){
return userRepository.findById(id).orElse(null);
}

@Override
public List<User> findAllUser() {
return userRepository.findAll();
}

@Override
public User updateUser(User user) {
return userRepository.save(user);
}
}

9.Controller控制层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.fizzyelf.demo.Controller;


import com.fizzyelf.demo.Entity.User;
import com.fizzyelf.demo.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;

/**
* 新增用户
*/
@PostMapping("")
public User addUser(@RequestBody User user){
return userService.insertUser(user);
}

/**
* 根据id删除
*/
@DeleteMapping ("/{id}")
public void deleteUser(@PathVariable("id") int id){
userService.deleteUser(id);
}

/**
* 根据id查找
*/
@GetMapping ("/{id}")
public User findUser(@PathVariable("id") int id){
return userService.findUserById(id);
}

/**
* 查询所有用户
*/
@GetMapping ("")
public List<User> findAllUser(){
return userService.findAllUser();
}

/**
* 修改用户
*/
@PutMapping ("")
public User updateUser(@RequestBody User user){
return userService.updateUser(user);
}
}

10.测试

新增一条数据

image-20210612151348013

查询数据

image-20210612151436185

image-20210612151506271

修改

image-20210612151709221

删除

image-20210612151800483