生活随笔
收集整理的這篇文章主要介紹了
idea+springboot+mongodb的实战使用分享
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
昨天的時候我們先在網(wǎng)上找了測試類,測試了一下mongdb的簡單使用,今天就來實(shí)地在項(xiàng)目中用一用
沒安裝mongodb的參考我上一篇文章:idea+springboot+mongodb的簡單測試使用分享
其實(shí)我們初學(xué)者最好是安裝一個可視化的工具,這樣方便我們管理數(shù)據(jù): 下載地址:MongoDB Compass
我們選擇Msi 安裝版的,然后安裝。 安裝完成以后就可以打開軟件使用了。 默認(rèn)連接我們的27017端口
說完這個,現(xiàn)在來說一下我的業(yè)務(wù)需求:
今日用戶活躍度:登錄了web產(chǎn)品的用戶數(shù)統(tǒng)計(jì) 今日用戶訪問量:用戶每打開一個網(wǎng)站頁面就被記錄1次。用戶多次打開同一頁面,瀏覽量值累計(jì)。 思路: 用戶登陸了就要把用戶id,用戶ip,用戶名字等存入t_user_login,添加一條記錄;同時要在t_user_visits表插入一條記錄 用戶訪問了就t_user_visits表插入一條記錄
開始干! 1.首先在攔截器中攔截,然后獲取我們需要的信息 2.將獲取的信息存入MongoDB 3.然后統(tǒng)計(jì)人數(shù) 1>先復(fù)制出一個攔截器取名為AccessLogInterceptor,然后修改
public class AccessLogInterceptor implements HandlerInterceptor { @Autowired private MongoTemplate mongoTemplate
; @Autowired private SysParamMapper sysParamMapper
; @Autowired private UserMapper userMapper
; private void responseForbiddenResult ( HttpServletResponse httpResponse
) { try { httpResponse
. setContentType ( "application/json;charset=UTF-8" ) ; PrintWriter pw
= httpResponse
. getWriter ( ) ; pw
. write ( JSONObject
. toJSONString ( ResultUtil
. getResult ( ResultCode
. FORBIDDEN
) ) ) ; pw
. flush ( ) ; } catch ( IOException e
) { e
. printStackTrace ( ) ; throw new RuntimeException ( e
) ; } } @Override public boolean preHandle ( HttpServletRequest request
, HttpServletResponse response
, Object handler
) { return true ; } @Override public void postHandle ( HttpServletRequest request
, HttpServletResponse response
, Object handler
, ModelAndView modelAndView
) { String ip
= IPUtil
. getIpAddress ( request
) ; HttpSession session
= request
. getSession ( ) ; String userID
= request
. getSession ( ) . getAttribute ( WebConstants
. CURRENT_USER_ID
) . toString ( ) ; int userId
= Integer
. parseInt ( userID
) ; User user
= userMapper
. get ( userId
) ; if ( "/v1/user/login_sms" . equals ( request
. getRequestURI ( ) ) ) { UserLoginDoc userLoginDoc
= new UserLoginDoc ( ) ; userLoginDoc
. setUserName ( user
. getUsername ( ) ) ; userLoginDoc
. setCreateTime ( new Date ( ) ) ; userLoginDoc
. setLastIp ( ip
) ; userLoginDoc
. setUserId ( user
. getUserId ( ) ) ; mongoTemplate
. save ( userLoginDoc
, "t_user_login" ) ; } UserAccessDoc userAccessDoc
= new UserAccessDoc ( ) ; userAccessDoc
. setUserId ( user
. getUserId ( ) ) ; userAccessDoc
. setAccessUrl ( request
. getRequestURI ( ) ) ; userAccessDoc
. setLastIp ( ip
) ; userAccessDoc
. setUserName ( user
. getUsername ( ) ) ; userAccessDoc
. setCreateTime ( new Date ( ) ) ; mongoTemplate
. save ( userAccessDoc
, "t_user_visits" ) ; } @Override public void afterCompletion ( HttpServletRequest request
, HttpServletResponse response
, Object handler
, Exception ex
) { }
}
實(shí)體類:
@Getter
@Setter
@ToString
public class UserLoginDoc implements Serializable { private Integer userId
; private String userName
; private Date createTime
; private String lastIp
; }
上面就已經(jīng)存到我們的MongoDB中了,現(xiàn)在就要去查詢總數(shù)
Date morning
= DateUtil
. getTimesmorning ( ) ;
Date times
= DateUtil
. getTimesnight ( ) ;
int loginCount
= mongoTemplate
. aggregate ( Aggregation
. newAggregation ( Aggregation
. match ( where ( "createTime" ) . gte ( morning
) . lte ( times
) ) ) , "t_user_login" , BasicDBObject
. class ) . getMappedResults ( ) . size ( ) ;
總結(jié)
以上是生活随笔 為你收集整理的idea+springboot+mongodb的实战使用分享 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。