1.下载安装mongodb
百度教程:https://jingyan.baidu.com/article/09ea3ede5aff37c0aede3919.html
2.启动mongodb的服务器
(1)首先打开命令提示符,创建一个用于存放数据的目录
md d:\data
(2)启动服务
mongod ‐‐dbpath=d:\data
3…通过mongodb客户端连接服务器 (新打开一个cmd窗口)
我们在启动信息中可以看到,mongoDB的默认端口是27017
如果我们想改变默认的启动端口,可以通过--port来指定端口
在命令提示符输入以下命令即可完成登陆
mongo
退出mongodb
exit
mongodb的中文官网:https://www.mongodb.org.cn/tutorial/13.html
4.通过java连接mongodb
4.1:用maven创建工程mongoDemo,引入依赖
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.11.0</version>
</dependency>
4.2在java下创建测试类
package com.xf.test;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
public class MongodbTest {
public static void main(String[] args) {
//创建连接
MongoClient mongoClient=new MongoClient("127.0.0.1");
//打开数据库
MongoDatabase spitdb = mongoClient.getDatabase("spitdb");
//获取集合
MongoCollection<Document> spit = spitdb.getCollection("spit");
//查询记录获取文档集合
FindIterable<Document> documents = spit.find();
//遍历集合
for (Document document:documents){
System.out.println("内容:"+ document.getString("content"));
System.out.println("用户ID:"+document.getString("userid"));
System.out.println("浏览量:"+document.getInteger("visits"));
}
mongoClient.close();
}
}
这样就成功了。加油
5 条件查询
5.1查询userid为1013的记录
public class MongoDemo1 {
public static void main(String[] args) {
// 创建连接
MongoClient client = new MongoClient("192.168.206.128");
// 打开数据库spitdb
MongoDatabase spitdb = client.getDatabase("spitdb");
// 获取集合
MongoCollection<Document> spit = spitdb.getCollection("spit");
// 构建查询条件,通过userid进行查询
BasicDBObject bson = new BasicDBObject("userid", "1013");
// 通过userid查询记录获取文档集
FindIterable<Document> documents = spit.find(bson);
// 循环遍历
for (Document document : documents) {
System.out.println("内容:" + document.getString("content"));
System.out.println("用户ID:" + document.getString("userid"));
System.out.println("浏览量:" + document.getInteger("visits"));
}
// 关闭连接
client.close();
}
}
5.3、查询浏览量大于1000的记录
public class MongoDemo2 {
public static void main(String[] args) {
// 创建连接
MongoClient client = new MongoClient("192.168.206.128");
// 打开数据库spitdb
MongoDatabase spitdb = client.getDatabase("spitdb");
// 获取集合
MongoCollection<Document> spit = spitdb.getCollection("spit");
// 构建查询条件,查询visits大于1000的结果
BasicDBObject bson = new BasicDBObject("visits", new BasicDBObject("$gt", 1000));
// 通过userid查询记录获取文档集
FindIterable<Document> documents = spit.find(bson);
// 循环遍历
for (Document document : documents) {
System.out.println("内容:" + document.getString("content"));
System.out.println("用户ID:" + document.getString("userid"));
System.out.println("浏览量:" + document.getInteger("visits"));
}
// 关闭连接
client.close();
}
}
5.4`插入数据
public class MongoDemo3 {
public static void main(String[] args) {
// 创建连接
MongoClient client = new MongoClient("192.168.206.128");
// 打开数据库spitdb
MongoDatabase spitdb = client.getDatabase("spitdb");
// 获取集合
MongoCollection<Document> spit = spitdb.getCollection("spit");
// 准备数据
HashMap<String, Object> map = new HashMap<>();
map.put("content", "我要吐槽");
map.put("userid", "9999");
map.put("visits", 123);
map.put("publishtime", new Date());
//
Document document = new Document(map);
// 插入一条数据
spit.insertOne(document);
// 关闭连接
client.close();
}
}
如果想使用mongodb原生命令。
1.service注入
@Autowired
private MongoTemplate mongoTemplate;
例子:点赞功能
/**
* 点赞
* @param id
*/
public void updateThumbup(String id){
Spit spit = spitDao.findById(id).get();
spit.setThumbup(spit.getThumbup()+1);
spitDao.save(spit);
//使用mongodb原生命令
Query query=new Query();
query.addCriteria(Criteria.where("_id").is(id));
Update update=new Update();
update.inc("thumbup",1);
mongoTemplate.updateFirst(query,update,"spit");
}
1.创建一个模板或项目
2.向pom.xml导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>com.tensquare</groupId>
<artifactId>tensquare_common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
3.创建application.yml
server:
port: 9006
spring:
application:
name: tensquare‐spit #指定服务名
data:
mongodb:
host: 192.168.184.134
database: spitdb
4.创建启动类
@SpringBootApplication
public class SpitApplication {
public static void main(String[] args) {
SpringApplication.run(SpitApplication.class, args);
}
@Bean
public IdWorker idWorkker(){
return new IdWorker(1, 1);
}
}
5.创建实体类
/**
* 吐槽
* @author Administrator
*
*/
public class Spit implements Serializable{
@Id
private String _id;
private String content;
private Date publishtime;
private String userid;
private String nickname;
private Integer visits;
private Integer thumbup;
private Integer share;
private Integer comment;
private String state;
private String parentid;
// getter and setter .....
}
6.创建数据访问接口
/**
* 吐槽数据访问层
* @author Administrator
*
*/
public interface SpitDao extends MongoRepository<Spit, String>{
}
7.)创建业务逻辑类
@Service
public class SpitService {
@Autowired
private SpitDao spitDao;
@Autowired
private IdWorker idWorker;
/**
* 查询全部记录
* @return
*/
public List<Spit> findAll(){
return spitDao.findAll();
}
/**
* 根据主键查询实体
* @param id
* @return
*/
public Spit findById(String id){
Spit spit = spitDao.findById(id).get();
return spit;
}
/**
* 增加
* @param spit
*/
public void add(Spit spit) {
spit.set_id(idWorker.nextId()+""); //主键值
spitDao.save(spit);
}
public void update(Spit spit) {
spitDao.save(spit);
}
/**
* 删除
* @param id
*/
public void deleteById(String id) {
spitDao.deleteById(id);
}
}
8.创建controller类
@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {
@Autowired
private SpitService spitService;
/**
* 查询全部数据
* @return
*/
@RequestMapping(method= RequestMethod.GET)
public Result findAll(){
return new Result(true, StatusCode.OK,"查询成
功",spitService.findAll());
}
/**
* 根据ID查询
* @param id ID
* @return
*/
@RequestMapping(value="/{id}",method=RequestMethod.GET)
public Result findOne(@PathVariable String id){
return new Result(true,StatusCode.OK,"查询成
功",spitService.findById(id));
}
/**
* 增加
* @param spit
*/
@RequestMapping(method=RequestMethod.POST)
public Result add(@RequestBody Spit spit ){
spitService.add(spit);
return new Result(true,StatusCode.OK,"增加成功");
}
/**
* 修改
* @param spit
*/
@RequestMapping(value="/{id}",method=RequestMethod.PUT)
public Result update(@RequestBody Spit spit,@PathVariable String id )
{
spit.set_id(id);
spitService.update(spit);
return new Result(true,StatusCode.OK,"修改成功");
}
/**
* 删除
* @param id
*/
@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
public Result deleteById(@PathVariable String id ){
spitService.deleteById(id);
return new Result(true,StatusCode.OK,"删除成功");
}
}