mirror of
https://gitee.com/many2many/java-web.git
synced 2025-01-11 22:50:54 +08:00
Compare commits
No commits in common. "8e4db3d25bbe8ab5c8f65eb78d3f65849a9f192b" and "2832dd7d41080adff801af951e6550a9da231312" have entirely different histories.
8e4db3d25b
...
2832dd7d41
@ -171,7 +171,11 @@
|
||||
#### 6.4.1 ORM基本概念
|
||||
#### 6.4.2 常见ORM框架介绍
|
||||
#### 6.4.3 JPA (掌握)
|
||||
|
||||
### 6.5 事务管理(掌握)
|
||||
#### 6.5.1 事务管理的基本概念
|
||||
#### 6.5.2 Spring事务管理器
|
||||
#### 6.5.3 使用@Transactional进行声明式事务管理
|
||||
#### 6.5.4 使用编程式事务管理
|
||||
|
||||
## 7. [项目实践](./docs/chapter07.md)
|
||||
#### 7.1 项目准备
|
||||
|
@ -305,4 +305,79 @@ spring:
|
||||
format_sql: true # 保持原样,用于格式化SQL输出
|
||||
```
|
||||
|
||||
### 6.5 事务管理(掌握)
|
||||
|
||||
#### 6.5.1 事务管理的基本概念
|
||||
|
||||
在计算机科学中,特别是在数据库管理和分布式计算领域,事务(Transaction)是一种基本的操作单位,它确保了一系列操作要么全部成功,要么全部失败。事务的概念是数据库管理系统(DBMS)中用来保证数据完整性和一致性的重要机制之一。
|
||||
|
||||
事务具有以下几个核心特点,通常称为ACID属性
|
||||
|
||||
- **原子性 (Atomicity)**: 事务中的所有操作要么全部完成,要么一个也不完成。
|
||||
- **一致性 (Consistency)**: 事务的执行不会破坏数据的一致性。
|
||||
- **隔离性 (Isolation)**: 事务之间是隔离的,不会相互影响。
|
||||
- **持久性 (Durability)**: 一旦事务提交,它的效果是持久的。
|
||||
|
||||
#### 6.5.2 Spring事务管理器
|
||||
- **定义**: Spring提供了多种事务管理器,如`DataSourceTransactionManager`。
|
||||
- **示例**:
|
||||
```java
|
||||
@Configuration
|
||||
@EnableTransactionManagement
|
||||
public class AppConfig {
|
||||
@Bean
|
||||
public PlatformTransactionManager transactionManager(DataSource dataSource) {
|
||||
return new DataSourceTransactionManager(dataSource);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 6.5.3 使用@Transactional进行声明式事务管理
|
||||
- **定义**: 使用@Transactional注解来声明式地管理事务,简化了事务控制代码。
|
||||
- **示例**:
|
||||
```java
|
||||
@Service
|
||||
@Transactional
|
||||
public class UserService {
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Autowired
|
||||
public UserService(UserRepository userRepository) {
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public void createUser(User user) {
|
||||
userRepository.save(user);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 6.5.4 使用编程式事务管理
|
||||
- **定义**: 通过编程的方式显式地管理事务,适用于需要更细粒度控制事务的情况。
|
||||
- **示例**:
|
||||
```java
|
||||
@Service
|
||||
public class UserService {
|
||||
private final UserRepository userRepository;
|
||||
private final PlatformTransactionManager transactionManager;
|
||||
|
||||
@Autowired
|
||||
public UserService(UserRepository userRepository, PlatformTransactionManager transactionManager) {
|
||||
this.userRepository = userRepository;
|
||||
this.transactionManager = transactionManager;
|
||||
}
|
||||
|
||||
public void createUser(User user) {
|
||||
TransactionDefinition def = new DefaultTransactionDefinition();
|
||||
TransactionStatus status = transactionManager.getTransaction(def);
|
||||
try {
|
||||
userRepository.save(user);
|
||||
transactionManager.commit(status);
|
||||
} catch (Exception e) {
|
||||
transactionManager.rollback(status);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -170,6 +170,12 @@
|
||||
#### 6.4.1 ORM基本概念
|
||||
#### 6.4.2 常见ORM框架介绍
|
||||
#### 6.4.3 JPA (掌握)
|
||||
### 6.5 事务管理(掌握)
|
||||
#### 6.5.1 事务管理的基本概念
|
||||
#### 6.5.2 Spring事务管理器
|
||||
#### 6.5.3 使用@Transactional进行声明式事务管理
|
||||
#### 6.5.4 使用编程式事务管理
|
||||
|
||||
|
||||
## 7. [项目实践](./chapter07.md)
|
||||
### 7.1 项目准备
|
||||
|
@ -1,30 +0,0 @@
|
||||
package com.lk.paopao.controller;
|
||||
|
||||
|
||||
import com.lk.paopao.entity.User;
|
||||
import com.lk.paopao.service.AuthService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/v1/auth")
|
||||
public class AuthController {
|
||||
|
||||
@Autowired
|
||||
private AuthService authService;
|
||||
|
||||
@PostMapping("/register")
|
||||
public Map<String, Object> register(@RequestBody User req) {
|
||||
User user = authService.register(req);
|
||||
|
||||
Map<String,Object> data = Map.of("username",user.getUsername(),"id",user.getId());
|
||||
|
||||
return Map.of("code","0", "msg","success","data", data);
|
||||
|
||||
}
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
package com.lk.paopao.entity;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(unique = true, nullable = false)
|
||||
private String username;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String password;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
@ -1,7 +0,0 @@
|
||||
package com.lk.paopao.repository;
|
||||
|
||||
import com.lk.paopao.entity.User;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package com.lk.paopao.service;
|
||||
|
||||
import com.lk.paopao.entity.User;
|
||||
import com.lk.paopao.repository.UserRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class AuthService {
|
||||
@Autowired
|
||||
private UserRepository userRepository;
|
||||
|
||||
// 注册
|
||||
public User register(User user){
|
||||
return userRepository.save(user);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user