PS:最后有springBoot使用mongodb方法,入参分别为map,json,entity等类型;
1. 下载: curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.9.tgz
2. 解压: tar zxvf mongodb-linux-x86_64-3.2.9.tgz
3. 安装mongo
3.1) 进入指定文件夹并
cd /usr/local/
3.2) 创建mongodb文件夹
mkdir mongodb
3.3) mongodb-linux-x86_64-3.2.9下的文件复制到指定文件夹/usr/local/mongodb;
mv /usr/local/mongodb/mongodb-linux-x86_64-3.2.9/* /usr/local/mongodb
3.4) 删除原mongodb-linux-x86_64-3.2.9文件夹及文件;
rm -rf mongodb-linux-x86_64-3.2.9
4. 启动mongo
/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/data/mongodb.log --logappend&
5. 查看mongo是否启动成功
ps -ef | grep mongodb
6. 回到根目录,进入mongo,输入help 就可以开始操作mongo了
cd /
./usr/local/mongodb/bin/mongo
--------------------------------------------------- 小分割线 --------------------------------------------------
7. 为了方便 把mongo放到 根目录bin下
ln -s /usr/local/mongodb/bin/mongo /bin/mongo
8. 启动
./bin/mongo
9. 设置账号密码, 可参考: https://www.cnblogs.com/mengyu/p/9071371.html
db.createUser({user: "admin", pwd: "asd123", roles: [{ role: "dbOwner", db: "book" }]})
// 用户 密码 控制级别 哪个文档
--------------------------------------------------- 大分割线, 后台使用 --------------------------------------------------
0. pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
1. MongoTestC.java
package com.book.controller;
import com.alibaba.fastjson.JSONObject;
import com.book.dao.entity.MongoTest;
import com.book.service.MongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
/**
* @author :于志强
* @date :Created in 2019/9/12 15:14
* @description:
*/
@RestController
public class MongoTestC {
@Autowired
private MongoService mtdao;
@GetMapping(value="/test0")
public void saveTest0() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 22);
map.put("name", "test0");
JSONObject json = new JSONObject(map);
mtdao.saveTest0(json);
}
@GetMapping(value="/test6")
public void saveTest6() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("id", 5);
map.put("name", "2222");
mtdao.saveTest6(map);
}
@GetMapping(value="/test1")
public void saveTest() throws Exception {
MongoTest mgtest=new MongoTest();
mgtest.setId(11);
mgtest.setAge(33);
mgtest.setName("ceshi");
mtdao.saveTest(mgtest);
}
@GetMapping(value="/test2")
public MongoTest findTestByName(){
MongoTest mgtest= mtdao.findTestByName("ceshi");
System.out.println("mgtest is "+mgtest);
return mgtest;
}
@GetMapping(value="/test3")
public void updateTest(){
MongoTest mgtest=new MongoTest();
mgtest.setId(11);
mgtest.setAge(44);
mgtest.setName("ceshi2");
mtdao.updateTest(mgtest);
}
@GetMapping(value="/test4")
public void deleteTestById(){
mtdao.deleteTestById(11);
}
@GetMapping("test5")
public Map findTestById(HttpServletRequest request) {
Map mgtest= mtdao.findTestById(Integer.parseInt(request.getParameter("id")));
return mgtest;
}
}
2. MongoService.java
package com.book.service;
import com.alibaba.fastjson.JSONObject;
import com.book.dao.entity.MongoTest;
import com.mongodb.BasicDBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author :于志强
* @date :Created in 2019/9/12 15:13
* @description:
*/
@Component
public class MongoService {
@Autowired
private MongoTemplate mongoTemplate;
/**
* 创建对象
*/
public void saveTest(MongoTest test) {
mongoTemplate.save(test);
}
/**
* 创建对象
*/
public void saveTest0(JSONObject document) {
mongoTemplate.save(document);
} /**
* 创建对象
*/
public void saveTest6(Map map) {
mongoTemplate.save(map, "tetetet");
}
/**
* 根据用户名查询对象
* @return
*/
public MongoTest findTestByName(String name) {
Query query=new Query(Criteria.where("name").is(name));
MongoTest mgt = mongoTemplate.findOne(query , MongoTest.class);
return mgt;
}
/**
* 根据用户名查询对象
* @return
*/
public Map findTestById(int id) {
Query query=new Query(Criteria.where("id").is(id));
Map mgt = mongoTemplate.findOne(query , Map.class);
return mgt;
}
/**
* 更新对象
*/
public void updateTest(MongoTest test) {
Query query=new Query(Criteria.where("id").is(11));
Update update= new Update().set("age", test.getAge()).set("name", test.getName());
//更新查询返回结果集的第一条
mongoTemplate.updateFirst(query,update,MongoTest.class);
//更新查询返回结果集的所有
// mongoTemplate.updateMulti(query,update,TestEntity.class);
}
/**
* 删除对象
* @param id
*/
public void deleteTestById(Integer id) {
Query query=new Query(Criteria.where("id").is(id));
mongoTemplate.remove(query,MongoTest.class);
}
}
3. MongoTest.java
package com.book.dao.entity;
import lombok.Data;
/**
* @author :于志强
* @date :Created in 2019/9/12 15:13
* @description:MongoTest
*/
@Data
public class MongoTest {
private Integer id;
private Integer age;
private String name;
}
4. 测试方法(需要入参的自行处理)
http://localhost:8181/test0
http://localhost:8181/test1
http://localhost:8181/test2
http://localhost:8181/test3
http://localhost:8181/test4
http://localhost:8181/test5
http://localhost:8181/test6