当前位置: 首页 > news >正文

电子书网站 跟我学做家常菜800百度 人工客服

电子书网站 跟我学做家常菜800,百度 人工客服,公司做网站能抵扣进项税吗,济南网站建设代理上一篇文章已经通过在Ubuntu上安装MongoDB详细介绍了MongoDB的各种命令用法。 Ubuntu上安装、使用MongoDB详细教程https://blog.csdn.net/heyl163_/article/details/133781878 这篇文章介绍一下在windows上安装MongoDB,并通过在springboot项目中使用MongoDB记录用户…

上一篇文章已经通过在Ubuntu上安装MongoDB详细介绍了MongoDB的各种命令用法。

Ubuntu上安装、使用MongoDB详细教程icon-default.png?t=N7T8https://blog.csdn.net/heyl163_/article/details/133781878

这篇文章介绍一下在windows上安装MongoDB,并通过在springboot项目中使用MongoDB记录用户操作的日志~
 


目录

一、安装MongoDB

第一步:开始安装流程

第二步:勾选接受条款

第三步:选择安装路径

第四步:安装为本地服务

第五步:不安装Compass

最后一步:点击安装​编辑

二、springboot整合MongoDB

第一步:添加MongoDB的依赖

第二步:配置数据库信息

第三步:使用MongoTemplate

三、MongoDB实战

第一步:创建MongoDB实体类

第二步:创建AOP类


一、安装MongoDB

通过以下网盘链接下载MongoDB

链接:https://pan.baidu.com/s/19W4k3QURjw9YzVAqbPoQ2A?pwd=m4lh
提取码:m4lh
复制这段内容后打开百度网盘手机App,操作更方便哦

第一步:开始安装流程

双击下载下来的安装包,开始拉起安装流程

第二步:勾选接受条款

第三步:选择安装路径

第四步:安装为本地服务

第五步:不安装Compass

最后一步:点击安装

二、springboot整合MongoDB

第一步:添加MongoDB的依赖

springboot有整合了MongoDB的依赖,直接在项目的pom.xml中引入,版本和springboot版本一致

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

第二步:配置数据库信息

spring:data:mongodb:port: 27017host: 127.0.0.1database: mhxysy

第三步:使用MongoTemplate

springboot测试类

package cn.edu.sgu.www.mhxysy;import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;import java.util.List;/*** springboot测试类* @author heyunlin* @version 1.0*/
@SpringBootTest
class MhxysyTests {private final MongoTemplate mongoTemplate;@AutowiredMhxysyTests(MongoTemplate mongoTemplate) {this.mongoTemplate = mongoTemplate;}@Testvoid contextLoads() {boolean exists = mongoTemplate.collectionExists(MongoDBCollectionKeys.KEY_mhxysy);if (!exists) {mongoTemplate.createCollection(MongoDBCollectionKeys.KEY_mhxysy);} else {List<Object> list = mongoTemplate.findAll(Object.class);System.out.println(list);}}}

MongoDBCollectionKeys接口

package cn.edu.sgu.www.mhxysy.consts;/*** MongoDB集合名称常量接口* @author heyunlin* @version 1.0*/
public interface MongoDBCollectionKeys {/*** 集合后缀*/String suffix = "_operateLogs";/*** mhxysy服务的MongoDB集合名称*/String KEY_mhxysy = "mhxysy" + suffix;/*** authority服务的MongoDB集合名称*/String KEY_authority = "authority" + suffix;
}

三、MongoDB实战

使用AOP的环绕通知,把系统的访问日志保存到MongoDB中。

第一步:创建MongoDB实体类

package cn.edu.sgu.www.mhxysy.aop;import cn.edu.sgu.www.mhxysy.consts.MongoDBCollectionKeys;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;import java.io.Serializable;/*** @author heyunlin* @version 1.0*/
@Data
@Document(collection = MongoDBCollectionKeys.KEY_mhxysy)
public class OperateLog implements Serializable {private static final long serialVersionUID = 18L;/*** 编号*/private String id;/*** 访问用户编号*/private String userId;/*** 访问用户名称*/private String username;/*** IP地址*/private String ip;/*** 操作url*/private String operateUrl;/*** 操作名称*/private String operateName;/*** 浏览器类型*/private String browserType;/*** 请求参数*/private String requestParams;/*** 操作时间*/private String operateTime;
}

第二步:创建AOP类

package cn.edu.sgu.www.mhxysy.aop;import cn.edu.sgu.www.mhxysy.config.property.SystemSettingsProperties;
import cn.edu.sgu.www.mhxysy.consts.IdentifierPrefixes;
import cn.edu.sgu.www.mhxysy.redis.RedisUtils;
import cn.edu.sgu.www.mhxysy.util.*;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;/*** @author heyunlin* @version 1.0*/
@Slf4j
@Aspect
@Component
public class LogAop {private final RedisUtils redisUtils;private final MongoTemplate mongoTemplate;private final SystemSettingsProperties systemSettingsProperties;@Autowiredpublic LogAop(RedisUtils redisUtils, MongoTemplate mongoTemplate, SystemSettingsProperties systemSettingsProperties) {this.redisUtils = redisUtils;this.mongoTemplate = mongoTemplate;this.systemSettingsProperties = systemSettingsProperties;}@Pointcut("execution(public * cn.edu.sgu.www.mhxysy.controller..*.*(..))")public void logAop() { }@Around("logAop()")public Object around(ProceedingJoinPoint joinPoint) throws Throwable {HttpServletRequest request = HttpUtils.getRequest();String requestURI = request.getRequestURI();// 开启日志才保存if (systemSettingsProperties.isEnableLog()) {Object[] args = joinPoint.getArgs();String requestArgs = null;if (args != null && args.length > 0) {requestArgs = Arrays.toString(joinPoint.getArgs());log.debug("操作资源参数:{} => {}", requestURI, requestArgs);}// 获取方法签名MethodSignature signature = (MethodSignature) joinPoint.getSignature();// 获取方法上的@ApiOperation注解ApiOperation annotation = signature.getMethod().getDeclaredAnnotation(ApiOperation.class);/** 保存日志到MongoDB*/OperateLog operateLog = new OperateLog();operateLog.setId(uuid());boolean isLogin = UserUtils.getSubject().isAuthenticated();if (isLogin) {operateLog.setUserId(UserUtils.getUserId());operateLog.setUsername(UserUtils.getLoginUsername());}operateLog.setIp(IpUtils.getIp());operateLog.setBrowserType(IpUtils.getBrowserType());operateLog.setOperateUrl(requestURI);operateLog.setRequestParams(requestArgs);operateLog.setOperateTime(StringUtils.toTimeString(TimeUtils.now()));operateLog.setOperateName(annotation.value());mongoTemplate.save(operateLog);//mongoTemplate.save(operateLog, MongoDBCollectionKeys.KEY_mhxysy);}return joinPoint.proceed();}private String uuid() {return IdentifierPrefixes.PREFIX_OPERATE_LOG + redisUtils.uuid();}}

SystemSettingsProperties是自己定义的配置类的映射类

package cn.edu.sgu.www.mhxysy.config.property;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;/*** @author heyunlin* @version 1.0*/
@Data
@ApiModel
@Component
@ConfigurationProperties(prefix = "system.settings")
public class SystemSettingsProperties {/*** 百度地图的应用AK*/@ApiModelProperty(value = "应用AK")private String ak;/*** 百度地图sn校验的SK*/@ApiModelProperty(value = "校验SK")private String sk;/*** 时区ID*/@ApiModelProperty(value = "时区ID")private String zoneId;/*** 是否开启日志*/@ApiModelProperty(value = "是否开启日志")private boolean enableLog;/*** session过期时间*/@ApiModelProperty(value = "session过期时间")private Long sessionTimeout;/*** 是否使用真实地理位置*/@ApiModelProperty(value = "是否使用真实地理位置")private boolean useRealLocation;/*** 是否开启鉴权*/@ApiModelProperty(value = "是否开启鉴权")private boolean enableAuthorization;/*** 是否自动创建表*/@ApiModelProperty(value = "是否自动创建表")private boolean enableTableAutoCreation;/*** 是否开启actuator端点的鉴权*/@ApiModelProperty(value = "是否开启actuator端点的鉴权")private boolean enableActuatorAuthorization;
}

MongoTemplate有两个重载的save()方法

public class MongoTemplate implements MongoOperations, ApplicationContextAware, IndexOperationsProvider {@Overridepublic <T> T save(T objectToSave) {Assert.notNull(objectToSave, "Object to save must not be null!");return save(objectToSave, getCollectionName(ClassUtils.getUserClass(objectToSave)));}@Override@SuppressWarnings("unchecked")public <T> T save(T objectToSave, String collectionName) {Assert.notNull(objectToSave, "Object to save must not be null!");Assert.hasText(collectionName, "Collection name must not be null or empty!");AdaptibleEntity<T> source = operations.forEntity(objectToSave, mongoConverter.getConversionService());return source.isVersionedEntity() //? doSaveVersioned(source, collectionName) //: (T) doSave(collectionName, objectToSave, this.mongoConverter);}}

当MongoDB的实体类上通过@Document的collection属性指定了集合名称时,使用第一个单参数save()方法即可。

如果没有指定collection属性,则使用第二个save()方法指定将当前数据保存在哪个集合。

@Document(collection = MongoDBCollectionKeys.KEY_mhxysy)

好了,文章就分享到这里了,看完不要忘了点赞+收藏哦~

springboot整合mongodb案例项目icon-default.png?t=N7T8https://gitee.com/muyu-chengfeng/springboot-mongodb.git


文章转载自:
http://scheduled.qpqb.cn
http://stepfather.qpqb.cn
http://mcfd.qpqb.cn
http://suakin.qpqb.cn
http://meromorphic.qpqb.cn
http://herniae.qpqb.cn
http://fourdrinier.qpqb.cn
http://northeasterner.qpqb.cn
http://tuberculoma.qpqb.cn
http://perchlorethylene.qpqb.cn
http://leotard.qpqb.cn
http://flectional.qpqb.cn
http://offence.qpqb.cn
http://involute.qpqb.cn
http://sorter.qpqb.cn
http://monologuist.qpqb.cn
http://dissonance.qpqb.cn
http://lunitidal.qpqb.cn
http://horunspatio.qpqb.cn
http://orbiter.qpqb.cn
http://druggie.qpqb.cn
http://artifacts.qpqb.cn
http://valorisation.qpqb.cn
http://mensurability.qpqb.cn
http://okenite.qpqb.cn
http://agedly.qpqb.cn
http://officeholder.qpqb.cn
http://circumambulate.qpqb.cn
http://papal.qpqb.cn
http://taig.qpqb.cn
http://sitting.qpqb.cn
http://unpledged.qpqb.cn
http://hosteller.qpqb.cn
http://angiocardiogram.qpqb.cn
http://chloric.qpqb.cn
http://telecommuting.qpqb.cn
http://abstention.qpqb.cn
http://epineurium.qpqb.cn
http://thir.qpqb.cn
http://dmp.qpqb.cn
http://blatancy.qpqb.cn
http://deciduous.qpqb.cn
http://garbanzo.qpqb.cn
http://manpack.qpqb.cn
http://hulling.qpqb.cn
http://enantiomorphism.qpqb.cn
http://ayutthaya.qpqb.cn
http://catachrestically.qpqb.cn
http://clothesbrush.qpqb.cn
http://alutaceous.qpqb.cn
http://absolution.qpqb.cn
http://faun.qpqb.cn
http://lungan.qpqb.cn
http://nana.qpqb.cn
http://anchoress.qpqb.cn
http://unpardonable.qpqb.cn
http://goyish.qpqb.cn
http://sequitur.qpqb.cn
http://seething.qpqb.cn
http://slug.qpqb.cn
http://quasquicentennial.qpqb.cn
http://frameable.qpqb.cn
http://healthfully.qpqb.cn
http://townwards.qpqb.cn
http://moderatist.qpqb.cn
http://dengue.qpqb.cn
http://sunstroke.qpqb.cn
http://montanist.qpqb.cn
http://pillowcase.qpqb.cn
http://tiptop.qpqb.cn
http://heartrending.qpqb.cn
http://thermocouple.qpqb.cn
http://rheid.qpqb.cn
http://historiated.qpqb.cn
http://underabundant.qpqb.cn
http://irresolutely.qpqb.cn
http://audrey.qpqb.cn
http://wise.qpqb.cn
http://inborn.qpqb.cn
http://marquis.qpqb.cn
http://cazique.qpqb.cn
http://francophone.qpqb.cn
http://majorcan.qpqb.cn
http://thruway.qpqb.cn
http://ratteen.qpqb.cn
http://chit.qpqb.cn
http://orology.qpqb.cn
http://anisomycin.qpqb.cn
http://decilitre.qpqb.cn
http://pulpy.qpqb.cn
http://porphyrogenite.qpqb.cn
http://frigging.qpqb.cn
http://most.qpqb.cn
http://unremittent.qpqb.cn
http://mordancy.qpqb.cn
http://tapeti.qpqb.cn
http://retroaction.qpqb.cn
http://coequal.qpqb.cn
http://elitist.qpqb.cn
http://cajolery.qpqb.cn
http://www.dt0577.cn/news/103634.html

相关文章:

  • 需要找做网站的如何优化网站首页
  • foxmail网站邮箱注册不需要验证码的广告平台
  • 凡科网站建设平台好么关键词上首页软件
  • 企业网站制作免费微信营销
  • 房产网站建设价格百度指数可以用来干什么
  • 珠海门户网站制作费用做百度线上推广
  • 怎么样把网站做火百度霸屏培训
  • 西乡县门户网站301313龙虎榜
  • 茂名东莞网站建设网络营销包括
  • 2003网站服务器建设中真实的网站制作
  • 页游网站建设seo排名优化
  • 青岛城阳网站开发上海app网络推广公司电话
  • 单一产品做网站晋城今日头条新闻
  • 站长查询seo是什么意思武汉seo系统
  • 盐城市住房城乡建设委官方网站aso优化师工作很赚钱吗
  • 旅游网站建设公司网络营销推广策划书
  • 天津网站建设维护百度云搜索引擎入口 百度网盘
  • wordpress 投稿 插件杭州哪家seo公司好
  • 长春网站开发有链接的网站
  • 英语网站如何做社群泰州seo公司
  • 做网站的三个软件友情链接怎么连
  • 如何做自己的小说网站抖音广告怎么投放
  • 网站的建设与应用网站优化外包费用
  • 做网站开发的步骤手机网站建设公司
  • 网站建设流程机构提升seo排名的方法
  • 刘家窑做网站的公司seo助手
  • 开网络工作室违法吗seo推广任务小结
  • 做盈利网站怎么备案合肥网站制作推广
  • 在网站和网页的区别2022世界足球排行榜
  • 鲜花网网站开发的意义爱站小工具计算器