springboot集成mongodb使用mongoTemplate和MongoRepository
在pom文件引入spring-boot-starter-data-mongodb依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
数据源的配置
如果mongodb端口是默认端口,并且没有设置密码,可不配置,sprinboot会开启默认的。
spring.data.mongodb.uri=mongodb://localhost:27017/mydb
如何mongodb设置了密码,这样配置:
spring.data.mongodb.uri=mongodb://kerry:123456@localhost:27017/mydb
如果多个节点集群配置
#more ip cluster
#spring.data.mongodb.uri=mongodb://user:pwd@ip1:port1,ip2:port2/database
定义一个实体类
package com.powercn.probe.model;
public class Customer {
private String id;
private String probMac;
private String devcMac;
private String inTime;
private String outTime;
private String rssi;
public Customer(String probMac, String devcMac, String inTime,
String outTime, String rssi) {
this.probMac = probMac;
this.devcMac = devcMac;
this.inTime = inTime;
this.outTime = outTime;
this.rssi = rssi;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getInTime() {
return inTime;
}
public void setInTime(String inTime) {
this.inTime = inTime;
}
public String getOutTime() {
return outTime;
}
public void setOutTime(String outTime) {
this.outTime = outTime;
}
public String getProbMac() {
return probMac;
}
public void setProbMac(String probMac) {
this.probMac = probMac;
}
public String getDevcMac() {
return devcMac;
}
public void setDevcMac(String devcMac) {
this.devcMac = devcMac;
}
public String getRssi() {
return rssi;
}
public void setRssi(String rssi) {
this.rssi = rssi;
}
@Override
public String toString() {
return "Customer [probMac=" + probMac + ", devcMac=" + devcMac
+ ", inTime=" + inTime + ", outTime=" + outTime + ", rssi="
+ rssi + "]";
}
}
定义repository接口继承mongoRepository接口
public interface CustomerRepository extends MongoRepository<Customer, String> {
public Customer findByDevcMac(String devcMac);
}
写一个接口,继承MongoRepository,这个接口有了几本的CURD的功能。如果你想自定义一些查询,比如根据firstName来查询,获取根据lastName来查询,只需要定义一个方法即可。注意firstName严格按照存入的mongodb的字段对应。在典型的
Java的应用程序,写这样一个接口的方法,需要自己实现,但是在springboot中,你只需要按照格式写一个接口名和对应的参数就可以了,因为springboot已经帮你实现了。
测试 ,springboot会自动注入mongotemplate
package com.powercn.probe.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
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.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.powercn.probe.dao.CustomerRepository;
import com.powercn.probe.model.Customer;
@Controller
public class AccController {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private CustomerRepository repository;
@RequestMapping("/probe/wp")
@ResponseBody
public Object wpPhp(HttpServletRequest request,HttpServletResponse response){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
System.out.println(sdf.format(new Date())+"-->prob post begin.......");
try {
Enumeration<String> enums = request.getParameterNames();
while (enums.hasMoreElements()) {
String name = enums.nextElement();
System.out.println("data:"+name+"="+request.getParameter(name));
}
} catch (Exception e) {
e.printStackTrace();
}
Customer cus = repository.findByDevcMac("FCE998A1E7EF");
System.out.println(cus.toString());
Customer cus1=mongoTemplate.findOne(new Query(Criteria.where("devcMac").is("FCE998A1E7EF")), Customer.class);
System.out.println(cus1);
System.out.println(sdf.format(new Date())+"-->wphp post end.......");
return "OK";
}
}
测试结果
20170828145959-->prob post begin.......
2017-08-28 14:59:59.837 INFO 7032 --- [nio-8080-exec-3] org.mongodb.driver.connection : Opened connection [connectionId{localValue:20, serverValue:450627}] to 127.0.0.1:27017
Customer [probMac=141FBA7890E8, devcMac=FCE998A1E7EF, inTime=20170612135348, outTime=20170612135401, rssi=7]
Customer [probMac=141FBA7890E8, devcMac=FCE998A1E7EF, inTime=20170612135348, outTime=20170612135401, rssi=7]
20170828145959-->wphp post end.......