目 录
①在控制器部分增加logout部分代码,清除session记录即可
1、登陆
①dao层代码
UserMapper.java
package com.gec.newsproject.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gec.newsproject.entity.Users;
import org.apache.ibatis.annotations.Mapper;
public interface UsersMapper extends BaseMapper<Users>{
}
②service接口
UserService.java
package com.gec.newsproject.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.gec.newsproject.entity.Users;
public interface UserService extends IService<Users>{
Users login(String username,String password);
}
③接口实现类
UserServiceImpl.java
package com.gec.newsproject.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.newsproject.dao.UsersMapper;
import com.gec.newsproject.entity.Users;
import com.gec.newsproject.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@Service
public class UserServiceImpl extends ServiceImpl<UsersMapper, Users> implements UserService {
@Autowired
UsersMapper usersMapper;
@Override
public Users login(String username, String password) {
QueryWrapper<Users> wrapper = new QueryWrapper<>();
wrapper.eq("uname",username);
wrapper.eq("upwd",password);
Users users = usersMapper.selectOne(wrapper);
return users;
}
}
④控制器代码
UsersController.java
package com.gec.newsproject.controller;
import com.gec.newsproject.entity.Users;
import com.gec.newsproject.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
@Controller
@RequestMapping("/users")
public class UsersController {
@Autowired
UserService userService;
@RequestMapping("/login")
public String login(String userName, String password, HttpSession session){
Users login = userService.login(userName, password);
if (login != null) {
System.out.println("登录成功!!!!!");
session.setAttribute("loginUser",login);
}
return "redirect:/index.html";
}
}
⑤效果图
2、退出登录
①在控制器部分增加logout部分代码,清除session记录即可
在前面Hellocontroller.java的基础上在后面加上以下代码
@RequestMapping("/logout")
public String logout(HttpSession session){
session.invalidate();
return "redirect:/index.html";
}
②效果图