该项目是一个基于JavaWeb、MySQL、SSM(Spring MVC + Spring + MyBatis)框架和Maven构建工具的大学生校园图书管理系统。
以下是部分核心代码:
UserController.java(用户控制器,处理用户相关的请求)
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/login")
public String login(HttpServletRequest request, User user) {
User currentUser = userService.login(user.getUsername(), user.getPassword());
if (currentUser != null) {
request.getSession().setAttribute("currentUser", currentUser);
return "redirect:/book/list";
} else {
request.setAttribute("error", "用户名或密码错误!");
return "login";
}
}
// 其他的用户控制器方法
}
UserService.java(用户服务层)
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User login(String username, String password) {
return userMapper.login(username, password);
}
// 其他的用户服务方法
}
UserMapper.java(用户映射器,与数据库操作有关)
@Mapper
public interface UserMapper {
@Select("SELECT * FROM user WHERE username = #{username} AND password = #{password}")
User login(@Param("username") String username, @Param("password") String password);
// 其他的数据库操作方法
}
User.java(用户实体类)
public class User {
private Integer id;
private String username;
private String password;
// 省略getter和setter方法
}
pom.xml(Maven项目的依赖配置文件)
<project ...>
<!-- 省略其他配置 -->
<dependencies>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.20</version>
</dependency>
<!-- Spring JDBC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.20</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<versi