概述
文章目录
- 代码展示
- 建立实体类
- 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;
}
}
最后
以上就是魁梧狗为你收集整理的spring boot +mybatis plue 实现用户统计代码展示的全部内容,希望文章能够帮你解决spring boot +mybatis plue 实现用户统计代码展示所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复