编写程序基于spring+springMVC+mybatis框架的电影票务管理系统实现登录系统
时间: 2023-08-09 15:08:53 浏览: 200
好的,下面是一个简单的电影票务管理系统的登录功能的示例代码,基于Spring、SpringMVC和MyBatis框架:
1. 在pom.xml中添加Spring、SpringMVC和MyBatis依赖:
```xml
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.8</version>
</dependency>
<!-- SpringMVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.8</version>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
```
2. 创建数据库表和实体类
```sql
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`password` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `user` (`id`, `username`, `password`) VALUES
(1, 'admin', 'admin123');
```
```java
public class User {
private Integer id;
private String username;
private String password;
// getter and setter methods
}
```
3. 创建Mapper接口和Mapper.xml文件
```java
public interface UserMapper {
User findByUsernameAndPassword(@Param("username") String username, @Param("password") String password);
}
```
```xml
<mapper namespace="com.example.mapper.UserMapper">
<select id="findByUsernameAndPassword" resultType="com.example.entity.User">
SELECT * FROM user WHERE username = #{username} AND password = #{password}
</select>
</mapper>
```
4. 创建Service接口及其实现类
```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) {
return userMapper.findByUsernameAndPassword(username, password);
}
}
```
5. 创建Controller
```java
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/login")
public String login(@RequestParam("username") String username,
@RequestParam("password") String password,
HttpSession session) {
User user = userService.login(username, password);
if (user == null) {
// 登录失败
return "redirect:/login.jsp";
} else {
// 登录成功
session.setAttribute("user", user);
return "redirect:/index.jsp";
}
}
}
```
6. 配置SpringMVC
```xml
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.example.interceptor.LoginInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
```
7. 编写登录页面和成功页面
login.jsp:
```html
<form action="login" method="post">
<label>用户名:</label>
<input type="text" name="username" /><br>
<label>密码:</label>
<input type="password" name="password" /><br>
<input type="submit" value="登录">
</form>
```
index.jsp:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>欢迎您,${user.username}!</h1>
<a href="logout">退出登录</a>
</body>
</html>
```
以上是基于Spring、SpringMVC和MyBatis框架的简单电影票务管理系统的登录功能的示例代码,仅供参考。
阅读全文