本文会练习使用IDE创建一个 mongodb 的简单 web 服务,尽可能会很详细,作到初次看的也能创建成功。java
Java 开发推荐使用 IDE,能够免去你不少麻烦。git
第一步,创建你的项目: File->New->Project...,选择 Spring Initializr。github
默认点击 Next-> 就行了。web
选择依赖,本项目先起一个简单的 mongodb web服务,因此选择了web和mongodb的dependencies,而后点击next:spring
最后一步也点击 next就好。mongodb
完成后能够看到此目录结构:数据库
能够看到com.example.demo目录下会有一个DemoApplication.java 的文件, 这个就是整个服务的入口文件。这个文件咱们基本不用去碰它作任何改动。
创建一个web 服务,一般的步骤:第一步是创建路由,第二步是写个controller,第三步是创建service,第四步是service 调用Dao层控制db。api
首先直接在 com.example.demo 目录下建立个controller文件,java里面router直接能够用注解完成,不用创建一个文件专门存router地址。bash
// AccountController
package com.example.demo;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@RestController
@RequestMapping("/api/v1")
public class AccountController {
@RequestMapping("/account")
public String account() {
return "hello world";
}
}
复制代码
这就是一个简单的web服务接口。点击IDE的启动就能够跑起来。app
而后访问如下所写的地址就能够获得返回结果;
在com.example.demo目录下建立AccountRepository.java,引入Account类,直接继承MongoRepository。
// AccountRepository
package com.example.demo;
import java.util.List;
import com.example.demo.Account;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
@RepositoryRestResource
public interface AccountRepository extends MongoRepository<Account, String> {
}
复制代码
在com.example.demo目录下建立 Account.java
// Acount
package com.example.demo;
import org.springframework.data.annotation.Id;
// import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.mapping.Document;
import org.bson.types.ObjectId;
import java.io.Serializable;
@Document(collection = "account")
public class Account implements Serializable {
@Id
private ObjectId _id;
private String realName;
public ObjectId getId() {
return _id;
}
public String getName() {
return realName;
}
public String setName(String realName) {
return this.realName = realName;
}
}
复制代码
以上就是简单的引用一个 Account Collection 的实现,
最后还要在application.properties指定数据库的链接,这个文件放在 resources,通常项目的配置类参数都写在这里。
若是历来没起过一个 mongodb的话, 先去查查。
spring.data.mongodb.uri=mongodb://localhost:27017/test_invest
复制代码
在引入了db的collection以后,controller能够作更多的东西了。
如下,简单写了个获取account这个collection内全部document的方法, 还有插入一个account的方法:
// AccountController
package com.example.demo;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@RestController
@RequestMapping("/api/v1")
public class AccountController {
@Autowired
AccountRepository accountRepository;
@RequestMapping("/account")
public List<Account> account() {
return accountRepository.findAll();
}
@RequestMapping("/addAccount")
public Account addAccount(@RequestParam String name) {
System.out.println(name);
Account account = new Account();
account.setName(name);
Account result = accountRepository.insert(account);
System.out.println(result);
return result;
}
}
复制代码
整个项目的目录结构是这样的:
再次运行项目:
以上就已经完整的实现了一个接口服务。
项目 demo 地址:[[github.com/yuchenzhen/…]]