springmvc+hibnate+mysql实现用户的登入和登出日志,用jsp页面显示
时间: 2023-12-03 14:45:59 浏览: 117
springmvc+hibernate项目基础框架,实现登录查询
3星 · 编辑精心推荐
首先,你需要在项目中引入SpringMVC、Hibernate和MySQL的依赖。可以在pom.xml文件中添加如下依赖:
```xml
<dependencies>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.10</version>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.5.7.Final</version>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
</dependencies>
```
接下来,创建一个用户实体类User,包含用户名、密码和登录时间等属性,并使用Hibernate注解进行映射:
```java
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "username", nullable = false)
private String username;
@Column(name = "password", nullable = false)
private String password;
@Column(name = "login_time", nullable = false)
private Date loginTime;
// 省略getter和setter方法
}
```
接下来创建一个DAO类UserDao,用于对用户实体进行CRUD操作:
```java
@Repository
public class UserDao {
@Autowired
private SessionFactory sessionFactory;
public void save(User user) {
Session session = sessionFactory.getCurrentSession();
session.save(user);
}
public User findByUsername(String username) {
Session session = sessionFactory.getCurrentSession();
Query<User> query = session.createQuery("from User where username=:username", User.class);
query.setParameter("username", username);
return query.uniqueResult();
}
}
```
接下来,创建一个登录控制器LoginController,用于处理用户登录请求:
```java
@Controller
public class LoginController {
@Autowired
private UserDao userDao;
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@RequestParam String username, @RequestParam String password, Model model) {
User user = userDao.findByUsername(username);
if (user == null || !user.getPassword().equals(password)) {
// 登录失败
model.addAttribute("error", "用户名或密码错误");
return "login";
} else {
// 登录成功,记录登录日志
user.setLoginTime(new Date());
userDao.save(user);
// 将用户信息存入session
HttpSession session = request.getSession();
session.setAttribute("user", user);
return "redirect:/index.jsp";
}
}
}
```
最后,创建一个注销控制器LogoutController,用于处理用户注销请求:
```java
@Controller
public class LogoutController {
@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logout(HttpServletRequest request) {
// 清空session
HttpSession session = request.getSession();
session.invalidate();
return "redirect:/login.jsp";
}
}
```
在JSP页面中,可以通过EL表达式获取用户信息并显示在页面上:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>欢迎 ${user.username} 登录</h1>
<a href="${pageContext.request.contextPath}/logout">注销</a>
</body>
</html>
```
以上就是一个简单的SpringMVC+Hibernate+MySQL实现用户登录和注销日志,并在JSP页面中显示用户信息的示例。
阅读全文