您好,欢迎访问一九零五行业门户网

SpringBoot怎么整合Mongodb实现增删查改

一、什么是mongodbmongodb与我们之前熟知的关系型数据库(mysql、oracle)不同,mongodb是一个文档数据库,它具有所需的可伸缩性和灵活性,以及所需的查询和索引。
mongodb将数据存储在灵活的、类似json的文档中,这意味着文档的字段可能因文档而异,数据结构也会随着时间的推移而改变。文档模型映射到应用程序代码中的对象,使数据易于处理。mongodb是一个以分布式数据库为核心的数据库,因此高可用性、横向扩展和地理分布是内置的,并且易于使用。况且,mongodb是免费的,开源的。
二、在window10上安装mongodb打开mongodb官网
下载msi版本(安装版)
下载的时候选择custom
安装的时候,注意不要勾上安装可视化插件,否则安装会非常慢(除非你网速够快)
三、配置mongodb服务配置环境变量
复制当前路径
我的电脑->右键->高级系统设置->环境变量->系统变量
在系统变量找到path,编辑,将上面复制的路径增加进去
四、启动服务win+r->输入services.msc
服务启动后,在浏览器输入 127.0.0.1:2701
出现这行英语则代表服务启动成功。
五、springboot整合mongodb环境准备
操作系统:window10
ide:intellij idea 2018.2.4
数据库:mongodb
1)引入依赖
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-mongodb</artifactid> </dependency>
2)在application.yml添加如下配置
spring: data: mongodb: uri: mongodb://localhost/test_mongodb
完整的配置信息如下:
spring: data: mongodb: authentication-database: # authentication database name. database: # database name. field-naming-strategy: # fully qualified name of the fieldnamingstrategy to use. grid-fs-database: # gridfs database name. host: # mongo server host. cannot be set with uri. password: # login password of the mongo server. cannot be set with uri. port: # mongo server port. cannot be set with uri. repositories: type: # type of mongo repositories to enable. uri: # mongo database uri. cannot be set with host, port and credentials. username: # login user of the mongo server. cannot be set with uri.
3)新增实体类userentity
public class userentity { @id private string uid; private string username; private string password; public string getuid() { return uid; } public void setuid(string uid) { this.uid = uid; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public string getpassword() { return password; } public void setpassword(string password) { this.password = password; } @override public string tostring() { return "userentity{" + "uid='" + uid + '\'' + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; }}
4)新建测试。这里我用navicat作为mongodb的可视化工具进行查看。
测试一:插入操作
@autowired private mongotemplate mongotemplate; @test public void saveuser(){ userentity userentity1 = new userentity(); userentity userentity2 = new userentity(); userentity userentity3 = new userentity(); userentity1.setuid("111"); userentity1.setusername("用户1"); userentity1.setpassword("密码1"); userentity2.setuid("222"); userentity2.setusername("用户2"); userentity2.setpassword("密码2"); userentity3.setuid("333"); userentity3.setusername("用户3"); userentity3.setpassword("密码3"); mongotemplate.save(userentity1); mongotemplate.save(userentity2); mongotemplate.save(userentity3); }
数据库信息:
可以看到,mongodb自动创建了数据库以及通过实体类生成了集合(也就是我们经常说的数据表),而且我们已经通过mongotemplate往数据库的userentity集合插入了几条文档(也就是插入了几条记录)。而 _id 为主键,_class 则为实体类包名+类名
测试二:查询操作
@autowired private mongotemplate mongotemplate; @test public void finduserbyusername(){ string username = "用户1"; query query=new query(criteria.where("username").is(username)); userentity user = mongotemplate.findone(query , userentity.class); system.out.println(user); }
输出结果:
userentity{uid='111', username='用户1', password='密码1'}
测试三:更新操作
@autowired private mongotemplate mongotemplate; @test public void updateuser(){ userentity userentity = new userentity(); userentity.setuid("111"); userentity.setusername("更新后的用户名"); userentity.setpassword("更新后的密码"); query query = new query(criteria.where("_id").is(userentity.getuid())); update update = update.update("username",userentity.getusername()).set("password",userentity.getpassword()); //更新返回结果集的第一条 mongotemplate.updatefirst(query,update,userentity.class); //更新返回结果集的所有 //mongotemplate.updatemulti(query,update,userentity.class); }
更新后数据库如图所示:
测试四:删除操作
@autowired private mongotemplate mongotemplate; @test public void deletebyuserid(){ string id = "222"; query query=new query(criteria.where("_id").is(id)); mongotemplate.remove(query,userentity.class); }
删除后数据库如图所示:
以上就是springboot怎么整合mongodb实现增删查改的详细内容。
其它类似信息

推荐信息