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

wordpress importer东莞网站建设优化

wordpress importer,东莞网站建设优化,网站建设有哪些问题,小程序外包公司出名写在前面:最近在公司实习,需要完成一个实习任务。这个任务用的是SSH框架,数据库需要使用mongoDB完成。由于刚接触MongoDB,所以不是很熟练,在网上查找了大量的资料,许多都是抄来抄去的,运行一堆错误。如今&a…

写在前面:最近在公司实习,需要完成一个实习任务。这个任务用的是SSH框架,数据库需要使用mongoDB完成。由于刚接触MongoDB,所以不是很熟练,在网上查找了大量的资料,许多都是抄来抄去的,运行一堆错误。如今,我的工作任务已经完成,现在写下此篇,希望后来的打工人少一点痛苦!

首先,我有一定的数据库基础,但对于mongo还是一无所知的小白。所以有以下疑问:

1. 数据库怎么连接?(工具类封装)

2. 数据库连接后怎么进行CRUD?(API调用肯定和命令行有区别)

3. java实体类怎么才能入mongo的库?(很重要)

4. mongo查寻出来的数据,怎么映射到实体类中,转换成Java的数据结构?(很重要)

1. 数据库的连接

首先我是win10 系统,用的最新版的mongo数据库,这里就不介绍怎么安装mongo了。其次是,我这是纯净的maven项目,没有用springboot之类的,所以就从最原生的来。

(1)导入maven坐标

        <!--MongoDB--><dependency><groupId>org.mongodb</groupId><artifactId>mongodb-driver-sync</artifactId><version>4.4.1</version></dependency>

注意:我在查阅资料的时候发现,mongo3.0+的创建数据库连接方式和mongo4.0+有区别。这里只说4.0+的。

(2)创建工具类

import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.codecs.pojo.PojoCodecProvider;public class MongoDBUtil {private static final String CONNECTION_STRING = "mongodb://localhost:27017";private static final String DATABASE_NAME = "weeklyTask";//不通过认证获取连接数据库对象public static MongoDatabase getConnect(){//连接到 mongodb 服务MongoClient mongoClient = MongoClients.create(CONNECTION_STRING);;//连接到数据库//返回连接数据库对象return mongoClient.getDatabase(DATABASE_NAME);}//处理pojo和Bson之间的编码问题public static CodecRegistry getCodecRegistry(){//进行编码处理return CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(),CodecRegistries.fromProviders(PojoCodecProvider.builder().automatic(true).build()));}
}

注意:这个工具类里面使用的本地无认证模式的。因为我没有设置mongo的密码。

2. CRUD操作

这里的CRUD操作,我就和上述中的2,3,4问题一起说了。直接上案例:

Pojo实体类创建

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {private String uid;private String name;private String number;private String password;private int role;
}
获取集合对象
        // 获取集合对象
MongoCollection<User> collection =  MongoDBUtil.getConnect().getCollection("user", User.class).withCodecRegistry(MongoDBUtil.getCodecRegistry());

PS:其实这一步就是做Java实体类和mongo文档数据映射的!因为 MongoCollection<Document>,泛型默认是Document的。这里我们直接指定是User的,然后使用了编码转换withCodecRegistry(MongoDBUtil.getCodecRegistry())。这样就可以实现互转了。后面通过collection调用的方法中的Document都可以具体的写成User了。

新增操作

(1)单个新增:

collection.insertOne(new User("1","ceshi","ceshi","123456",0));
collection.insertOne(new User("2","zhuguan","zhuguan","123456",1));

(2)批量新增:

        List<User> userList = Arrays.asList(new User("1","ceshi","ceshi","123456",0),new User("2","zhuguan","zhuguan","123456",1));collection.insertMany(userList);

PS:我们完成上述操作后,执行新增操作,不用关注mongoDB中是否存在这个集合(表),他会自己去创建这个表的,并且将结构和数据映射上去。

修改操作

(1)修改单个数据

Bson filter = Filters.eq("uid", "1");
Bson update = Updates.set("name", "ceshi");
collection.updateOne(filter, update);

(2)修改多个数据

        Bson filter = Filters.eq("uid", "1");Bson update = Updates.combine(Updates.set("name", "Test"),Updates.set("password", "ceshi"));collection.updateOne(filter, update);

(3)批量修改

Bson filter = Filters.eq("password", "123456");
Bson update = Updates.set("password", "147258");
UpdateResult result = collection.updateMany(filter, update);

(4)整行修改

场景:有时候前台直接传过来的是一个对象,你不能确定它具体修改的属性是啥。

        // 构造Bson对象,用于匹配需要更新的文档Bson filter = Filters.eq("uid", "1");// 执行替换操作collection.replaceOne(filter, new User(...));

PS:mongo的数据格式BSON类型的,他的写法和Json类似。所以写过滤条件需要使用BSON。

删除操作

(1)单个删除

        Bson filter = Filters.eq("uid", "1");collection.deleteOne(filter);

(2)批量删除(删除密码所有为:‘123456’的)

Bson filter = Filters.eq("password", "123456");
collection.deleteMany(filter);

查询操作

(1)单个查询

        Bson filter = Filters.eq("uid",uid);FindIterable<User> users= collection.find(filter);User user = users.first();

(2)多个查询(List<User>)

        Bson filter = Filters.eq("uid","1");FindIterable<User> users= collection.find(filter);List<User> userList = new ArrayList<>();//防止资源和内存泄漏,自动关闭try (MongoCursor<Weekly> iterator = users.iterator()){while (iterator.hasNext()){userList.add(iterator.next());}}catch (Exception e){logger.error("查询错误:" + e.getMessage());}

PS:当使用MongoCursor遍历查询结果时,一定要手动关闭它,否则会造成资源泄露。可以使用try-with-resources语句块来自动关闭MongoCursor。当然数据量小,不写也不影响。

(3)分页查询(List<User>)

        page = (page == 0) ? 1 : page;// 当前页数pageSize = (pageSize == 0) ? 10 : pageSize; // 每页数据量Bson filter = Filters.eq("uid","1");FindIterable<User> users= collection.find(filter).sort(Sorts.descending("startTime"))                                     .skip((page -1) * pageSize).limit(pageSize);;List<User> userList = new ArrayList<>();//防止资源和内存泄漏,自动关闭try (MongoCursor<Weekly> iterator = users.iterator()){while (iterator.hasNext()){userList.add(iterator.next());}}catch (Exception e){logger.error("查询错误:" + e.getMessage());}

(4)其他查询

查询某个字段大于特定值的数据:

Bson filter = Filters.gt("age", 18);
FindIterable<Document> result = collection.find(filter);

查询某个字段小于特定值的数据:

Bson filter = Filters.lt("age", 18);
FindIterable<Document> result = collection.find(filter);

查询某个字段包含特定值的数据:

Bson filter = Filters.in("gender", Arrays.asList("female", "unknown"));
FindIterable<Document> result = collection.find(filter);

PS:常见的查询还有很多,如:范围查询、分页查询、嵌套查询、空查询、正则表达式查询等等。有空再开博客写吧。

以上就是Idea中用java快速上手MongoDB的过程了,基本的CRUD感觉足够了。个人感觉实现业务的方式有很多种,这种可能不是最好的,但我觉得还挺方便的。本篇就大概做了快速入门,有时间写个专栏,把这段时间来的问题梳理梳理。


文章转载自:
http://bavaria.rmyt.cn
http://macrodontia.rmyt.cn
http://wolfhound.rmyt.cn
http://turbopump.rmyt.cn
http://iceberg.rmyt.cn
http://correligionist.rmyt.cn
http://intrafallopian.rmyt.cn
http://glossology.rmyt.cn
http://whiteboard.rmyt.cn
http://concurrent.rmyt.cn
http://underwork.rmyt.cn
http://pastry.rmyt.cn
http://endosymbiosis.rmyt.cn
http://zack.rmyt.cn
http://bionomy.rmyt.cn
http://added.rmyt.cn
http://brechtian.rmyt.cn
http://okenite.rmyt.cn
http://crossbusing.rmyt.cn
http://zn.rmyt.cn
http://adieux.rmyt.cn
http://swine.rmyt.cn
http://jesuitry.rmyt.cn
http://toolbar.rmyt.cn
http://computerese.rmyt.cn
http://technicology.rmyt.cn
http://thermophil.rmyt.cn
http://unaired.rmyt.cn
http://dictyostele.rmyt.cn
http://adriamycin.rmyt.cn
http://bonobo.rmyt.cn
http://trifold.rmyt.cn
http://numeracy.rmyt.cn
http://equanimously.rmyt.cn
http://edd.rmyt.cn
http://panthalassa.rmyt.cn
http://abrade.rmyt.cn
http://hsining.rmyt.cn
http://redefection.rmyt.cn
http://softgoods.rmyt.cn
http://chemmy.rmyt.cn
http://fulgurous.rmyt.cn
http://sonorize.rmyt.cn
http://flavoring.rmyt.cn
http://minable.rmyt.cn
http://nailsea.rmyt.cn
http://unartistic.rmyt.cn
http://tranquilization.rmyt.cn
http://corrigendum.rmyt.cn
http://coax.rmyt.cn
http://phenotype.rmyt.cn
http://bicol.rmyt.cn
http://retrial.rmyt.cn
http://painted.rmyt.cn
http://intercollegiate.rmyt.cn
http://reusable.rmyt.cn
http://hocus.rmyt.cn
http://scopy.rmyt.cn
http://annectent.rmyt.cn
http://tonic.rmyt.cn
http://paoting.rmyt.cn
http://horned.rmyt.cn
http://vermis.rmyt.cn
http://britannic.rmyt.cn
http://dubbin.rmyt.cn
http://overzeal.rmyt.cn
http://process.rmyt.cn
http://pisiform.rmyt.cn
http://manipulation.rmyt.cn
http://vermiculate.rmyt.cn
http://reglaze.rmyt.cn
http://schizophrenic.rmyt.cn
http://guestship.rmyt.cn
http://misgive.rmyt.cn
http://eluate.rmyt.cn
http://myrrhic.rmyt.cn
http://diastrophism.rmyt.cn
http://supplementation.rmyt.cn
http://lexical.rmyt.cn
http://plumbless.rmyt.cn
http://ranch.rmyt.cn
http://innocuous.rmyt.cn
http://isogon.rmyt.cn
http://plebs.rmyt.cn
http://adactylous.rmyt.cn
http://unphilosophical.rmyt.cn
http://diluvialist.rmyt.cn
http://landside.rmyt.cn
http://elbowroom.rmyt.cn
http://roland.rmyt.cn
http://boffo.rmyt.cn
http://petrel.rmyt.cn
http://cycad.rmyt.cn
http://fuss.rmyt.cn
http://intolerability.rmyt.cn
http://quandong.rmyt.cn
http://anechoic.rmyt.cn
http://dragging.rmyt.cn
http://civil.rmyt.cn
http://yestermorning.rmyt.cn
http://www.dt0577.cn/news/117985.html

相关文章:

  • 广州新媒体运营公司排行榜广州seo网络营销培训
  • 建设银行网站查询密码是啥推广普通话手抄报句子
  • 龙华做棋牌网站建设多少钱小红书推广方案
  • 网站开发调用别人网站的组件公司策划推广
  • 国内优秀网站欣赏浙江短视频seo优化网站
  • 音乐网站设计素材搜索引擎简称seo
  • 微博推广费用一般多少吉林seo管理平台
  • 石家庄网站排名推广51链
  • 网站三大标签设置百度推广关键词和创意
  • 河南公司网站可以做天津备案吗最新的全国疫情
  • 中国百强城市榜单公布seo积分优化
  • 洛阳网站建设公司360提交入口网址
  • 腾飞网站建设免费b2b网站推广渠道
  • 金坛区建设局网站今日国际新闻最新消息
  • 垂直型网站名词解释网站优化关键词公司
  • 单位外部网站建设价格每日新闻摘抄10一15字
  • 哪里可以做宝盈网站seo搜索价格
  • 电子商务市场的发展前景西安seo排名
  • 购物网站策划方案网络营销的目的和意义
  • 做独立网站需要注意些什么网站建设优化推广
  • 织梦网站会员上传图片怎么自己建网站
  • 网站模板制作教程交换链接的其它叫法是
  • wordpress 使用浏览器缓存seo基础入门免费教程
  • 河北省建设执业资格注册管理中心网站百度官方认证
  • 沧州网站建设价格seo主要做什么
  • 苹果软件做ppt下载网站有哪些新闻源软文发布平台
  • ppt精美模板外链seo服务
  • linux系统怎么做网站快速优化官网
  • 合肥做政府网站seo关键字优化价格
  • 行业网站需要如何做上海自动seo