写功能前,需要要配置好文件
写功能前,需要要配置好文件
写功能前,需要要配置好文件
1、先创建实体类
在这里插入代码片
package com.gm.bean;
/**
* 2020/2/19 14:34
* UserInfo — 实体类,映射数据库字段,字段建议一致
*/
public class UserInfo {
//为什么要封装?
//Spring 容器,去访问对象成员属性的时候后必须是封装类型的,不然就没办法访问!
private int id; //用户ID
private String username;//用户名你
private String password;//用户密码
//get /sset 访问器 alt+insert快捷键
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;
}
//构造方法
public UserInfo() {
}
public UserInfo(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
//toString的方法重写
@Override
public String toString() {
return "UserInfo{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
2、其次dao层接口,这里其实没必要用request,都用session就好
**
* 2020/2/19 16:19
*/
@Controller
@RequestMapping("/user")
public class UserInfoController {
@Autowired
private IUserInfoService userInfoService;
//登陆
@RequestMapping("/doLogin.do")
public String doLogin(String username, String password, HttpServletRequest request){
//1、调用service,查询得到结果,自动补全返回值 Ctrl +Alt +V
UserInfo userInfo = userInfoService.doLogin(username);
Map<String,String> errors=new HashMap<>();
String url;
//2、判断
if (userInfo!=null){
if (userInfo.getPassword().equals(password)){
System.out.println("密码正确,登陆成功");
HttpSession session=request.getSession();
session.setAttribute("userInfo",userInfo);
url="main";
}else {
System.out.println("密码错误");
url="../login";
errors.put("message","密码错误");
}
}else {
System.out.println("用户不存在");
url="../login";
errors.put("message","用户不存在");
}
request.setAttribute("errors",errors);
return url;
}
3、在是写mapper
<?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.gm.dao.IUserInfoDao" >
<!--登陆-->
<select id="doLogin" parameterType="String" resultType="com.gm.bean.UserInfo">
select * from tb_user where username=#{username}
</select>
</mapper>
4、service接口
/**
* 2020/2/19 15:31
* IUserInfoService — 业务逻辑层
*/
public interface IUserInfoService {
UserInfo doLogin(String username);
}
@Service
public class UserInfoService implements IUserInfoService {
//通过对象名
// @Resource(name="u1")
// //通过类名注入
@Autowired
private IUserInfoDao userInfoDao;
@Override
public UserInfo doLogin(String username) {
return userInfoDao.doLogin(username);
}
}
4、最后controller
/**
* 2020/2/19 16:19
*/
@Controller
@RequestMapping("/user")
public class UserInfoController {
@Autowired
private IUserInfoService userInfoService;
//登陆
@RequestMapping("/doLogin.do")
public String doLogin(String username, String password, HttpServletRequest request){
//1、调用service,查询得到结果,自动补全返回值 Ctrl +Alt +V
UserInfo userInfo = userInfoService.doLogin(username);
Map<String,String> errors=new HashMap<>();
String url;
//2、判断
if (userInfo!=null){
if (userInfo.getPassword().equals(password)){
System.out.println("密码正确,登陆成功");
HttpSession session=request.getSession();
session.setAttribute("userInfo",userInfo);
url="main";
}else {
System.out.println("密码错误");
url="../login";
errors.put("message","密码错误");
}
}else {
System.out.println("用户不存在");
url="../login";
errors.put("message","用户不存在");
}
request.setAttribute("errors",errors);
return url;
}
5、登陆页面form表单action地址修改
```java
<form action="${pageContext.request.contextPath}/user/doLogin.do" method="post">
<div class="form-group has-feedback">
<input type="text" name="username" class="form-control"
placeholder="用户名"> <span
class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="password" name="password" class="form-control"
placeholder="密码"> <span
class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div style="color: red">${errors.message}</div>
<div class="row">
<div class="col-xs-8">
<div class="checkbox icheck">
<label><input type="checkbox"> 记住 下次自动登录</label>
</div>
</div>
<!-- /.col -->
<div class="col-xs-4">
<button type="submit" class="btn btn-primary btn-block btn-flat">登录</button>
</div>
<!-- /.col -->
</div>
</form>
6、Tomcat创建(点击左上角加号),启动
Name命名为Tomcat,Application server指定使用的Tomcat
点击Deployment,“+”,添加Artifact…