1 项目环境搭建请看上篇博客
2 需求分析:
1 用户在登录页面输入用户名和密码进行数据库校验 检验成功则进入成功页面 失败则返回登录页面
3 准备工作
1 新建数据库bishe 在此数据库下新建user表
2 在entity包下新建User类
package com.m.entity; public class User { private int id; private String username; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
4 修改UserController类
package com.m.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.m.entity.User; import com.m.service.UserService; @Controller public class UserController { @Autowired //注入service private UserService userService; /** * 请求路径/user/login * @param user * @return */ @RequestMapping("/user/login") public String login(User user){ User us =userService.login(user); if(us!=null){ return "success"; }else{ return "login"; } } }
4 新建UserService接口
package com.m.service; import com.m.entity.User; public interface UserService { User login(User user); }
5 新建UsServiceImpl 实现UserService接口
package com.m.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.m.entity.User;
import com.m.mapper.UserMapper;
import com.m.service.UserService;
@Service
public class UserServiceImpl implements UserService{
/**
* 注入mapper接口
*/
@Autowired
private UserMapper userMapper;
@Override
public User login(User user) {
return userMapper.login(user);
}
}
6 新建UserMapper接口
package com.m.mapper; import com.m.entity.User; public interface UserMapper { User login(User user); }
7 在src/main/resources 下创建mapper 文件夹 在该文件夹下新建UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.m.mapper.UserMapper"> <select id="login" parameterType="com.m.entity.User" resultType="com.m.entity.User"> select username ,password from user where username =#{username} and password =#{password} </select> </mapper>
8 新建login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/user/login"> 用户名:<input type="text" name="username"></input> 密码:<input type="text" name="password"></input> <input type="submit" value="登录"> </form> </body> </html>
9 在数据库user表中插入一条数据 用户名 zhangsan 密码123
10 页面测试运行截图:
登陆成功跳转到success.jsp页面
密码不正确则进入login.jsp
’
至此简单的用户登录小案例就完成了。