原創 2016-08-04 景峯 Netkillerhtml
Maven pom.xmlweb
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>netkiller.cn</groupId> <artifactId>api.netkiller.cn</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>api.netkiller.cn</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.0.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <scope>compile</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.3</version> <configuration> <source /> <target /> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build> </project>
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); } }
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 + "]"; } }
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(); } }
> 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" }
延伸閱讀redis
Spring Boot 快速開始spring
Elasticsearch 瞬間入門mongodb
Elasticsearch 急速入門·文檔與搜索apache
使用OpenLDAP 操做 Windows Active Directoryapp
長按下面二維碼,關注個人公衆號,天天推推送原創技術文章。maven