Spring Boot MongoDB 例項
阿新 • • 發佈:2022-05-02
節選自《Netkiller Java 手札》
11.12.3. Spring boot mongodb
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.data.authentication.UserCredentials; import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import com.mongodb.Mongo; @Configuration @SpringBootApplication @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class }) @ComponentScan({ "cn.netkiller.controller", "cn.netkiller.rest" }) @EnableMongoRepositories public class Application { @SuppressWarnings("deprecation") public @Bean MongoDbFactory mongoDbFactory() throws Exception { UserCredentials userCredentials = new UserCredentials("finance", "En7d0l0wssXQ8owzedjb82I0BMd4pFoZ"); return new SimpleMongoDbFactory(new Mongo("db.netkiller.cn"), "finance", userCredentials); } public @Bean MongoTemplate mongoTemplate() throws Exception { return new MongoTemplate(mongoDbFactory()); } public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
11.7.2.3. POJO
ackage cn.netkiller.pojo; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection = "tracker") public class Tracker { @Id private String id; private String name; private String unique; private String hostname; private String referrer; private String href; public Tracker() { // TODO Auto-generated constructor stub } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUnique() { return unique; } public void setUnique(String unique) { this.unique = unique; } public String getHostname() { return hostname; } public void setHostname(String hostname) { this.hostname = hostname; } public String getReferrer() { return referrer; } public void setReferrer(String referrer) { this.referrer = referrer; } public String getHref() { return href; } public void setHref(String href) { this.href = href; } @Override public String toString() { return "Tracker [id=" + id + ", name=" + name + ", unique=" + unique + ", hostname=" + hostname + ", referrer=" + referrer + ", href=" + href + "]"; } }
11.7.2.4. Controller
package cn.netkiller.controller; import cn.netkiller.pojo.Tracker; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*; @Controller public class TrackerController { @Autowired private MongoTemplate mongoTemplate; public TrackerController() { } @RequestMapping("/tracker/test") @ResponseBody String hello() { return "Hello World!"; } @RequestMapping("/tracker") @ResponseBody String execute() { Tracker tracker = new Tracker(); tracker.setName("test"); tracker.setUnique("111223456"); tracker.setHostname("www.example.com"); tracker.setHref("http://example.com/test.html"); tracker.setReferrer("http://example.com/"); this.mongoTemplate.insert(tracker); return tracker.toString(); } }
11.7.2.5. 檢視測試結果
> db.tracker.find();
{ "_id" : ObjectId("5757c0b92c526a6bda5eea3a"), "_class" : "cn.netkiller.repositories.Tracker", "name" : "test", "unique" : "111223456", "hostname" : "www.example.com", "referrer" : "http://example.com/", "href" : "http://example.com/test.html" }