编写程序基于spring+springmvc+mybatis框架的电影票务管理系统实现登录系统
时间: 2023-12-28 07:04:44 浏览: 108
好的,以下是基于Spring+SpringMVC+MyBatis框架的电影票务管理系统实现登录系统的示例代码:
1. 创建用户表
```
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
2. 创建用户DAO
```java
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SqlSessionFactory sqlSessionFactory;
@Override
public User getUserByUsername(String username) {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
return sqlSession.selectOne("getUserByUsername", username);
}
}
}
```
3. 创建用户Service
```java
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public User getUserByUsername(String username) {
return userDao.getUserByUsername(username);
}
}
```
4. 创建登录控制器
```java
@Controller
public class LoginController {
@Autowired
private UserService userService;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String showLoginPage() {
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String doLogin(String username, String password, HttpSession session) {
User user = userService.getUserByUsername(username);
if (user != null && user.getPassword().equals(password)) {
session.setAttribute("user", user);
return "redirect:/home";
} else {
return "login";
}
}
}
```
5. 创建登录页面
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<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>
</body>
</html>
```
6. 配置Spring Security
```xml
<security:http auto-config="true">
<security:intercept-url pattern="/" access="permitAll"/>
<security:intercept-url pattern="/login" access="permitAll"/>
<security:intercept-url pattern="/**" access="isAuthenticated()"/>
<security:form-login login-page="/login" default-target-url="/home"/>
<security:logout logout-success-url="/login"/>
</security:http>
<security:authentication-manager>
<security:authentication-provider>
<security:jdbc-user-service data-source-ref="dataSource"
users-by-username-query="SELECT username, password, 1 FROM user WHERE username=?"
authorities-by-username-query="SELECT u.username, r.name FROM user u, role r, user_role ur WHERE u.id=ur.user_id AND r.id=ur.role_id AND u.username=?"/>
</security:authentication-provider>
</security:authentication-manager>
```
以上是基于Spring+SpringMVC+MyBatis框架的电影票务管理系统实现登录系统的示例代码,希望能够对您有所帮助。
阅读全文