spring boot +mybatis plue 实现用户统计

简介: spring boot +mybatis plue 实现用户统计

文章目录


代码展示

建立实体类

UserBean

RespBean

建立持久层

ListenerMapper

建立服务层接口

IListenerService

建立服务层实现

ListenerServiceImpl

建立控制层

ListenerController


代码展示


建立实体类


UserBean


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
/**
 * @Author: Ljh
 * @Date: 2021/5/11 16:20
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@AllArgsConstructor
@NoArgsConstructor
@TableName("tab_member")
@ApiModel(value="用户", description="注册时产生的基本账户")
public class UserBean {
    @TableId(type = IdType.AUTO)
    private Integer memberId;
    private Integer memberAccountNumber;
    private String memberPassword;
    private String memberPhone;
}

RespBean


/**
 * @Author: Ljx
 * @Date: 2021/5/13 21:58
 */
public class RespBean {
    private Integer status;
    private String msg;
    private Object obj;
    public static RespBean build() {
        return new RespBean();
    }
    public static RespBean ok(Object obj) {
        return new RespBean(200, null, null);
    }
    public static RespBean ok(String msg) {
        return new RespBean(200, msg, null);
    }
    public static RespBean ok(String msg, Object obj) {
        return new RespBean(200, msg, obj);
    }
    public static  RespBean error(Object obj){
        return new RespBean(500,null,obj);
    }
    public static RespBean error(String msg) {
        return new RespBean(500, msg, null);
    }
    public static RespBean error(String msg, Object obj) {
        return new RespBean(500, msg, obj);
    }
    private RespBean() {
    }
    private RespBean(Integer status, String msg, Object obj) {
        this.status = status;
        this.msg = msg;
        this.obj = obj;
    }
    public Integer getStatus() {
        return status;
    }
    public RespBean setStatus(Integer status) {
        this.status = status;
        return this;
    }
    public String getMsg() {
        return msg;
    }
    public RespBean setMsg(String msg) {
        this.msg = msg;
        return this;
    }
    public Object getObj() {
        return obj;
    }
    public RespBean setObj(Object obj) {
        this.obj = obj;
        return this;
    }
}

建立持久层


ListenerMapper


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
import studio.banner.forumwebsite.bean.UserBean;
/**
 * @Author: Ljx
 * @Date: 2021/5/14 21:05
 */
@Repository
public interface ListenerMapper extends BaseMapper<UserBean> {
}

建立服务层接口


IListenerService


import java.util.List;
/**
 * @Author: Ljx
 * @Date: 2021/5/14 21:00
 */
public interface IListenerService {
    /**
     * 查询所有用户
     * @return
     */
    List<UserBean> selectAllUser();
}

建立服务层实现


ListenerServiceImpl


import java.util.List;
/**
 * @Author: Ljx
 * @Date: 2021/5/14 21:09
 */
@Service
public class ListenerServiceImpl implements IListenerService {
    @Autowired
    private ListenerMapper listenerMapper;
    @Override
    public List<UserBean> selectAllUser() {
        List<UserBean> list = listenerMapper.selectList(null);
        return list;
    }
}

建立控制层


ListenerController


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import studio.banner.forumwebsite.bean.RespBean;
import studio.banner.forumwebsite.bean.UserBean;
import studio.banner.forumwebsite.config.MyHttpSessionListener;
import studio.banner.forumwebsite.service.impl.ListenerServiceImpl;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
 * @Author: Ljx
 * @Date: 2021/5/13 22:02
 */
@RestController
@Api(tags = "统计在线人数", value = "CollectController")
public class ListenerController {
    private static final Logger logger = LoggerFactory.getLogger(ListenerController.class);
    /**
     * 登录
     */
    @Autowired
    private ListenerServiceImpl listenerService;
    @ApiOperation(value = "用户登录", notes = "用户对象不能为空", httpMethod = "POST")
    @PostMapping("/login")
    public RespBean getUser(Integer username, String password, HttpSession session) {
        session.setMaxInactiveInterval(60*30);
        for (UserBean user: listenerService.selectAllUser()) {
            if (username.equals(user.getMemberAccountNumber()) && password.equals(user.getMemberPassword())){
                logger.info("用户【"+username+"】登陆开始!");
                session.setAttribute("loginName",username);
                logger.info("用户【"+username+"】登陆成功!");
                return RespBean.ok("用户【"+username+"】登陆成功!");
            }
        }
        logger.info("用户【"+username+"】登录失败!");
        return RespBean.error("用户【"+username+"】登录失败!");
    }
    /**
     *查询在线人数
     */
    @ApiOperation(value = "查询在线人数", httpMethod = "GET")
    @GetMapping("/online")
    public RespBean online() {
        return  RespBean.ok("当前在线人数:" + MyHttpSessionListener.online + "人");
    }
    /**
     * 退出登录
     */
    @ApiOperation(value = "退出登录", httpMethod = "GET")
    @GetMapping ("/logout")
    public RespBean logout(HttpServletRequest request) {
        logger.info("用户退出登录开始!");
        HttpSession session = request.getSession(false);
        if(session != null){
            session.removeAttribute("loginName");
            session.invalidate();
        }
        logger.info("用户退出登录结束!");
        return RespBean.ok("退出成功");
    }
    /**
     * 判断session是否有效
     * @param httpServletRequest
     * @return String
     */
    @ApiOperation(value = "判断session是否有效",httpMethod = "GET")
    @GetMapping("/getSession")
    public RespBean getSession(HttpServletRequest httpServletRequest) {
        HttpSession session = httpServletRequest.getSession();
        Integer loginName = (Integer) session.getAttribute("loginName");
        if (loginName != null) {
            return RespBean.ok("session有效");
        }
        return null;
    }
}


相关文章
|
1天前
|
SQL Java 数据库连接
SpringBoot整合Mybatis
SpringBoot整合Mybatis
31 2
|
1天前
|
运维 监控 安全
云HIS医疗管理系统源码——技术栈【SpringBoot+Angular+MySQL+MyBatis】
云HIS系统采用主流成熟技术,软件结构简洁、代码规范易阅读,SaaS应用,全浏览器访问前后端分离,多服务协同,服务可拆分,功能易扩展;支持多样化灵活配置,提取大量公共参数,无需修改代码即可满足不同客户需求;服务组织合理,功能高内聚,服务间通信简练。
34 4
|
1天前
|
Java 数据库连接 Spring
Spring 整合mybatis
Spring 整合mybatis
19 2
|
1天前
|
JSON Java 数据格式
nbcio-boot升级springboot、mybatis-plus和JSQLParser后的LocalDateTime日期json问题
nbcio-boot升级springboot、mybatis-plus和JSQLParser后的LocalDateTime日期json问题
|
1天前
|
SQL Java 数据库连接
15:MyBatis对象关系与映射结构-Java Spring
15:MyBatis对象关系与映射结构-Java Spring
31 4
|
1天前
|
XML Java 数据库连接
Spring Boot与MyBatis:整合与实战
【4月更文挑战第29天】在现代的Java Web应用开发中,持久化层框架扮演了至关重要的角色。MyBatis作为一款优秀的持久化框架,被广泛应用于Java开发中。Spring Boot提供了简化开发流程的功能,而与MyBatis的整合也变得更加便捷。
25 0
|
1天前
|
Java 数据库连接 数据库
spring+mybatis_编写一个简单的增删改查接口
spring+mybatis_编写一个简单的增删改查接口
17 2
|
1天前
|
Java 数据库连接 mybatis
【SpringBoot】整合Mybatis
【SpringBoot】整合Mybatis
16 2
|
前端开发 druid Java
SpringBoot 整合 MyBatis
文本是基于MVC前后端分离模式的一个SpringBoot整合MyBatis的项目,不过没有用到前端页面,使用了更方便的Apifox请求工具。SpringBoot+MyBatis使用起来更方便,更舒服。掌握SpingBoot整合MyBatis,要比Spring整合简单的多,少了很多繁琐的配置。......
171 0
SpringBoot 整合 MyBatis
|
XML 数据可视化 Java
Springboot整合mybatis(注解而且能看明白版本)
这篇文章主要讲解Springboot整合Mybatis实现一个最基本的增删改查功能,整合的方式有两种一种是注解形式的,也就是没有Mapper.xml文件,还有一种是XML形式的,我推荐的是使用注解形式,为什么呢?因为更加的简介,减少不必要的错误。
525 0
Springboot整合mybatis(注解而且能看明白版本)