1.创建数据库,填充数据库表格内容
CREATE TABLE users(
id int PRIMARY KEY AUTO_INCREMENT not null,
username VARCHAR(20) not null,
password VARCHAR(20) not null
);
INSERT into users VALUES (2,"ro","root"),(3,"roo","root");
创建数据库和表,插入数据,完成第一步准备
2.做pom.xml文件的配置,依赖配置
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- Servlet API,注意scope为provided --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <!-- 根据需要调整版本 --> <scope>provided</scope> </dependency> <!-- Spring Boot Starter Security --> <!-- <dependency>--> <!-- <groupId>org.springframework.boot</groupId>--> <!-- <artifactId>spring-boot-starter-security</artifactId>--> <!-- </dependency>--> <!-- JPA API --> <dependency> <groupId>javax.persistence</groupId> <artifactId>persistence-api</artifactId> <version>1.0.2</version> </dependency> <!-- Spring Boot Starter Data JPA --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- Spring Boot Starter Web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Boot DevTools --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!-- MySQL Connector Java --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <scope>runtime</scope> </dependency> <!-- Spring Boot Starter Test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.jetbrains</groupId> <artifactId>annotations</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency>
3.编辑配置文件.yml , .properties, .yaml;
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/user_management?useSSL=false&serverTimezone=UTC spring.datasource.username=root spring.datasource.password=root
重定向快捷访问,后缀和路径
spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
4.创建实体类User
pojo,entity包名随意,
package com.example.chat2.pojo; import jakarta.persistence.*; @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String username; private String password; public Integer getId() { return id; } public void setId(Integer 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; } }
5.创建UserRepository 继承JPA方法
package com.example.chat2.repository; import com.example.chat2.pojo.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface UserRepository extends JpaRepository<User, Long> { User findByUsername(String username); }
6.创建UserService接口,以及其实现类
package com.example.chat2.service.serviceimpl; import com.example.chat2.pojo.User; import com.example.chat2.repository.UserRepository; import com.example.chat2.service.UserService; import jakarta.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service @Transactional public class UserServiceImpl implements UserService { @Autowired private UserRepository repository; @Override public User loginUser(String username, String password) { User user=repository.findByUsername(username); if (user!=null&&user.getPassword().equals(password)){ return user; }else { return null; } } @Override public User registerUser(User user) { if (repository.findByUsername(user.getUsername())!=null){ return null; }else { User user1=repository.save(user); return user1; } } }
6.实现Controller方法进行测试
附上b站视频讲解链接mybrother黄建的个人空间-mybrother黄建个人主页-哔哩哔哩视频 (bilibili.com)