记录网站访问量

1、在数据库中创建表,存放点击量

2、创建监听器(MyWebListener)实现ServletContextListener接口,当项目启动时通过dao层(ContextMapper)获取点击量,存入ServletContext中。

3、访问主页时增加访问量

4、前端获取点击量或在项目关闭前,将点击量存入数据库(ContextController)

数据库

create table context(
	click_num int
)

dao层

接口

public interface ContextMapper {
    Integer selectClickNum();

    int updateClickNum(Integer clickNum);
}

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="包名.ContextMapper">
    <select id="selectClickNum" resultType="java.lang.Integer">
        select click_num
        from context
    </select>

    <update id="updateClickNum" parameterType="java.lang.Integer">
        update context
        set click_num = #{clickNum, jdbcType=INTEGER}
    </update>
</mapper>

监听器

import cn.javak.mapper.ContextMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

@Component
public class MyWebListener implements ServletContextListener {
    @Autowired
    private ContextMapper contextMapper;
    private static final Logger logger = LoggerFactory.getLogger(MyWebListener.class);
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        logger.info("项目初始化变量");
        ServletContext context = sce.getServletContext();
        Integer clickNum = null;
        try {
            clickNum = contextMapper.selectClickNum();
            context.setAttribute("clickNum",clickNum);
            logger.info("初始化网站点击量"+clickNum);
        }catch (Exception e){
            logger.error("获取点击量失败", e);
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        logger.info("项目终止");
    }
}

访问主页时插入以下代码

//增加点击量
ServletContext context = request.getServletContext();
Integer clickNum = (Integer) context.getAttribute("clickNum");
context.setAttribute("clickNum", ++clickNum);

提供网站访问量接口

import com.alibaba.fastjson.JSONObject;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.WebApplicationContext;

import javax.annotation.PreDestroy;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/context")
public class ContextController {
    @Autowired
    private ContextMapper contextMapper;
    @Autowired
    private WebApplicationContext webApplicationConnect;

    private static final Logger logger = LoggerFactory.getLogger(ContextController.class);

    @GetMapping("/clickNum")
    public JSONObject getClickNum(HttpServletRequest request) {
        ServletContext context = request.getServletContext();
        Integer clickNum = (Integer) context.getAttribute("clickNum");
        JSONObject obj = new JSONObject();
        obj.put("status","success");
        obj.put("msg","获取网站访问量成功");
        obj.put("clickNum", clickNum);
        return obj;
    }

    /**
     * 项目关闭前更新网站点击量
     */
    @PreDestroy
    public void destroy() {
        Integer clickNum = (Integer) webApplicationConnect.getServletContext().getAttribute("clickNum");
        contextMapper.updateClickNum(clickNum);
        logger.info("更新点击量成功" + clickNum);
    }
}

前端ajax

url = function (i) {
    //项目路径
    return "http://localhost:8080/" + i;
};
loadClickNum = function () {
    $.ajax({
        type: 'get',
        url: url("context/clickNum"),
        cache: false,
        dataType: 'json',
        success: function (data) {
            //获取显示点击量元素的id
            $("#clickNum").text(data.obj.clickNum);
        }
    });
};

记录网站访问量_第1张图片

你可能感兴趣的:(记录网站访问量)