基于springboot+mybatis-plus的登陆系统开发(含完整代码)
以下是一个简单的Spring Boot + MyBatis-Plus登录系统的示例代码。
- 引入依赖(
pom.xml
):
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-extension</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
- 配置数据库和MyBatis-Plus(
application.properties
):
spring.datasource.url=jdbc:mysql://localhost:3306/your_database?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis-plus.mapper-locations=classpath:/mappers/*.xml
mybatis-plus.type-aliases-package=com.example.demo.model
- 创建实体类(
User.java
):
public class User {
private Long id;
private String username;
private String password;
// getters and setters
}
- 创建Mapper接口(
UserMapper.java
):
@Mapper
public interface UserMapper extends BaseMapper<User> {
// 这里可以添加自定义方法,但对于登录系统,默认的CRUD方法就足够了
}
- 创建Service接口和实现类(
UserService.java
,UserServiceImpl.java
):
public interface UserService {
User login(String username, String password);
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User login(String username, String password) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", username);
queryWrapper.eq("password", password);
return userMapper.selectOne(queryWrapper);
}
}
- 创建Controller(
UserController.java
):
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/login")
public User login(@RequestParam String username, @RequestParam String password) {
return userService.log
评论已关闭