Springboot怎么实现注册功能并且把注册信息添加到数据库,数据库使用mysql,请给我一个完整代码
时间: 2024-03-24 10:37:16 浏览: 59
SpringBoot连接MYSQL数据库并使用JPA进行操作
5星 · 资源好评率100%
好的,下面是一个完整的 Spring Boot 注册功能的实现代码,使用 MySQL 数据库:
1. 创建 User 实体类
```java
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String password;
@Column(nullable = false, unique = true)
private String email;
// 省略 getter 和 setter 方法
}
```
2. 创建 UserRepository 接口
```java
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
```
3. 创建 UserService 类
```java
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User register(User user) throws BusinessException {
// 判断用户名是否已经存在
if (userRepository.findByUsername(user.getUsername()) != null) {
throw new BusinessException("用户名已存在");
}
// 对密码进行加密
user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
// 保存用户信息到数据库
return userRepository.save(user);
}
}
```
4. 创建 UserController 类
```java
@RestController
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/register")
public ResponseEntity<?> register(@RequestBody User user) throws BusinessException {
userService.register(user);
return ResponseEntity.ok().build();
}
}
```
5. 创建 application.properties 配置文件
在 src/main/resources 目录下创建 application.properties 文件,添加以下配置:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=create
```
其中,spring.datasource.url 是数据库的连接地址,mydb 是数据库名,root 是数据库用户名,123456 是数据库密码。
6. 运行程序
启动程序,访问 http://localhost:8080/register,提交注册信息即可将用户信息保存到数据库。
完整代码见下面:
User.java
```java
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String password;
@Column(nullable = false, unique = true)
private String email;
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;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
```
UserRepository.java
```java
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
```
UserService.java
```java
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public User register(User user) throws BusinessException {
// 判断用户名是否已经存在
if (userRepository.findByUsername(user.getUsername()) != null) {
throw new BusinessException("用户名已存在");
}
// 对密码进行加密
user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
// 保存用户信息到数据库
return userRepository.save(user);
}
}
```
UserController.java
```java
@RestController
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/register")
public ResponseEntity<?> register(@RequestBody User user) throws BusinessException {
userService.register(user);
return ResponseEntity.ok().build();
}
}
```
application.properties
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=create
```
注意:以上代码仅供参考,具体实现方式可以根据实际需求进行调整。
阅读全文